Blog RefWyWen = new Blog(); //This blog is about refactoring: why and when should be done?.

These days I'm enjoying reading "Clean Code: A Handbook of Agile Sofware Craftsmanship " by Robert C. Martin, which I recommend any software developer to read because it makes me feel smart and dump at the same time! smart when I shout: That what I always think how code must be written! and dump when seen my code is messy as hell!

So here I will try to summarize what I find the "two" most common mistakes that we as software engineers fall into most the time by noticing my Smart/Dump moments, and those mistakes not only brings more bugs to our code but also makes the code harder to maintain to fix those bugs.

First Mistake: Naming wrongly!

The book starts with the following image:


which makes me really thinking: "what makes me say WTF while reading a code?" and I notice my self saying that -even on my own code- on two scenarios:

Names!

Reading the name of a class, variable, method ... etc, that is shorted in a way that lost its meaning or reading a name that has no relation with what that code element is nor containing nor doing!
So it's so critical to name properly, and the book gave a bit of good advice on dealing with naming: Names should be pronounceable, could be said loudly and searchable, and the most important thing is it relevant.

Examples:

Variables and Objects:
naming an object as notiSrv is hard to understand or read for anyone how doesn't write the code, but name it as notificationService is better because it is searchable and pronounceable and making sense.
the only role for naming an object and non-boolean variables, that it should be none, not a verb like notificationBuild this is even hard to understand its type.
 For boolean I recommend naming to be a question like isPageLoaded or PasswordIsValid, Therefore, it could be answered using yes or no (true or false).

Functions:
The function name should be a verb, showing the action that this function is doing, what it should handle and when (by when I mean which event or in which situation).
Therefore, RegULDT() is confusing name, and no one even the one who wrote it can understand what this function is doing, and you will be surprised by knowing this function should register login date and time for a user, and it is better to be LoginTimestamping().

Therefore, clear naming will reduce comments that describe your code, because the code will describe itself.


Since we already mentioned functions, we will move to my next "WTF shouting" scenario which is:

Long Nested functions:


Long Nested functions, the book advice to make small functions, and even that small function should be smaller! that makes code editing easier and clear, even reading code will be faster, and walks you step by step to its logic.

Example:


I had this constructor on app.component.ts for an Ionic app that I'm working on, it doesn't have that much code but it was confusing:


And it had been changed to be something like this:

and that's it!

Comments

Popular posts from this blog

How to Automatically deploy Configuration Files with Azure DevOps CD pipelines

Adding Multiple DB Contexts in your DotNet Project

Adapting a New Technology: A General Guide to keep Your Systems up-to-date