8 ms·
After gaining a bit of experience programming, I've developed for myself the following rule: If I feel particularly smart and proud after writing a piece of co
by nottorp 9y ago
After gaining a bit of experience programming, I've developed for myself the following rule:
If I feel particularly smart and proud after writing a piece of code, I look at it again and remove some smartness in favour of readability.
Unless it's absolutely performance critical, in which case I try to comment the hell out of what's going on.
- taeric 9y agoFirst, I am not at all contradicting this. :) I just want to get that out of the way. I have found it pedagogical to read some more difficult programs, without trying to find ways to make them necessarily "read" better. In particular, generic and/or high performance versions of algorithms. To your point, this is precisely why I will refuse to generalize some parts of my code on the first pass. First, solve my problem specifically, then try and generalize. (This is not a waterfall style approach, but I do try and keep it somewhat phased.)
- nottorp 9y agoIMO don't generalize unless you find out later you need the same algorithm somewhere else. Otherwise, just leave the specific version in and stick some info in the comments (like a link to the high level description of the algorithm, if it's a common one that has a name, plus some pointers to your specifics).
- taeric 9y agoCompletely agreed. I do recommend playing with it (in like a repl or some other place) to see if you can possibly improve the algorithm, but have specific metrics for what an improvement would be. I'm reminded of a section in Programming Pearls where they explore several versions of a function, each time improving it. Too often I'm impatient and want the final solution first.