I’ve always really wondered what people thought when they hear the term “clean code”. I often hear people say it is commenting your code, while others say object-oriented programming, and some just say “no spaghetti code”. While I agree with all those, I tend to lean towards object-oriented programming the most. In fact, I don’t even believe commenting your code should necessarily be a function of clean and legible code.
Lately, my team and I have been tackling a goal I set to clean up our code, to make it faster and more reliable. Prior to starting this project, we had just over 200k lines of code. Had a lot of duplication of functions, a lot of code that could be simplified by turning into more generic functions and a bunch of functions that existed but never used. 200k lines of code is a lot, but we are also building a very complex system and we do use some open source libraries like Mootools that add to the total. However, given that JavaScript has never been particularly nice with OOP, it is difficult to make nice clean extensible code and save on lines of code. We’ve built hacks into our JavaScript to mimic OOP to our best abilities, but it still isn’t as perfect as you’d find it in languages like Java.
As you can probably tell, my approach is to cut down on code by making functions and existing code more modular and used by more sources, as opposed to living in the cache as a one time use function. Granted that the function is clearly written, declaring variables in plain text and using variable names that make sense for what they do, commenting becomes much less relevant, especially when the functions themselves are less than 30-50 lines of code (including line breaks). Commenting is something I felt was always added to help another developer understand one developers sloppy writing. Yes, creating code is like writing. If you cannot properly outline your introduction (declarations), your content (the main code) and the conclusion (clean up and return statements), then no one will understand what you are attempting to carry out.