C# Functions that might help pt. 1

14. September 2009

When talking to a friend of mine we had the idea to do a blog series over C# functions that might help people if they don?t know about them. This will be categorized so you can keep up on it, and it will be once a week on Mondays. First I was going to start with ?continue?. Nest ifs inside a loop cause major performance issues in most applications but there are a few ways to avoid this. The MSDN definition of continue is ?The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.? . Here is the code snippet that we are going to clean up. ?Caution the following code is not suitable for young viewers or those that are faint of heart.?

IEnumerable<ReflectionParameter> test = new List<ReflectionParameter>();

            foreach (var parameter in test)
            {
                if (parameter != null)
                {
                    parameter.MethodName = "Something";
                    if (parameter.children != null)
                    {
                        foreach (var child in parameter.children)
                        {
                            if (child.MethodName == null)
                            {
                                child.MethodName = "Something";
                            }
                        }
                    }
                }
            }

I know right? We can clean this up by inverting the first if to a continue statement.

IEnumerable<ReflectionParameter> test = new List<ReflectionParameter>();

            foreach (var parameter in test)
            {
                if (parameter == null) continue;
                parameter.MethodName = "Something";
                    if (parameter.children != null)
                    {
                        foreach (var child in parameter.children)
                        {
                            if (child.MethodName == null)
                            {
                                child.MethodName = "Something";
                            }
                        }
                     }
            }
We can still clean this up by inverting the children check to a continue statement.
IEnumerable<ReflectionParameter> test = new List<ReflectionParameter>();

            foreach (var parameter in test)
            {
                if (parameter == null) continue;
                parameter.MethodName = "Something";
                if (parameter.children == null) continue;
                foreach (var child in parameter.children)
                {
                    if (child.MethodName == null)
                    {
                        child.MethodName = "Something";
                    }
                }
            }

We can also do this to the if inside the second for each loop.

IEnumerable<ReflectionParameter> test = new List<ReflectionParameter>();

            foreach (var parameter in test)
            {
                if (parameter == null) continue;
                parameter.MethodName = "Something";
                if (parameter.children == null) continue;
                foreach (var child in parameter.children)
                {
                    if (child.MethodName != null) continue;
                    child.MethodName = "Something";
                }
            }

This is a much cleaner section of code but is not great. It still has a nested for each loop. which makes my insides scream, but we will attack that sort of nesting next week.

.NET, Productivity, C# Helpful Functions

Comments

3/16/2010 3:42:47 AM #
I Carry hearing to the information speak just about having totally free on-line grant functions so I have been searching all-around for your ideal web page to get one.
3/29/2010 8:28:30 PM #
Tolles Gewinnspiel, vielen Dank für den Tipp
3/29/2010 9:29:38 PM #
Thanks for this! This is awesome!.
3/31/2010 1:11:42 AM #
Thanks a bunch I really enjoyed reading this. It makes me want to create my own weblog! Just what topic though? I am a dentist by trade but can't imagine most people wishing to read about dentistry? Maybe I am wrong! Tradeking Review
3/31/2010 1:56:08 AM #
Great blog and post cheers. How long has this blog been running now? The only thing is I seem to be having some technical difficulties getting to your RSS feed though. Whitesmoke Review
4/8/2010 12:57:45 AM #
I normally don't post in Blogs but your blog drew me to, spectacular work,beautiful!
4/8/2010 9:27:45 PM #
I usually don't post in Blogs but your blog drew me to, sensational work..beautiful!
4/9/2010 5:35:27 AM #
I normally don't post in Blogs but your blog pushed me to, stunning work..beautiful!