![]() Modern text editors like Visual Studio Code and Sublime also have add-ons to format code with them. We just run them automatically whenever we like to format the code. Vertical formatting rules like variable and function grouping has to be looked at in code reviews since they can’t be fixed automatically.įor horizontal formatting, we can use tools like ESLint, JSLine or Prettier to format our code. This is at least true for horizontal formatting. Fortunately, we just have to run the code formatter of the team’s choice most of the time. If we work on a team, then it’s important to keep a set of rules for formatting code. However, for anything else, we should stick to the usual horizontal formatting rules. Photo by mkjr_ on Unsplash Breaking Indentationįor short functions, especially single line arrow functions, we can keep them in one line if they’re less than 120 characters long altogether. As we can see, spacing and indentation are quite important. We can easily discern the if block and for in the second example, while the first example is almost completely unreadable. It doesn’t make as much difference as between operators and variables or literals.įor arrow functions, we should have a space between the closing parentheses, the fat arrow, and the opening brace.įor example, a class with clean horizontal formatting may look something like the following: class Calculator We do not need a space between the method name and the opening parentheses. ![]() Also, the space between literals and operators are also good. Good places to put spaces are between variables and operators. There should be some spaces between some entities in a horizontal line of code. ![]() The point is that most people shouldn’t have to scroll horizontally to read our code. With screens being bigger than the olden days, we can have horizontal lines that are longer than before.Ĩ0 characters long were the standard in the olden days, but now 100 to 120 is also fine. In this article, we’ll look at how to format JavaScript code consistently so that they can be read easily by looking at horizontal formatting. Code that isn’t formatted properly takes more brainpower to interpret and understand by humans. Formatting code in an easy to read way is an important part of keeping code clean.
0 Comments
Leave a Reply. |