Clean Code – Part 5

Hello, how are you?

My last post was too long ago because my pos graduation is taking all my time. I was doing a project and I had o hard time coding a full system. That is why I am without time to post.
Let us keep talking about clean code? The older posts you will find here: Part 01, Part 02, Part 03, Part 04

  • Copy and Paste (even in non OO codes) -> Avoid doing this copy and paste. When you got a task there is an easy way to finish it, it is very easy to copy a code from a place and past it to where we have to use it. By doing this code replication we are increasing the difficult of the code maintenance, breaking good coding patterns and even worst, propagating some bugs.Imagine if we got a complex method, and we need to use it in another place of our software; instead of doing a change to transforme the code to be more generic and apply it to every situation, some developers choose the easiest way to finish the task that is coping and pasting the method. After pasting the code the developer just have to do small changes to complete the task. Imagine now that we have a bug in this pasted code, so we fix the pasted and the original code. The problem is that the older developer did not tell us that this code was replicated more three times. Can you see the problem of replication?

    It would be easier create just one method to be invoked in all parts of the system.

    When writing your code, if you think something like: “I have done/seen something like this before…” it means that is time to you to stop and think. Be aware of any kind of replication:

    • Non OO codes -> Create just one method and use it in any place of your code.
    • OO codes -> Time to apply super-class, abstract class, etc.
  • Remove unnecessary code -> Have you notice the large amount of commented code on legacy software? By example:
    // user.setCreditCardByDate(credidCard, new Date());
    user.setCreditCard(credidCard);
    list.add(user);
    //for(int a = 0; i < list.size(); i++){
        //daoUser.deleteCredidCardHistory(list.get(i));
        //Logger.debug("im here!!!!");
    //}
    daoUser.deleteCredidCardHistory(list);

    If the commented code has no more use, why will you keep it? Sometimes it is normal keep the older code while you test the new one, but saving it? If you are not so sure about the code you wrote, it is better think twice before saving it.
    If we have a tool to control our software version, what is the purpose of saving a commented code? With this kind of versioning tool we can always check the older code.
    The committed code must be clean, easy to understand and with a straight objective. When your code has a lot of old commented code, the current code might lose its focus because the developer might try to understand the commented code and forget about the current code.
    Take a look on how the above code would be so much easier to read:

    user.setCreditCard(credidCard);
    list.add(user);
    daoUser.deleteCredidCardHistory(list);
  • Knowledge of the development language -> Always seeks to upgrade the knowledge you got in your development language. Sometimes your development language already has the method that you are trying to create. “There is no need to re-create the wheel”. Keep updated about your development language. When some task demands you to do something you never done before, research.
    Someday I found this code on certain software and became a joke on the company:

    for(int i = 0; i < 10; i++){
        //... Do stuff
        // Get inside the if when finds the condition
        if (...){
            i = 13;
        }
        // Do stuff
    }

    You can see that instead using the word break, the developer changed the counter value. That task could be done in an easier way and with more style:

    for(int i = 0; i < 10; i++){
        //... Do stuff
        // Get inside the if when finds the condition
        if (...){
            break;
        }
        // Do stuff
    }

    Maybe you think: “Well, at least was working right?!” I will answer your “Yes”, but I will also say that you will not become a joke at your company if you keep updated about your working tool. Another thing to think about the code above is: “What if we must change the counter value”? “What if we have to change the counter value?” “What if we have to change the counter value from 10 up to 100?” You would have to change at the “for” declaration and in the “if”. It’s not worth it.;

  • TODO -> The IDEs that we can find today help us with this reminder. We can easily write “// TODO”, and see this reminder all over the class/system. It is really easy to put this tag TODO in all system code, and it can remember us of everything that we have to do.

TODO

  • The problem is “as easy is to create it, the TODO will be easy to forget about it”. Once this is usual to all company, tons of TODOs codes might appear at the Task view in a short a time, doing your code dirty.One way out to avoid this kind of problem, once you know that the problem will not be fixed in a short time you will not write the tag TODO in your code (To more information about this approach: Watch out for TODO comments! By Alexandre Gazola).
    Other way out it would be to create your own tags. In your company you could agree in use the task TODO just to codes that will be fixed before the end of the task. We could use like: “// REMAKE” or “// REFACTOR” and by the IDEs we can filter by created TAGs.
  • Lose your ego -> I believe that one of the most difficulties of the human being is to lose the ego. Maybe you got a task to do, trusting in your 30 years of experience you will not talk with anyone about your task and will not gather ideas to do that. Ego it is something that only harm the human being. The best thing to do is to have a quick chat with a few people of your team to gather ideas, put them all together, and you can be sure that you code will have everything to get good design patterns, the right technologies and a good OO modeling.
    Just because someone was recently hired to the team, would mean that he does not have any knowledge. Talking and gathering ideas will take your project to another level.
    Leave your ego away; it will not take you anywhere.

That is all for today.

If you have any question, just post it. I do not know when I will post again because the Pos Graduation is taking a lot of time, but soon I will have more tutorials to post here.

See you later! o_

Leave a Comment