1. 2016
    Jun
    30

    Importance of Clean Code

    Posted By


    clean code

    It doesn’t matter which programming language, anyone that has spent years programming understands the importance of “clean code”. Looking at someone else’s code generates 2 reactions, either leaves you with smiling at the brilliance of the design or cursing at the headache you just inherited. In either case It becomes clear in software development you need someone else to understands what your code is going, it makes it easier to test, it lowers maintenance burden and it helps make bugs more obvious, you can even make the argument that “clean code” fosters innovation, something that inspires a newbie to greater heights, part of the evolution of things. But have you ever gone back to something you have written 3 years ago and wondered “what was I thinking?” Then you realize that you’re not immune and like any good habit “cleanliness” is a habit that can and must be developed!

    How do you go about writing clean code?

    It’s a common misconception that “clean code” is hard to write, although it takes time of cultivate, like any good habit it just takes dedication and a little practice. Here are some simple things you can do to improve your coding habits.

    Use intention-revealing names to name variables and methods

    When naming variables and methods it’s best practice to provide meaningful names that gives the reader an idea of what the variable or method will represent , it’s easier to understand what the variable or method is doing if the reader is able to pronounce the name of the variable or method. If you are having trouble figuring out what name you should give the variable or method, you can always use the verb-noun naming convention rule. Give your methods and functions verbs names like public void eat(), public void sleep(), public void increasePrice () and your classes and attributes nouns names like class Dog class Product or private int price

    It also best practice to always make sure you identify the “visibility” of the variable or method so the reader knows how it will interact will other classes protected, private or public

    Construct better methods and functions

    Even when there is a large class that is required to do multiple complex tasks, it’s best practice to make sure that each component (method) takes up a small amount to that task. For instance if you have a shopping cart class, one method should be adding items public void addToCart(String itemName, double price, int quantity) another should be updating items public void updateCart(String itemName, double price, int quantity). There should no ambiguity or confusions about what the methods or functions should be doing, 2 functions cannot be doing relatively the same thing, each function MUST be doing only one thing.

    When handling errors its better throwing an exception than returning different error codes. The nice this about exceptions it bubbles up, a method can detect an exception throw by a different method called during its execution. Makes things a whole lot easier when debugging and you are trying to find the root cause of your issue.

    Comments and documentation is essential

    I cannot overemphasis the importance of documentation in your code, it’s the first thing any reader will do when looking at when trying to understand your code. It should be a part of any programmers coding style, surprisingly it’s not. And it’s understandable, when rushing to meet tough deadlines it’s hard to go back to your code and do documentation. My tip is when initially naming your variable or method also add a comment in as well, might not be much at first. Making this a habit you will notice it forces you always documenting your code. Exception is code that goes live for production

    Benefits of Clean Code

    There are a number of benefits gained from writing clean high quality code, one obvious benefit you don’t have to bushwack your way through messy code.

    Reduce number of bugs

    Because the is less code there are less chances of creating weird bugs, it’s also easier to find and fix bugs in your code.

    Quicker execution time

    Clean code are more efficient, you can notice it when you check the execution time of running the program. This is a big competitive advantage because it makes for a pleasurable user experience. User’s nowadays care about ease of access and get turned off with application is slow or takes too long to execute a function.

    Show of Professionalism

    Developers know when they come across good, clean code and acknowledge programmers that write clean code. It serves as a source of inspiration of newbie’s and beginners and shows professionalism and experience in your line of work. Because clean code are usually so well done gets reused and utilized by other developers in their code to help complete tasks.


  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.