Everyone has a different code style for the format of the code, and that’s fine. But for a team that can best unified code style, in the same project, if filled with different code style, compared to read is not so comfortable, such as where to place the brackets, the indentation a few characters, how naming constants, variables and methods, etc., the whole team should follow the same set of rules, You can even write these rules into the IDE’s code format to help with IDE hints.
The style mentioned above varies, but it does not affect the reader’s reading. But some code styles are hard to read, while others are pleasant to read. So what format is enjoyable to read? Simply summarize a few points and see if they help you read. If so, please do so.
- Precede each method with a blank line for emphasis
- Local variables live as close to where they are used as possible
- Class variables should be declared at the top of the class
- If a function calls another function, the caller should be placed above the called one, so that there is a natural reading order
- Groups of functions that perform similar operations should be placed together
- Each line of code should not be too long, at least not beyond the width of the screen to read
- Indentation of code, without explaining it, is a pain to read code that is not properly indent
- If the body of the loop statement is empty, make sure the empty body is indent as well, otherwise it is easy to mistake the following sentence for the body of the loop
- , etc.