What is clean code?

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.

Today, we are down to about 178k lines of code.  We did not remove any functionality, we improved what we had and made it more legible.

    1248 text files.
    1227 unique files.
    6226 files ignored.

T=33.0 s (36.8 files/s, 7438.0 lines/s)
-----------------------------------------------------------------
Language         files        blank          comment       code
-----------------------------------------------------------------
JavaScript       460          19763          26134         122762
PHP              486           6131          10442          36176
CSS               88           1944            937          11144
HTML              38            650           1243           5432
XML              132              0              4           1213
ASP                7            165            409            889
DOS Batch          1              1              0              8
Bourne Shell       1              3              3              2
------------------------------------------------------------------
SUM:            1213          28657          39172         177626
------------------------------------------------------------------

As we see here, our JavaScript needs a lot of improvement and is what we are focusing on during this exercise.  The bulk of the 22k lines of code we’ve removed were in JavaScript alone.  I’ll be posting an update on the number of lines as we complete our refactoring.  Like any essay, there is no sense in adding B.S., because it really doesn’t help solidify your argument and only adds clutter.

Happy clean coding folks!