What is clean code?
We all start writing rather dirty code, and somehow, during our learning phases, we get better and better at writing clean code. It happens automatically. At least most of the time.
Clean or readable code is achievable very quickly. We can discuss several factors like putting the curly brackets {} in the current or the next line.
“Brackets {} belong in the next line. Just use my correct brackets formatter” — zgast’s opinion
However, those factors only count as an opinion. May the official code conventions show something else, but it is okay as long as it is readable. Clean code is more about consistency and being expressive with the fewest amount of words. It would be no problem to describe a function with a 200-word essay, but why the extra effort? One expressive name can do the job.
What is this magic rule?
Many people think there is only one true style to follow, but this does not seem right. Putting the brackets in the same line saves vertical space but putting them in the next line makes everything more grouped. It depends on what the developer prefers. Like everything in life, even this has its pros and cons.
The first thing a developer encountered is often the thing he will keep using. If the developer started with the inline {}-style he will likely stick to that. The same thing counts the other way around.
The preferred {}-style has nothing to do with the first language. Suppose the first tutorial the developer watches is in Java but made by a C++ Engineer. In that case, he will probably use the next line {}-style instead of the typical Java inline {}-style, because the content creator uses that style. Beginners are unaware of that and think this is the correct way for everything.
Consistency is key
Keep the code consistent. Do not switch the bracket-style in the middle of a project.
It’s similar to creating a text document: nobody switches the typeface after each sentence written. They stick to one throughout the whole file, except for headers.
What is clean code now precisely?
Clean code is easy to read. It prevents bugs. It is consistent, and the readers instantly know what the code does. Clean code is excellent code.
Everyone knows what this code does, but it is a horrible code. It is prone to bugs and takes more time to read.
The optimised way is to return “admin” directly, and this also saves you a ton of headaches.
This approach is way better.
Focus
Clean code also has focused. Every class or function is written with a single purpose behind it. Methods should do only one thing, otherwise split them into multiple methods. Doing this forms more logical blocks, and it is easier to reuse existing code.
Duplications
One of the best IDE features is static code analysis. It also tells how often a code is duplicated and how to merge these duplicates into one function. It is fast and reliable. The best way to make code more organised. Very simple, but effective.
Naming variables and methods
Never name a variable with one single letter, except while iterating in a loop. There are no additional comments needed if a variable or method has an expressive name like daysSinceLaunch
and not d
. It is easier to use this variable or method repeatedly because everyone instantly knows what it does. It also helps the developer to prevent many serious bugs.
Some feel that commenting functions is not needed if the code is well written, but comments are always helpful. They display the developer’s intents, and they help make complex functions and variables more understandable.
However, if your code is not understandable without comments, it may be a good idea to split functions into simpler pieces or rewrite some code.
Do not put any useless information in a name. It is evident that this is a variable. How about just counter
?
Is it called a function or a method?
What does this have to do with clean code? It means a lot. If the developer writes a comment like “this calls the helper method” but you actually mean a function, there can be a lot of confusion.
A function is static. A method is related to an object. Usually, helper functions are static, so the comment above could confuse many developers. “this calls the helper function” makes more sense.
Simplicity
Many developers try to show off. They put everything they know in a single project. In the eyes of the developer, it looks great and useful. For everyone else, it may be an enormous mess.
Conclusion
Remember to write expressive variable names, consistent code and keep everything simple, even if you are writing the smallest piece of code in the world. It will help in many aspects!