Originally published on Caffeina
When a group of people starts working on a very large and important project, it is inevitable to encounter different methods of thinking and writing the code. The risk of having the code difficult to maintain is very high. We decided to study (YES, you need to study to improve processes and write clean code) and understand what was the best approach to be used internally to leave the freedom to interpret the code but maintain a clean and maintainable code base.
Clean code is a consistent style shared by the programming group that simplifies writing, reading, and code maintenance. It is not enough that the code works for accepting a pull request (obviously with the necessary exceptions) but you have to ask yourself before doing the pull request:
have I removed all the ?
Will anyone else be able to understand this code in six months?
Is it necessary a refactoring that I can not do for reasons of time / information and I have to declare it?
In simpler terms, write code that you would be proud to take home and show your mother.
How many times have you really commented on your code? Or how many times have you commented on it and then changed it without even remembering to change the comment? the solution is simple and immediate. Write self-commenting code. It is fundamental to give the right name to things.
Boolean variables, or functions that return a boolean value, should start with “is,” “has” or “should”
Functions must be as small as possible. If your function is getting too long and complex or you can not give a unique name because it does too many things, it is 99% decomposable in sub functions that perform a specific operation. into individual functions also increases the ease of testing them and also reduces the possibility of errors.
Functions should be named for what they do, not how they do it.
Event function should have the name of the event or contain it if you have more than one event function (ex. onClick)
Example of clean and bad naming code
Another way to automatically comment on the ReactJS code is to declare the types of props used in order to make anyone who uses our component understand which props are necessary to operate and all those used. In addition, using the props-types, warnings will be printed when props don’t match with the declaration,this will help us to understand what we have wrong
Example of how use propTypes