Live data from Hacker News

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

news.ycombinator.com

11–20 of 78 posts

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

#11
- Don’t write code until you need it. This results in less code, which is directly correlated with ease of change. - If you write lots of separated classes but then use them directly inside other classes, you’ve defeated the point. When you depend on something, pass it in instead of directly referencing, if possible. - keep methods less than 5 lines, classes less than 100 lines.

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

#13
Start with the absolute most simplistic structure, or no structure. Starting from "Hello world" is OK! (ie. starting in a new programming language)

Keep in mind you can always start over, especially if you componentize your work!

As you prototype, or just design by drawing on whiteboard, learn your domain and gain ideas what would improve your initial lack of design.

Iterate and refine when cost effective to do so, not sooner. If still on whiteboard, you could benefit from implementing rapid no-code solutions before diving into any code at all!

Resist committing to structure, unless your experience and insights into the domain require it and you see clear benefits from doing so. Deconstruct structure you find bring unnecessary complexity, as well as build on structure when doing so clearly brings leverage.

Care for your code.

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

#15
Extensible doesn’t mean the code itself gets changed often. It would help to read up on SOLID [1] design principles and books on software development. Actually implementing SOLID would come by reading good code, getting mentored and from experience.

[1]: https://en.m.wikipedia.org/wiki/SOLID

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

#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 the road it's easy to delete what you did, or code up some changes to it. I personally think the worst thing you can do is write code that is all things to all possible future features.

Post reply on HN