Readable code is maintainable code
In this short post, I will go over some of the rules that you can apply to improve your code. All cod...
For further actions, you may consider blocking this person and/or reporting abuse
"Guard clause" is the commonly used title for early exit.
Oh thanks! Learned something new today!
Honestly, I get stuck with how to clean my code, but this is my rule of thumb. What do you think?:
The main (class and/or) method is considered unrelated to anything else, while, unrelated sections are 3 lines down, semi-related, 2, and related 1.
They don't always have to be labeled with comments, but, I think it looks neater.
One example is the
CPane
class from my most recent post (here).Another is:
Again, tell me what you think of my writing style, I'd love to hear feedback.
You don't need comments to explain what your code is doing. Better practice is to use code to explain what the code is doing.
for your last example you could rather do this:
In this example out functions are telling the developer exactly what is happening and splitting the code up into sections instead of comments. A common issue is that comments can lie. If you update the code and forget to update your comments your comments will be lying and can confuse you. If your code is readable you shouldn't need comments to explain what is happening.
Uncle Bob explains the use of comments much better in this talk:
youtu.be/2a_ytyt9sf8
and I would recommend watching the entire clean code series if you have time.
Is it bad, or to a disadvantage to use comments, then?
Yes, experience teaches us that comments must be the last resource to be used only when the logic is very hard to explain. Even so in most times is better to break that complicated logic into multiple small functions to be easy to read that code later.
Always try to name all variables, functions, and classes with meaningful names that self explain itself, avoiding the usage of comments.
Also, another problem with comments is that when the code changes very often the developers don't update the comments (the linter don't tell us that they are wrong now) and they don't reflect anymore the current logic of the code.
Wow. Cool suggestion. I do this all the time in Readme files, but never tried on DEV. To
Great post!
I'm from HelloGitHub, a nonprofit organization in China. I am reaching out to you to ask if it's OK for us to translate your posts, "Clean up your code by applying these 7 rules", into Chinese and publish it on our WeChat official account. You own the copyright of course.
How does that sound to you?
You can translate it and publish it, thanks for asking! I appreciate the interests!
Nice article!
I really love to boyscout rule but I'm always afraid to destroy something 😂
About the team code style, every team should think about that, and a lot of them don't know or don't want to "waste" time about that..what a pity!
For the boy scout rule, the tests should help you to not break something. And most of the time people forget about very important parts before they start a project. Like the code style.
Why so verbose? Why not just:
It's equally readable
Well it’s just to make the code snippet clear. As I said in the article, it could be replaced by a nice oneliner, but that’s not really the point of this article.
Good solution for 'If statements' issue!
What about this way?..
In this case this would be a good solution, but it gets really big when you have to check 10 options for example! The switch statement is also not available in Python for example.
The reason I use JSDoc in VS Code is for its autocomplete feature! But I do agree a function should be as self explanatory as possible.
Great write-up! 😄
you should probably point out that destructuring is ES6 syntax, which may not be compatible with every codebase
I'm not in the sector of informatics, and I know very little about informatics (something about HTML PHP CSS and very little about C), but I've always wondered why editors and IDEs for writing and compiling code don't separate automatically comments into a second file, in order to keep the final code clean, and to preserve the useful organization through the comments thus preserved; or maybe similar programs already exist and I don't know them.
Hey, Thanks for good solutions, thank you for sharing with us.
Great tips to improve our coding skills. :)
Thanks for sharing this.
Thank you for sharing. I don't use the comment for my code. If I read my code after 1 month, I sometimes don't understand what it does and have to spend times rereading the whole function or file
Oh okay, thanks! Did not know that! 👏