Live data from Hacker News

Ask HN: How do you write code so it's easy to change?

news.ycombinator.com

21–30 of 78 posts

Re: Ask HN: How do you write code so it's easy to change?

#21
Only write code that's absolutely needed for what you're building.

It's very tempting to make it "extensible" by overgeneralizing. What you get then is overly complex code, most of which is never used.

Cut out everything that isn't currently used, and you have a small system where every line pays for its keep.

Re: Ask HN: How do you write code so it's easy to change?

#23
Keep a document of your design decisions. It's very tempting to hack away to get things done, but human memory is short and feeble: you will forget why stuff was coded the way it was faster then you think.

I usually keep a Google Docs page open where, as I write the code, I update the documentation. It keeps things consistent and flexible, and much easier to go back and refactor.

Re: Ask HN: How do you write code so it's easy to change?

#24
post #16

I'd say write the least amount of code you can. While you're adding a feature it's always tempting to go down the "what if" route and make some kind of crazy interface that is extensible and able to accept plugins and all that. The problem is you're likely making it extensible in ways that turn out to be useless down the road. Just code the least amount you have to in order to achieve the current goal. That way down…

This is probably the most valuable advice. Less code has less bugs, is easier to read, test and document.

Re: Ask HN: How do you write code so it's easy to change?

#25
Keep code as simple as possible, shun abstractions until the refactor stage, adopt a language / toolchain that enforces a modular / functional discipline. EDIT: also, reading the comments here - absolutely shun DRY, be very explicit with code and don't try to couple anything until its absolutely clear that is the way to go. This in my opinion is the true mark of somebody experienced with codebases that grow unmanageable vs the less initiated -- the latter are always trying to reduce code by coupling things into abstractions, the former wait until it absolutely makes sense.

Re: Ask HN: How do you write code so it's easy to change?

#26
Writing code always involves starting with assumptions about how the code is going to be used. Extensible code is really just code that includes in its design assumptions that a part of it might need to be switched out or accommodate something more in the future.

Sometimes its easy to predict how your code may need to be extended if you give it a bit of thought. In those cases, it might make sense to allow for easy extensibility. This is pretty rare though. Most times, you can't really predict what will be required of the code in the future. In those cases I prefer to bake as few assumptions into the code as possible, so when someone else is extending it in the future, they spend as little time understanding your code.

Re: Ask HN: How do you write code so it's easy to change?

#27
post #23

Keep a document of your design decisions. It's very tempting to hack away to get things done, but human memory is short and feeble: you will forget why stuff was coded the way it was faster then you think. I usually keep a Google Docs page open where, as I write the code, I update the documentation. It keeps things consistent and flexible, and much easier to go back and refactor.

I think this is key when you are working with code you don't touch every day.

On code you touch every day one can easily make changes since everything is fresh in one's mind.

When you are working with a code base that you touch once a week or even for a few hours/minutes every day having notes about what you did, why and perceived next steps at the time is crucial and VERY useful.

Another strategy I found useful is to try and be consistent with your choices even if they are not fantastic.

For example: all your tables are named like tbl_entityNam. Even if prefixing every table with 'tbl' is a bad idea, the consistency in keeping it will be useful later when you need to work with that code.

Re: Ask HN: How do you write code so it's easy to change?

#28

Only write code that's absolutely needed for what you're building. It's very tempting to make it "extensible" by overgeneralizing. What you get then is overly complex code, most of which is never used. Cut out everything that isn't currently used, and you have a small system where every line pays for its keep.

This. The problem with unused extensibility is that it tends to get into the way of the extension that you will actually need... It's easy to extends something simple in any way. It's hard to refactor something that's complex.

Re: Ask HN: How do you write code so it's easy to change?

#29
Assume the code is going to be changed by first semester CS students. Dumb it down as much as possible. Don't do any clever tricks. Make the patches as small as possible so they can be (i) easily reviewed and (ii) allow easier bisecting.

Example: if a computation requires some intermediate value, create a new variable with a proper name and assign the intermediate value to it, instead of just inlining it in a bigger statement or assigning it to "res". The compiler knows how to deal with temporary variables like these in an optimal way.

Post reply on HN