9 ms·
One of my most proud accomplishments was reducing the size of a driver from ~3000 lines of code to ~800. The file was 15 years old and had been modified by many
by cbsks 6y ago
One of my most proud accomplishments was reducing the size of a driver from ~3000 lines of code to ~800. The file was 15 years old and had been modified by many people since. There were conditionals that were impossible to hit, features that had been abandoned years ago, duplicate code, and lots of comments that didn’t match the code anymore. After my changes and a few new tests, the driver had full MCDC code coverage and the code actually matched the device specification!
- apozem 6y agoThat's awesome. Clearing out cruft without breaking anything or losing functionality is tricky, but man does it feel good.
- JoeAltmaier 6y agoThat is a win for everybody! My similar story was removing a 10,000 line module that built hundreds of different packets for sending over a mailbox to a wifi module. Each method was almost identical, with the exception of building a slightly different header. I replaced it with a template that given the structure, built a packet to send it. Less than 1 page of code.
- bostonvaulter2 6y agoWow, that almost makes it sound like the original author was getting paid per line.
- JoeAltmaier 6y agoMust have. It was a new version of an old driver. The old driver was 9 modules. The new one - 900. Every tiny little thing was another module. It conversed in 802.11 and ethernet etc, so of course it had three (3) copies of non-compatible packet layout declarations, each comprising dozens of modules. And on and on. It was like a computer science geek gone mad had figured, "I'll use every decomposition technique I ever heard of, and invent some more, so this is the most computer-sciencey source base in the world". Ultimately I rewrote it in 12 C++ base classes and a derived object for each radio card I had to work with.
- deleted 6y ago[deleted]
- Zelphyr 6y ago> There were conditionals that were impossible to hit The number of times I've seen: if (my_var == 'some value' || true == false) { ... } Why do people do this instead of commenting out the code?!
- ulucs 6y agoWhy even comment out? We have version control for a reason
- Zelphyr 6y agoI guess I've been assuming they did that because they wanted to temporarily disable whatever was within the conditional block but, yeah, you're right. Just delete it if you don't want that to run. Either way, that code should never be committed.
- slaymaker1907 6y agoVersion control is not immediately visible to future developers. You need to know that there is something to look for, particularly if it is a frequently changed file.
- gregmac 6y agoReally the only time a future developer will care about the old code is when there's a bug in the current code. (Or maybe an edge case like a request to "make it work like it used to in v1.0"). Looking at history via "blame" is useful to see why a bug was introduced (was it fixing another bug, and if so, is your fix going to break that bug again?), and how long it's existed for. Leaving old code commented out doesn't help either with of those things. Unless maybe it's accompanied by lots of comments and date stamps, in which case you've not only re-invented a very crappy, half-baked version control system, but also made the code hard to read and work with.
- slaymaker1907 6y agogit blame only tells you information about the last time a line was changed. If the code is commented out, I can quickly find the version said code was commented out. Git blame also doesn't help when the history gets truncated for performance reasons.
- hctaw 6y agoThe tricky side of this is languages that encourage terseness through "clever" syntax. But that's arguing syntax instead of principle, I think generally "less code" means "fewer expressions to evaluate"
- noveltyaccount 6y ago+1 to this. Code is fundamentally harder to read than write. If I use clever syntax, my plain-English comments make up the difference for characters saved :)
- jjice 6y agoThere is nothing more satisfying than refactoring code to something nice and condensed, but still very readable. A lot of my personal favorite experiences come from working with trees, since recursive code can be so lean and beautiful. I write a 5ish line function to generate a graphviz file to render by AST once and I still look back on that as beautiful code.
- Cthulhu_ 6y agoI'm sure I can reduce the code size of the codebase I inherited by half if I go ham on it, but it's 150K LOC of poorly written JS and PHP (like, really poorly, I could provide The Daily Wtf with a dozen articles I'm sure) and I'd rather spend my time on the rebuild. Of course, my manager is telling me to keep adding features to the old codebase. Sigh.