Live data from Hacker News

Cleaning bad code

gamasutra.com

1–10 of 22 posts

Re: Cleaning bad code

#4
I find the 7th point to be really great piece of advice, I simply love the subtle way, in which author is pointing out advantages of functional programming.

Re: Cleaning bad code

#5

>> On the other hand, the long-term effects of never cleaning your code can be devastating. Entropy is the code-killer. I love that quote. Can I have "Entropy is the code-killer" on a T-Shirt please.

Sounds like something out of Dune.

That's because it's based on something out of Dune: https://en.wikipedia.org/wiki/Bene_Gesserit#Litany_against_f...

Re: Cleaning bad code

#7
post #6

See also "Working Effectively with Legacy Code" book by Michael Feathers, IMHO _the_ book on this topic. http://my.safaribooksonline.com/book/software-engineering-an...

Very nice book. I use it often too. Highly recommend it.

Re: Cleaning bad code

#8
One thing that I often do when dealing with old code that's performing low-level operations and outputting some binary format is to md5 the output. After that, I can change the code and I often do this when people ask for less memory usage, better over-all performance, etc. Once the code has been changed, I can prove that it uses less memory/CPU/whatever, and verify the md5sum of my output against the original output so I know the code still outputs as intended.

Re: Cleaning bad code

#9
Most of this is really great advice. However I have run into horror stories that would shock you. For example:

1) Delete most of the comments 2) Stuff breaks. WTF? The code was processing its own comments!

Also I would add a third option here (the option we chose with LedgerSMB) which is:

"Localize the bad code, and replace with good code, a block at a time."

Some code can never be cleaned up effectively. In this case, you separate out, rewrite, and live with the fact that this will break some stuff while you get something that, on the whole, is more robust. Now in this approach the thing that is critical is you make as few changes to the legacy code base as you can. You also look for other layers at which you can implement things like new, needed security controls, or API's and you do everything you can to avoid putting new stuff through the legacy sections.

Re: Cleaning bad code

#10

Most of this is really great advice. However I have run into horror stories that would shock you. For example: 1) Delete most of the comments 2) Stuff breaks. WTF? The code was processing its own comments! Also I would add a third option here (the option we chose with LedgerSMB) which is: "Localize the bad code, and replace with good code, a block at a time." Some code can never be cleaned up effectively. In this cas…

> 1) Delete most of the comments 2) Stuff breaks. WTF? The code was processing its own comments!

My campfire story: change method name, stuff breaks. Method names had been parsed in a wrapper object (Python's __getattr__ being abused) so that calling 'setFoo()' triggered a 'Foo_changed' event. In fact, the method names constituted a mini-language with its own grammar and semantic. Apparently someone had gotten high on reflection.

Post reply on HN