TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

69 点作者 mtm7大约 5 年前
I work as a web developer in the startup industry. I've heard many times that code should be extensible – that it should be easy for engineers to make changes to the system in the future, especially as a product finds market fit. What tips would you give other engineers on writing easy-to-change code?

60 条评论

cle大约 5 年前
You&#x27;re going to get a bunch of extreme platitudes in response to this question.<p>Like &quot;DRY&quot;, &quot;YAGNI&quot;, 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.<p>My advice though would be to take them all as suggestions, not absolutes, and to look at your specific requirements of your project, what you know about it and your customers (how it&#x27;s likely to evolve over time, how it has already evolved, how your company will evolve, who the engineers are, etc.), and then find &amp; adjust the principles that apply. There are no silver bullets here, and which techniques are successful depends very much on the context in which they&#x27;re used. Work backwards from your team, your situation, and your customers.
评论 #22601545 未加载
BurningFrog大约 5 年前
Only write code that&#x27;s absolutely needed for what you&#x27;re building.<p>It&#x27;s very tempting to make it &quot;extensible&quot; by overgeneralizing. What you get then is overly complex code, most of which is never used.<p>Cut out everything that isn&#x27;t currently used, and you have a small system where every line pays for its keep.
评论 #22594523 未加载
评论 #22597838 未加载
评论 #22594629 未加载
评论 #22630503 未加载
评论 #22599394 未加载
jrockway大约 5 年前
Less code, loose coupling. Do one thing and do it well. Keep the external API simple.<p>It&#x27;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.<p>I think my favorite recent HN example of this was that article where Discord rewrote their &quot;unread message count&quot; service in Rust. They were able to make a huge change to a critical section slowly and incrementally because it was loosely coupled. The programming language they chose didn&#x27;t matter, because it could just be changed. The implementation didn&#x27;t matter because the consuming code didn&#x27;t know about any implementation details. The change happened and all the other teams had to care about was that there weren&#x27;t weird latency spikes anymore.
评论 #22602595 未加载
nybblesio大约 5 年前
Separate <i>policy</i> from <i>mechanism</i>. [1] I usually reframe this as separating the <i>what</i> from the <i>how</i>. Back in the 80s and early 90s people would commonly call this &quot;data driven design&quot; (this is distinct from &quot;data-oriented design&quot; which lives more in the <i>how</i>).<p>One area you can quickly see this in action is with user interfaces. So many modern applications construct their UI tediously by coding each component and all of the aggregations by hand. If you take a step back, it&#x27;s usually very easy to see that the configuration of the UI is just data. Why not just build one layout &quot;engine&quot; and feed it data to generate your UI? The engine part can use whatever hotness you like, but you only have to write that code once. The configuration of the UI (the policy) is what changes frequently (the business requirements); not the mechanism underneath. (This doesn&#x27;t mean you won&#x27;t have to change the mechanism, you will. It just requires change at a much slower pace and the changes are bound within the context of the mechanism itself.)<p>This approach separates layers of a system which have different change vectors and velocities. The mechanism tends to have a much lower rate of change and it&#x27;s where we programmers like to live. Build tools for &quot;the business&quot; -- whatever that means in your context -- so they can maintain as much of the policy as possible.<p>If you think through designs like this; however, you will realize that our modern fascination with massive &quot;scaling&quot; of teams is no longer attractive. This may explain why it has fallen out of favor.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Separation_of_mechanism_and_policy" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Separation_of_mechanism_and_po...</a>
contingencies大约 5 年前
Documentation (document the &quot;why&quot;). Loose coupling (this goes for databases, programming libraries, languages and paradigms, and operating systems as well as function interfaces). Design data structures first. Make everything a program. Make every program a filter.<p><i>Those who don&#x27;t understand Unix are condemned to reinvent it, poorly</i>. - Henry Spencer<p>Many architecture tips here: <a href="https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;taoup" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;globalcitizen&#x2F;taoup</a>
munificent大约 5 年前
The answer to your question is essentially the entire field of software architecture and to some degree even software engineering itself.<p>Thus there aren&#x27;t many simple always-applicable tips. Every tip requires experience to know when you apply it. Any tip that doesn&#x27;t require that experience ends up not accomplishing much.<p>Keeping things simple is good. But sometimes you do need some abstractions, scaffolding, and structure that sacrifices simplicity but enables the code to grow.<p>Tests are good. But sometimes code can be so heavily tested that changing the tests themselves is a large maintainence burden when the requirements change.<p>Abstraction and encapsulation are good. But both add complexity and raise the amount of code someone has to load into their head to understand what&#x27;s actually happening.<p>Documentation is good. But writing good docs itself an artform, and docs that aren&#x27;t maintained well can cause more harm than good.<p>The best advice I can give is to not seek out &quot;tips&quot;. Just work with the best engineers you can find, write code, reflect on it, and trust that your experience and intuition will grow over time. There is no shortcut to mastery.
ezekg大约 5 年前
Be as explicit as possible. Use guard clauses. Don&#x27;t couple things for the sake of less code. Very, very often, repeating yourself is OK.
btilly大约 5 年前
Factor out logical units early and often.<p>DO NOT try to generalize and abstract until the third time that you are writing something. (Having written it for other companies counts.) Until the third time your guesses about the right generalizations and abstractions generally work out badly.<p>Focus on simple over clever. I cannot count how many abstract systems that I have seen which were designed to achieve some general purpose reusability. They almost never actually achieve the asked for dream and almost always become a barrier to comprehension by other developers.<p>FOLLOW ALL STANDARDS IN YOUR ORGANIZATION. Code review. Unit tests. Formatting. Naming conventions. Doing what you think is the perfect thing is almost always worse than staying consistent with other developers.
gregkerzhner大约 5 年前
Lots of comments here on keeping this as simple as possible and not over-architecting. This is a good value, but its equally important to add architecture and abstractions before its too late. Software projects become tangled spaghetti messes slowly, over time, by adding &quot;just one more thing&quot; to existing modules and functions until they are large and impossible to fully understand. My simple rule of thumb is this - no file should be longer than 300 lines of code and no function should be longer than 15 to 20 lines of code. If your file or function is longer than these guidelines, its trying to do too many things and its time to break it down.
ssalka大约 5 年前
I used to focus on making my code easy go extend. I still enjoy that style in personal projects, but on large teams, I find it is better to make your code easy to delete.
docteurklein大约 5 年前
Write code for use not for reuse. Write code that is easy to remove, not code that is easy to change.
dyingkneepad大约 5 年前
Assume the code is going to be changed by first semester CS students. Dumb it down as much as possible. Don&#x27;t do any clever tricks. Make the patches as small as possible so they can be (i) easily reviewed and (ii) allow easier bisecting.<p>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 &quot;res&quot;. The compiler knows how to deal with temporary variables like these in an optimal way.
评论 #22594590 未加载
Olreich大约 5 年前
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.<p>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.<p>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.<p>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.<p>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.
slifin大约 5 年前
Make it easy to delete conceptually<p>It should be removable in a way that does not break other systems and it should be easy to identify when &quot;it&quot; ends and when something else begins
simion314大约 5 年前
My strategy is make small functions that do one thing then combine this small functions to create larger ones. When a new requirement is added to change the large functions you probably have to change just one or a few of the small functions but many of the smaller functions are unchanged. Also when this new requirements are added you can reuse the smaller functions, most issues I had to fix is understanding code that evolved over time and ended up to be a giant function that does too many things , with many branches.<p>Also make the code easy to read and understand, have good naming convention for the small functions and variables, add comments if you have to do something unusual (like workarounds for weird quirks or bugs) so it makes sense 3 months later when you read the code again and you forgot most of it.
Rainymood大约 5 年前
Whole books have been written about this. I use the following rules from [1] myself. I like them because they definitely put some constraints on my coding which I like. It forces me to think beforehand and gives me (a junior developer) some guidance which I desperately need:<p>* Write functions&#x2F;units that are &lt;= 15 LoC.<p>* Create units with &lt;4 branching points<p>* Write functions&#x2F;units with &lt;5 parameters<p>These guidelines force you to write maintainable code. Always open for discussion. Note that these lessons are lifted straight from [1].<p>[1] <a href="https:&#x2F;&#x2F;www.softwareimprovementgroup.com&#x2F;resources&#x2F;ebook-building-maintainable-software&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.softwareimprovementgroup.com&#x2F;resources&#x2F;ebook-bui...</a>
grecy大约 5 年前
I&#x27;d say write the least amount of code you can.<p>While you&#x27;re adding a feature it&#x27;s always tempting to go down the &quot;what if&quot; route and make some kind of crazy interface that is extensible and able to accept plugins and all that. The problem is you&#x27;re likely making it extensible in ways that turn out to be useless down the road.<p>Just code the least amount you have to in order to achieve the current goal. That way down the road it&#x27;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.
评论 #22594456 未加载
joddystreet大约 5 年前
This is how we do the API -<p>- Database models should contain all the possible mechanisms&#x2F;methods for data I&#x2F;O<p>- Data Transformation utilities&#x2F; helper functions that do one job well, that is - sort, filter, validation, whatever -- -- If an API is using (more on API below), utils become immutable code<p>- Finally the API level -- -- treat the API code as immutable, once shipped -- -- The code should look like a pipeline of functions -- -- -- eg: serialize(...) validate(...) filter(...) db(...) -- -- -- need not be chained -- -- new API version, new router, new routing logic<p>- Review API with an intent to extract out any possible util methods. final API methods should look like a series of data transformation functions<p>- Overtime the utils would become extremely messy, but the APIs would be very clean<p>------------------------------------------------<p>The layer running business logic would be the most interacted with layer, if you can keep it clean, then you can keep frustration low in your fellow devs. A lot of people can work on the API level, irrespective of their experience.<p>Utils and Models should be managed by more experienced devs. Rate of change in Utils and Models is quite low as compared to the rate of change in the API levels.<p>------------------------------------------------<p>Not sure, I guess I am writing something very trivial and obvious.
jmilloy大约 5 年前
In codebases I&#x27;ve worked on, the biggest culprits that make maintenance and modification difficult are<p>- functions that do several different things<p>- tight coupling<p>- implicit behavior<p>- too much indirection<p>- too many thin custom wrappers<p>Often several of these come together when an inexperienced developer factors out everything they can (over-eager DRY) but still tries to keep a few highly specific things &quot;easy&quot;. One thing I see often is modules or classes that wrap a standard library, <i>adding</i> code essentially <i>removing</i> features in the interest of &quot;ease-of-use&quot;.<p>I think you can write better code if you write code in multiple passes: only write what you need <i>right now</i>, and use the code as-is between each pass. With that approach, you&#x27;ll gain some real intuition about what is easy to read and modify, which is much more helpful than someone&#x27;s list of tips.<p>That said, I&#x27;ve found that writing simple explicit functions that take arguments and return a result can get you pretty far. Same for class constructors and methods, API endpoints, etc. Try to avoid writing &quot;convenience&quot; functions or classes when just using the library directly with some arguments would suffice. In other words, boilerplate probably isn&#x27;t as bad as you think, and the indirection&#x2F;implicit behavior&#x2F;tight coupling is often worse than the boilerplate.
austincheney大约 5 年前
Here is my personal checklist:<p>* 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.<p>* 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.<p>* 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.<p>* Isolate concerns as much as possible so that a feature can be deleted and replaced with as little effort as possible.<p>* 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.<p>* Don&#x27;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&#x27;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.<p>* When other people want to impose their opinions on you push back until they come back with test automation and&#x2F;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.<p>* 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.
mzanchi大约 5 年前
Keep a document of your design decisions. It&#x27;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.<p>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.
评论 #22594514 未加载
thdrdt大约 5 年前
Easy to change code is code you understand a year later.<p>This includes:<p>Easy to understand variable and function names. IDEs make autocompletion possible so don&#x27;t fear long names. MyEasyToUnderstandFunction() is better than MyETUF().<p>Function names should describe what they do. CreateOrder() is better than NewOrder().<p>Make sure you don&#x27;t use global&#x2F;public state in any object or component. Public state is one of the biggest sources of failure and bugs. If you can, don&#x27;t use it.<p>KISS: Keep It Simple Superstar!<p>But maybe the most important (for me): use separation of concern and context. This always helped me a lot. For example: if an email must be sent when a new order is created you can put the email code directly after the order creation code. This will ofcourse work and might even be valid code. But the component that creates the order should not be bothered with how emails must be sent. If you separate this, it will help you a lot to create maintainable code.
DanielBMarkham大约 5 年前
Maybe a better question is this: how do you write code that does the most possible to prevent a maintainer from screwing it up?<p>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.<p>For instance, I used to be a fan of long variable names that say exactly what they do. I still am, but only in certain situations. In fact, I&#x27;d argue that using very descriptive variable names in the wrong situations can actually make the code less maintainable, not more.<p>This takes us naturally to a discussion about what works, why it works, and under which circumstances it might not work (or actually hurt), which is probably a much more productive conversation than something along the lines of &quot;how do you guys think the best way to code is?&quot;
评论 #22597778 未加载
Jugurtha大约 5 年前
The first thing we did when we started building our ml platform was to go with a plugin architecture. All applications are simply plugins. Data connectors are plugins.<p>We do consulting work and have to build custom solutions for our clients. Once they were satisfied with the models, they&#x27;d ask for an application to use these models: user management, data uploads, etc.<p>We used to build a custom solution for each client. It was very hard to change these solutions when client&#x27;s demands changed, and it was almost impossible to reuse that solution with another client.<p>We took advantage of a decrease in activity to build a platform where things would be plugins. The time series forecasting application is a plugin, with endpoints you can tap into with authorization tokens.<p>The recommender system is a plugin, the notebook application is a plugin, etc.<p>Now our notebooks are &quot;Published&quot; in one click, which generates an AppBook a business person can interact with by changing the parameters. The runs are tracked (params, metrics, and models) in case domain experts tweak parameters that result in a better model than we have. The models can be deployed to endpoints you can invoke with generated tokens.<p>The major benefit is that when a new member joined the team, they had to understand the whole code base to be able to contribute or add something. Now? They don&#x27;t have to understand anything, only the application they&#x27;re building. If they want to render it on the sidebar, they just have to add a `sidebar.html` with the proper icon and route. If they want to show it in our appstore, they just have to add a file with the proper icon, and our platform handles the rest. It will discover the application, load it, etc.<p>An administrator can even load an application as a zip file. The platform will consume it, install its dependencies, load it, and activate it.<p>We want the product to be easy to add to and modify its behavior with config, instead of changing the code and we work to do just that.
ben509大约 5 年前
Beyond having good testing, don&#x27;t try to write it to be easy to change, write it to be good code.<p>You should try to express problems elegantly, and that will help make some changes easy. That&#x27;s nice when that happens, and it does happen quite a bit. But many changes will still not be straightforward.<p>Generally, write the code to do what it&#x27;s meant to do, and when it needs to change, rip it out and rewrite it.<p>It&#x27;s shockingly easy to write something that&#x27;s concrete and later realize &quot;I need two of these&quot; and then quickly factor out the common functionality.<p>Especially, don&#x27;t kludge things. The problem with &quot;easy to change&quot; is it means &quot;easy to kludge just one more feature on here.&quot; That won&#x27;t remain easy to change. Refactor aggressively and your code still won&#x27;t be easy to change, but it won&#x27;t become a kludgey mess that&#x27;s hard to change, either.
_y5hn大约 5 年前
Start with the absolute most simplistic structure, or no structure. Starting from &quot;Hello world&quot; is OK! (ie. starting in a new programming language)<p>Keep in mind you can always start over, especially if you componentize your work!<p>As you prototype, or just design by drawing on whiteboard, learn your domain and gain ideas what would improve your initial lack of design.<p>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!<p>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.<p>Care for your code.
coziestSoup大约 5 年前
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.<p>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&#x27;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.
matt_s大约 5 年前
Simple things go a long way. An easy one is abstraction.<p>If you are writing an interface to another system of any kind (DB, API, etc.), abstract that interface one layer from other parts of your app. Then when that other thing changes you are less impacted.<p>An example: if your organization has multiple apps, it might sound easy to just let app #2 have direct DB access to app #1&#x27;s DB. Take a little time and build a simple API in app #1 to get at the data, then when you change column names, etc. as long as your API layer doesn&#x27;t change app #2 won&#x27;t require changes. You don&#x27;t have to build a fully RESTful API to all-the-things if it is just one table. Iterate.
Nican大约 5 年前
My usual go to is every time I write an interface, I think to myself: &quot;How easy would it be to explain this module to another developer?&quot;<p>For every additional change, I look at the module as a whole again, and keep thinking &quot;If we got a new hire, how easy would be for the person to understand this in the bigger picture?&quot;<p>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.
sush1612大约 5 年前
Write however you want to write, choose whatever best practices available, any design pattern, design principal. Just add lots of comments, as many comments as possible which talks about what each line is suppose to do. If you are using a pattern, write at the start that this is x pattern used for this scenario. if you are branching the code, which scenario each branch will take care of.<p>At the end, if you comments and logs are complete and self explanatory, any one can come in and change the code or debug the code.
mattsfrey大约 5 年前
Keep code as simple as possible, shun abstractions until the refactor stage, adopt a language &#x2F; toolchain that enforces a modular &#x2F; functional discipline. EDIT: also, reading the comments here - absolutely shun DRY, be very explicit with code and don&#x27;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.
EastLondonCoder大约 5 年前
The biggest bang for the buck for me has been using functional techniques where immutability is perhaps the most important thing. A good way to get started is to make a project with react&#x2F;redux.
bjourne大约 5 年前
Keep it short and succinct.
jressey大约 5 年前
Think of your tests as a consumer of the software you create. You will know it&#x27;s easy to test if your component does not take much setup. If you have to create all kinds of different objects to get a test to pass, you&#x27;ve built a coupled component.<p>Said another way: Making an easy to understand interface to your software should be your top priority. Make the interfaces within your public interface as easy to understand, etc., all the way down.<p>Easy to test: Easy to understand. That makes it easy to change.
newscracker大约 5 年前
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.<p>[1]: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;SOLID" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;SOLID</a>
jcutrell大约 5 年前
- 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.
HackOfAllTrades大约 5 年前
Just one tip: Comment your Intent, not the code.<p>Anyone can read the code and see what it does.<p>No one can read your mind to figure out what you intended it to do.
boltzmannbrain大约 5 年前
&quot;Write code that is easy to delete&quot;<p><a href="https:&#x2F;&#x2F;programmingisterrible.com&#x2F;post&#x2F;139222674273&#x2F;write-code-that-is-easy-to-delete-not-easy-to" rel="nofollow">https:&#x2F;&#x2F;programmingisterrible.com&#x2F;post&#x2F;139222674273&#x2F;write-co...</a>
gitgud大约 5 年前
Here&#x27;s a couple of mantras to write clean code:<p>- Code is written once and read <i>at least</i> once, have empathy for the readers...<p>- If code cannot be understood, it cannot be changed safely...<p>- Less dependencies on other code, makes code easy to understand...<p>- The smaller the <i>context</i> is, the easier it is to understand the code...
jgwil2大约 5 年前
Keep files small and use the file system to your advantage: it&#x27;s much easier to organize information in a tree than in a string. Keep code for components together (e.g. no &quot;controllers&quot; folder). Write twice as much documentation as you think you need.
xupybd大约 5 年前
Keep it simple and don&#x27;t allow any technical debt to build up. I&#x27;ve worked in run down legacy systems and in clean ones. The run down ones are impossible to change without breaking. The clean ones are just as easy as a green fields project.
ativzzz大约 5 年前
If this were an easy answer, software devs would not be paid as much. Read some books about the topic, then write a lot of code, then change a lot of that code, and learn from your experiences regarding what was easier or harder to change.
RedBeetDeadpool大约 5 年前
To add to the list - I just watched this great video on composition vs inheritance: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wfMtDGfHWpA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wfMtDGfHWpA</a>
deepaksurti大约 5 年前
Separate interface from implementation. [1]<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Interface_(computing)#Software_interfaces" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Interface_(computing)#Software...</a>
codekilla大约 5 年前
In a few words?....&#x27;Don&#x27;t write the code in the first place&#x27;. Yes, follow best practices of modules etc, but in general write a lot less code. This is harder than it sounds, and often requires multiple rewrites.
taherchhabra大约 5 年前
I would suggest reading Head first design pattern, apart from design patterns the book also explains how &#x27;change&#x27; is inevitable in software development and how to design the software extensible and modular
pier25大约 5 年前
In very simple terms make small pieces of untangled code. Think in terms of small lego blocks that connect and can be replaced and modified independently instead of tangled webs of logic.
ykevinator大约 5 年前
Comments and empathy for others and your future self. That means minimalism and lots of comments to remind yourself what you were thinking when you wrote it, who calls it, why, etc.
crimsonalucard大约 5 年前
Your asking about modular code where modules can be changed or swapped.<p>The functional paradigm is the most modular style I&#x27;ve seen. Modularity is a side effect of immuteability.
airbreather大约 5 年前
If you can, use state machines, they provide a level of effective encapsulation otherwise hard to beat.<p>This menas ability to change specific behaviours wih very limited side effects.
评论 #22597694 未加载
jiveturkey大约 5 年前
TDD is the key.<p>By using TDD methodology (structure your code to support it, not necessarily obey the principle head to toe), your code will be 80% of the way there.
peterhi大约 5 年前
Write shallow code, code that is the simplest solution for the problem<p>If the word &quot;architect&quot; springs to mind then you are doing it wrong
stevens32大约 5 年前
Make code easy to change by changing it often, thus forcing you to either structure it so that it&#x27;s easy to change, or die
Wildgoose大约 5 年前
Optimise code for Deletion.<p>If it&#x27;s easy to delete, then it&#x27;s easy to replace.<p>And if it is easy to delete then it should be naturally loosely coupled.
rolph大约 5 年前
try to anticipate where the code may have to change, and with what frequency. For example how likely is it that a modification to handle new file specification will be required, or how often would you have to integrate new authentication methods. how often would you need to give things a facelift to keep up with marketing trends.
sergioisidoro大约 5 年前
A lot of people say &quot;You could write this X&#x2F;Y block in one line&quot; or &quot;You can simplify this&quot;.<p>A lot of the times they don&#x27;t mean simplify, they mean compact code.<p>I write verbose code, as explicit as possible. Sometimes it looks like it was written by someone just learning to program. And that&#x27;s on purpose to make the code as easy to understand and change as possible.<p>TL:DR: Be skeptical about code &quot;simplification&quot; and don&#x27;t fall on using fancy language features when if&#x2F;else conditions do the job.
throwawayForMe2大约 5 年前
Encapsulate the concept that varies.
hprotagonist大约 5 年前
high test coverage and functional purity as much as possible.<p>don&#x27;t get trapped by being &quot;too DRY&quot;
lincpa大约 5 年前
Keep code Simple and Unified.<p>Keep data and logic separate, data-driven development.<p>Adhere to the &quot;standardized data interface specification&quot; and use pipeline-functions to manipulate data from the initial state to the final state.<p>Pipeline-functions conforming to standardized data interfaces are easy to change, replace, and insert extensions.<p>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:<p>- product standard (data interface): the req-map and resp-map of the ring<p>- warehouse: the ring<p>- workshop: the pipe functions on both sides of the C&#x2F;S, and Raw materials (hash-map) are transferred to each other through warehouses through interactions.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;linpengcheng&#x2F;PurefunctionPipelineDataflow" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;linpengcheng&#x2F;PurefunctionPipelineDataflow</a>
PaulHoule大约 5 年前
Don&#x27;t repeat yourself.<p>There is an objective way to tell if design A is better than design B, and that is to measure how hard it is to make changes. You have to build that into your culture and not optimize for something else, or do things because somebody thinks that is the way to do it.<p>A big problem in current architectures is that what seems to be a small change (say, add a &quot;cell phone&quot; field next do a &quot;home phone&quot; and &quot;office phone&quot;) field often requires a number of little changes in widely separated code:<p>* a database schema change * changes to database queries * changes to a back-end API * changes to a front-end Javascript app * changes to an iOS mobile app * changes to an Android mobile app<p>Conventional system design means that what looks like one change to management is actually at least six changes, which might affect that many source code repositories, etc.<p>An ideal design would let you create a feature by writing all of your code in one place. Of course this would require a big change in how applications are structured and built, but it could lead to a durable advantage. See<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Software_product_line" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Software_product_line</a> <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Feature-oriented_programming" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Feature-oriented_programming</a> <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Low-code_development_platform" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Low-code_development_platform</a><p>The biggest barrier to the above I think is that when you talk to a team they always have shiny objects that they can&#x27;t live without. Maybe they think functional programming is great (yeah, you can handle errors with monads, but you can drop errors on the floor just as easily as you can with exceptions, return codes, etc.) or that immutability solves everything, JDK 13 is the best, whatever. So people are thinking about everything except the thing that management will care about which is long-term sustained productive development.
评论 #22594081 未加载