Geeks With Blogs

News

Zac Harlan
Before I begin any large undertaking I like to do a little research first. I was reading a lot ofJeff Atwood at the time of the bomb going off so the book Code Complete seemed like a logical place to start. This book was a great read and served as a way to clean up my coding practices. For example, I’d routinely write code like this for a lot of my looping processes:
<Code>
            int x = 0;
            for (x = 0; x < 4; x++ )
            {
                //do stuff
            }
</Code>

There are a number of things wrong here but aside from . I knew about the other looping routines but was able to manipulate almost everything I needed to do with the above syntax. I was a hammer and everything looked like a nail. After reading through Code Complete I was faced with a decision. I could either file the knowledge I had just gained from the reading or I could implement it in my daily coding life and see what happened as a result.
Here is a snippet of code I wrote before reading Code Complete:
        public static void TestMethod2()
        {
            var arrArray1 = System.IO.Directory.GetFiles("c:\\somedir\\");
            int x;
       
            for (x = 0; x <= arrArray1.Length - 1; x++) {
                if (arrArray1[x]== "test.txt")
                {
                    //do stuff
                }
               
            }
        }
*full body shiver*
And after
        public static void TestMethod()
        {
            var logFiles = Directory.GetFiles("c:\\somedir\\");
            foreach (var logfile in logFiles)
            {
                if (logfile == "test.txt")
                {
                    //do stuff
                }
            }
        }
*smiling sigh*
I’m sure there is still room for improvement but I wanted to illustrate how much cleaner and more readable the code looks with just a few tweaks to my coding practices. The second snippet doesn’t have to manage any counters and it’s clear that I’m looping through a collection of log file. 
Nothing fancy or complex, just taking a little more care in what I do.  Turning my occupation into my craft if you will ;-)
Posted on Sunday, July 19, 2009 9:58 AM | Back to top


Comments on this post: A little more care.

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © ZacHarlan | Powered by: GeeksWithBlogs.net