The Magic of Coding Starts from Inside the Box

21 Sep 2021

Introduction

Coding standards are rules regarding the organization of code. Recently, I was introduced to ESLint, a tool that can be used to find errors in JavaScript code, as well as point out violations of coding standards. ESLint only requires Git (a source control system), Node.js (a JavaScript runtime) and npm (Node package manager). So, ESLint can be run from the command line or be enabled in an integrated development environment such as IntelliJ IDEA (the IDE of choice in ICS 314). I found ESLint and its suggestions very useful in both improving my code and observing JavaScript coding standards. Coding standards, though somewhat tedious if a person is a sloppy coder, are essential for the following reasons.

Coding standards help prevent mistakes

Firstly, coding standards are useful in helping to prevent mistakes. A sloppy coder might make mistakes simply because they are not careful when typing out their code. For example, they might forget a curly brace or they might declare a variable that is never used. Then, when they try to run their code, they wonder why it won’t compile or doesn’t run the way they expected. Having a standard appearance makes it easier to pick out errors. It gives guidelines as we type along so that we have a set image in our mind that we can compare our current code against. In addition, some coding standards help us remember unique features of JavaScript compared to other programming languages. For example, in C++, an underbar ( _ ) is used for private variables. JavaScript coding standards indicate we should avoid the use of underbar as the first character of a name because it does not make the name private. In fact, JavaScript does not have true private variables. As another example, we avoid starting names with the $ symbol because the $ symbol is used by JQuery, a popular JavaScript library to select an HTML element by its id (i.e. $() is commonly substituted for document.getElementById()).

Coding standards improve group collaboration

Secondly, coding standards allow for collaboration on a larger scale. If other programmers have an easier time reading your code, it will be easier for them to make suggestions to improve your code or directly modify the code themselves. A seasoned programmer might be able to pick apart messy code, but may waste time trying to test out the code line by line to see if they are understanding the code correctly. It can also be very tedious to fix bad code that was written without following any coding standards. The code may have to be entirely re-written and if the functions or methods used don’t make any sense, it can be difficult to know how the original writer intended for the program to run. Having sloppy code may not matter if you are the only one viewing/using your program. But in the industry, there will always be situations where you are working with other programmers on projects. Strong adherence to coding standards can help ensure that you get proper credit for your hard work and helps you maintain a good reputation among your colleagues.

But what about creativity?

Critics of some of the more semantic parts of JavaScript coding standards (e.g. requiring spaces around operators, using 2 spaces for indenting blocks of code, avoid writing lines of code longer than 80 characters) might say that the standards are too rigid. These critics might suggest that such rigid standards prevent programmers from being as creative with their code. If we are too picky about how the code appears, what if we overlook a solution? Why should we dictate how others like to program if their program works? I agree that it is true that coding standards take extra time to implement. But I believe that spending that extra time looking at the code is more likely to inspire new ideas. If the code is easier to read, there isn’t any clutter that distracts from the purpose of the program. So, in the long run, following coding standards makes us more efficient programmers.

Conclusion

Coding standards help to standardize the appearance of code, as well as improve the functionality of the code. Coding standards help make it easier for groups of programmers to collaborate on the same projects. Having a standard format is especially important in computer programming because there are so many ways to solve a programming problem. Like using good grammar, adhering to coding standards helps give context to the code and helps one section of the code flow smoothly to the next. Coding standards can encourage creativity because the code is easier to understand. However, coding standards alone cannot make up for a lack of content or a shallow understanding of the programming language. Even if a program is non-functional, ESLint may say that there are no violations of the coding standards. So, practicing writing out a lot of code and understanding what was written, are key to being a successful programmer.