“Small” code or “Less” code?
February 21, 2007
A lot has been written about how programmers should aim for writting “small” code (actually I was just reading Jeff’s post). But programmers usually mis-interpret the term “small”. Small code does not mean that you start reducing lines by trying to push everything in one line of code and eventually make it a meaningless gibberish that not even aliens can understand. What those people really mean is that if you can get the same amount of things done with “less” code then do that. Make it small by thinking of a smarter way of doing that thing. Or using a better data structure. Or removing useless variables. Or removing repeated code.
Reducing code by doing something like the following is not always of much help. At times, its worse actually.
var c = F1(F2(F3((a==b) ? F4() : d)))
One of the most important use of smaller code is to make it easy to manage. I am not making it easy to manage by writing something like the above code. I am making it even harder to understand and hence even more difficult to manage later on.
Write ”less” code. Keep it clean and easy to understand. Write comments. Use meaningful variable names. Have fun!