Clean Code Tip #7 – Method Purpose

#7:  Methods should either do something or decide something, but not both.

For example, the method below has a method GetDistinctArrarCount that returns an array count and it removes empty elements.  The removal elements should be done separately since the method purpose is to get the array count.

static void Main(string[] args)
        {
            List<string>  people = new List<string>() { "Brian", "Sara", "", "Bob"};

            int count = GetDistinctArrayCount(people);

            Console.Write(count);
            Console.ReadLine();
        }

        private static int GetDistinctArrayCount(IEnumerable<string> arrayValues)
        {
            // Remove Emtpy Values
            arrayValues = arrayValues.Where(x => !String.IsNullOrEmpty(x));

            // Return Distinct Count
            return arrayValues.Distinct().Count();
        }

Here’s a better way to write this:

 static void Main(string[] args)
        {
            List<string>  people = new List<string>() { "Brian", "Sara", "", "Bob"};

            // Remove empty elements before getting the distinct count
            people = people.Where(x => !String.IsNullOrEmpty(x)).ToList();
            int count = GetDistinctArrayCount(people);

            Console.Write(count);
            Console.ReadLine();
        }

        private static int GetDistinctArrayCount(IEnumerable<string> arrayValues)
        {
            // Return Distinct Count
            return arrayValues.Distinct().Count();
        }

Clean Code Tips #4-6: Method Arguments

#4:  Methods should have as few arguments as possible.  Ideally, they should never have more than three.  If there’s more than three, then it’s likely the arguments should be wrapped into a class of their own.

#5:  Boolean arguments should be avoided because it complicates the method name and the purpose of the method.  Passing a boolean will likely cause your method to do two different things based on the bool.  This is in contradiction to rule #2.

#6:  Methods that change input arguments should be defined so the argument is the return value.  This also prevents the usage of output arguments which can be confusing.

Clean Code Tips #1-3: Methods Should be Small, Do Only 1 Thing, and 1 Level of Abstraction

#1:  Methods should be small.  Ideally, they should hardly ever be 20 lines long.  This forces the developer to create more methods that can shared and tested more easily.  Smaller methods will also result in less unit tests for each method since there’s less logic applied.

#2:  Keeping method small also helps to enforce the concept that methods should only do one thing.  The more logic that exists in a method will likely limit it’s ability to be shared and reused.  It also ensures that changes to the method will only affect the single purpose of the method.

#3:  One level of abstraction per method is also a good rule to follow.  This helps others understand what the method is doing which prevents bugs from getting into the software.

Following the above tips will result in more methods, but it doesn’t necessarily result in more code since methods can be shared more easily.