This menas ability to change specific behaviours wih very limited side effects.
Ask HN: How do you write code so it's easy to change?
51–60 of 78 posts
Re: Ask HN: How do you write code so it's easy to change?
#52Re: Ask HN: How do you write code so it's easy to change?
#53Re: Ask HN: How do you write code so it's easy to change?
#54Only 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?
#55Only 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.
Agreed - and note that this principle is in tension with the junior/intermediate (heck, even senior) urge to make things "easy to change" by just making everything abstract and configurable. ("See, now if we want to do it this other way in the future, we only have to flip this bit!") I still haven't found a pithy way to describe the limitations of this principle. Obviously, frameworks are helpful and also fail this p…
Re: Ask HN: How do you write code so it's easy to change?
#56If you can, use state machines, they provide a level of effective encapsulation otherwise hard to beat. This menas ability to change specific behaviours wih very limited side effects.
Sure you can draw a pretty state machine diagram, but is the code really easier to read and maintain? I'm not sure.
Re: Ask HN: How do you write code so it's easy to change?
#57Maybe a better question is this: how do you write code that does the most possible to prevent a maintainer from screwing it up? It sounds like the same thing, but acknowledging that mistakes happen no matter what and trying to focus on preventing them is something much more tractable that trying to come up with the perfect thing that can be understood right away. For instance, I used to be a fan of long variable name…
By writing tests, that's the only way.. I remember recently writing the test two weeks after I added the feature, the feature was already broken!
The difficulty is that maintainers are very creative in their way to break your code so it's difficult to write 'thorough' tests.
Re: Ask HN: How do you write code so it's easy to change?
#58Only 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?
#59Maybe a better question is this: how do you write code that does the most possible to prevent a maintainer from screwing it up? It sounds like the same thing, but acknowledging that mistakes happen no matter what and trying to focus on preventing them is something much more tractable that trying to come up with the perfect thing that can be understood right away. For instance, I used to be a fan of long variable name…
> Maybe a better question is this: how do you write code that does the most possible to prevent a maintainer from screwing it up? By writing tests, that's the only way.. I remember recently writing the test two weeks after I added the feature, the feature was already broken ! The difficulty is that maintainers are very creative in their way to break your code so it's difficult to write 'thorough' tests.