Live data from Hacker News

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

news.ycombinator.com

61–70 of 78 posts

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

#62

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.

In other words, eXtreme Programming.

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

#63
Here is my personal checklist:

* If in a loosely typed language go way out of your way to ensure conventions are in place to identify things by data type. TypeScript has completely changed how I look at JavaScript particularly its use of interfaces for defining objects and data structures.

* Limit your use of dependencies as much as possible. In my current personal project I am down to 1 dependency. Dependencies will weigh a project down and prevent nimble shifts of direction. This also includes frameworks and tools.

* Write good documentation and keep it up to date. Automate the authoring of documentation where you can so that it stays up to date with less manual intervention.

* Isolate concerns as much as possible so that a feature can be deleted and replaced with as little effort as possible.

* Keep things simple. This requires lots of extra effort both in planning and refactoring. When there are fewer pieces and everything is as uniform or consistent change requires far less effort and leaves far less dead code in its place.

* Don't abstract things for convenience or easiness. Abstractions are incredibly helpful when they result in simplicity (fewer pieces and fewer ways of doing things). Absolutely don't use abstractions as a means of being clever or for vanity reasons. Then you just have extra code and the larger a project gets the harder it is test and change.

* When other people want to impose their opinions on you push back until they come back with test automation and/or strong provable evidence of a better way of doing things. If they still try to impose their opinions on you, and this will happen, kindly tell them to go fuck themselves. So much of the stupidity in programming comes from baseless irrational subjectivity.

* Test automation. Its not about how you test, but about what you test. There is a lot of nonsense out there about how to test. In my current personal project I have automation in place for code validation, compile checks, command tests in the terminal, service tests. Soon, as I figure it out, I will be adding test automation for user interaction and user data storage. However you are able to execute all the kinds of tests you need just do it. The goal is to promise your software can deliver a feature and prove it with test automation and then add more tests later to cover for edge cases you did not consider.

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

#64
Here's a couple of mantras to write clean code:

- Code is written once and read at least once, have empathy for the readers...

- If code cannot be understood, it cannot be changed safely...

- Less dependencies on other code, makes code easy to understand...

- The smaller the context is, the easier it is to understand the code...

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

#65
Most of the responses focus on how you meet the current requirements. However, the most important thing to know is not actually about current requirements, but future ones.

Sure you can assume that all the requirements will change, but then you’ll need to make a system so flexible it becomes a nightmare to do anything (just look at JIRA or other software like it, with 100 configuration options for every piece). These systems require vast resources to maintain because there is so much code to make it flexible.

You can also assume the requirements won’t change at all, tightly following the spec and optimizing all the edge cases so you have a pristine black box no one can touch.

Finding the happy medium involves knowing which assumptions are likely to change, and which aren’t. Usually, languages won’t change often, so you can rely on most of those features. Certain well-maintained libraries and APIs won’t change much, so those can usually be baked-in too. The business logic around things like pricing, users, analytics, data visualization, and data processing are likely to have changes. My favorite one for web apps with databases is search. The filters very often change and grow, so getting a highly flexible search working is usually a big win for not too much effort.

Note, I’ve never said always. If you can get knowledge about the business needs you’re supporting, you’ll have a much better idea of which things need flexibility, and which things don’t. The longer the roadmap on a project, the more wins you can get when architecting systems too, as you can set yourself up for easier implementation of future features.

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

#66
Keep code Simple and Unified.

Keep data and logic separate, data-driven development.

Adhere to the "standardized data interface specification" and use pipeline-functions to manipulate data from the initial state to the final state.

Pipeline-functions conforming to standardized data interfaces are easy to change, replace, and insert extensions.

In the web model, It can be easily replaced as long as the components that conform to the http data specification. For example, Clojure web application model:

- product standard (data interface): the req-map and resp-map of the ring

- warehouse: the ring

- workshop: the pipe functions on both sides of the C/S, and Raw materials (hash-map) are transferred to each other through warehouses through interactions.

https://github.com/linpengcheng/PurefunctionPipelineDataflow

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

#67
post #10

You're going to get a bunch of extreme platitudes in response to this question. Like "DRY", "YAGNI", SOLID, short vs. long functions, how to test, minimizing vs. maximizing reuse, static vs. dynamic typing, cyclomatic complexity, interfaces, functional programming, immutability, etc. You should definitely be familiar with these principles and their tradeoffs. My advice though would be to take them all as suggestions,…

I agree with this, but I'd like to add one more thing. No matter what you do, if you find that your code is hard to change, then change it so that it is less hard. Next time you visit it, change it again. Keep doing this until it is very easy to change the code. Write enough test support in your code that you can do this kind of work safely.

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

#69

Less code, loose coupling. Do one thing and do it well. Keep the external API simple. It's always easier to change a small thing instead of a large thing. So make a small thing, and only make it a big thing if you absolutely have to. I think my favorite recent HN example of this was that article where Discord rewrote their "unread message count" service in Rust. They were able to make a huge change to a critical sect…

> Less code, loose coupling. Do one thing and do it well. Keep the external API simple.

> It's always easier to change a small thing instead of a large thing. So make a small thing, and only make it a big thing if you absolutely have to.

this is along the lines of what i was going to suggest. i prefer to code everything such that it does just one thing that is appropriately at the level or context it sits at.

a function should just do one thing and have reliable outputs based upon inputs. a class should represent one thing. class methods should do one action. modules should collect functionality that does a thing. processes and servers should do a thing. and so one. if you build your system out of things only doing the thing they say they do, then it becomes clear when they're being changed in incorrect ways.

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

#70
My usual go to is every time I write an interface, I think to myself: "How easy would it be to explain this module to another developer?"

For every additional change, I look at the module as a whole again, and keep thinking "If we got a new hire, how easy would be for the person to understand this in the bigger picture?"

Understanding is all about mindset. If part of the code starts to do too much, then other developers are likely to make incorrect assumptions of the logic behind that piece of work. The small mistakes slowly compounds over time, and if not taken care of, the whole project becomes un-maintainable.

Post reply on HN