Paddockson

Paddockson

First And Most Important Lesson To Writing Good Code

First And Most Important Lesson To Writing Good Code

I made a lot of mistakes in my first 6-12 months as a developer in the industry and learnt a lot of new habits the hard way. This one in my opinion is by far the most important!

I found I had a good level of competence learning new technologies and how to put my current knowledge into practice within a new way of working. The first lesson was: Clean code = Happy dev and software. Clean code is so important and you should either learn this as soon as possible to conform to Microsoft coding standards or you will eventually learn the hard way!

Examples of messy code:

        public bool MyCodeIsHappy(int Levelofhappiness)
        {
            bool happycode = false;

            if (Levelofhappiness == 0)
            {
                happycode = true;
                return happycode;
            }

            if (Levelofhappiness == 1)
            {
                happycode = true;
                return happycode;
            }
            else
            {
                happycode = false;
                return happycode;
            }
        }

If you can spot 3 issues with this code then you are doing very well as a clean coder.

Issues with this code:

1. Passed in parameter name does not conform to Microsoft naming conventions

thats-a-dumb-name-wrong-name.gif

Microsoft naming conventions says that parameters, local and global variables should not begin with a capital letter and then each new word after that within that variable should have a capital letter.

So the variable Levelofhappiness should be levelOfHappiness.

And happycode should be happyCode .

Naming conventions can be found inside the Microsoft docs: docs.microsoft.com/en-us/dotnet/standard/de..

2. Unnecessary amount of code

giphy.gif

There are way too many IF statements in this code! Unnecessary code can come in all shapes and sizes and for someone new to CS and development it can be hard to identify what is necessary and what is not.This code can easily be reduced for optimisation reasons and could be more readable for future devs. Simply rethinking and refactoring your code can make all the difference!

A good rule to live by is to read through any new code you write and think to yourself if this can reduced and refactored to be more efficient.

            if(Levelofhappiness <= 1)
            {
                happycode = true;
                return happycode;
            }
            else
            {
                happycode = false;
                return happycode;
            }

3. Repeating code

ComplexVapidFieldmouse-size_restricted.gif

We have the same line of code in our method 2 times now, rethink and refactor again to see if maybe you can reduce this so its only being called/used once!

            if(levelOfHappiness <= 1)
            {
                happyCode = true;
            }
            else
            {
                happyCode = false;
            }

           return happyCode;

Sometimes when you have repeating blocks of code you can store them into there own method for later calls, easier debugging and nice CLEAN CODE!

Example of storing repeating code into a new method like this:

        public bool MyCodeIsHappy(int levelOfHappiness)
        {
            bool happyCode = false;
            if (levelOfHappiness <= 1)
            {
                var newLevelOfHappiness = levelOfHappiness * 2;
                if (newLevelOfHappiness == 2)
                {
                    happyCode = true;
                }
                else
                {
                    happyCode = false;
                }
            }
            else
            {
                var newLevelOfHappiness = levelOfHappiness * 2;
                if (newLevelOfHappiness == 4)
                {
                    happyCode = true;
                }
                else
                {
                    happyCode = false;
                }
            }

            return happyCode;
        }

This can so easily be reduced by returning the bool in a new method and passing in a new parameter:

        public bool MyCodeIsHappy(int levelOfHappiness)
        {
            bool happyCode = false;

            if (levelOfHappiness <= 1)
            {
                happyCode = IsHappy(levelOfHappiness, 2);
            }
            else
            {
                happyCode = IsHappy(levelOfHappiness, 4);
            }

            return happyCode;
        }
        public bool IsHappy(int levelOfHappiness, int maxHappiness)
        {
            var newLevelOfHappiness = levelOfHappiness * 2;
            if (newLevelOfHappiness == maxHappiness)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

You can store the logic within the if statements into its own method, this is a lot more readable while debugging and we can even wrap Unit Tests around IsHappy for ensuring that the logic stays intact during future development!

I hope this gave a better insight on clean coding and my first lesson as a developer that has helped and guided me for the past 3 years ... and also given me a reason to moan at juniors!

Clean code will be a skill on its own so please do not be discouraged if you do not get into the habit straight away but learning from your mistakes is how we become better developers. Happy coding!

 
Share this