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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Qualities of clean code

192 点作者 neokya超过 8 年前

22 条评论

ewjordan超过 8 年前
Over the years I&#x27;ve seen many more unmaintainable atrocities caused by diligent adherence to these principles than I ever have because of ignorance of them.<p>All the horrid Java libraries that we hated working with 10 years ago were created because of slavish devotion to the single responsibility principle, short methods, DRY, and testability. The absolute worst codebases I&#x27;ve ever seen sprung from an overaggressive concern for extensibility.<p>I do agree with the guiding principle, that when you&#x27;re writing code, it should be written for other people to read and work with. But sometimes that actually <i>does</i> mean that a class should represent an object that does more than one thing, and you shouldn&#x27;t fight it. Sometimes a gritty algorithm really <i>does</i> make the most sense if it&#x27;s laid out as a 40 line function in one place rather than spread across 5 different classes so each part is independently swappable and testable. Sometimes you really <i>don&#x27;t</i> need extensibility, you know that up-front, and building it in just pisses off everyone that ever has to touch your code because now they have to go through five or six &quot;Find implementing classes&quot; dances to find the actual code that runs when your interface methods (of which, of course, there&#x27;s only a single implementation) are called. Don&#x27;t even get me started on abuses of dependency injection when it&#x27;s not necessary...<p>Part of me is glad that these concepts are so commonly discussed, because they really are good things to consider, and can make code much tidier and easier to work with in the best case. But it takes a lot of experience to know when and where you should and shouldn&#x27;t follow these &quot;rules&quot;, and it tends to be much easier to unwind a novice&#x27;s spaghetti code and tidy it up than it is to pick apart a poorly conceived tangle of abstraction and put it back together in a way that makes sense.
评论 #12505848 未加载
评论 #12506026 未加载
评论 #12505724 未加载
评论 #12505677 未加载
评论 #12505668 未加载
评论 #12506441 未加载
评论 #12516804 未加载
评论 #12507662 未加载
评论 #12509734 未加载
评论 #12506183 未加载
评论 #12508071 未加载
评论 #12506918 未加载
评论 #12505716 未加载
评论 #12505734 未加载
gravypod超过 8 年前
I&#x27;ve often wanted to talk to people about things like these and I&#x27;m often pushed back by people saying &quot;This is simple, we all know this&quot; but I don&#x27; think that&#x27;s the case. If anything I think we know very little about how to keep software clean and simple. I&#x27;ve been trying to change that in my works by having people comment on my code that I&#x27;ve recently written for homework but people aren&#x27;t really up for the idea.<p>I&#x27;ve had one discussion that stood out. We were talking about functional programming and I said that the idea of functional programming was the segregate state to the smallest unit of computation that must operate on it. I was met with my &quot;friend&quot; just blindly telling me to &quot;stop saying that&quot;. I asked for a counter argument to that statement but they just said that I should stop saying that.<p>We as a community are horrible at speaking about code quality , evaluating each others work, and even just sifting out opinions from facts. It&#x27;s crazy and it&#x27;s something that needs to change if we want to take our field to a future where we can all be happy with the code we are writing.<p>I&#x27;ve got some suggestions I&#x27;m happy to talk to others about. My email is in my about page on this website. Please comment here or email me and I&#x27;d like to talk about this!
评论 #12505937 未加载
评论 #12505858 未加载
评论 #12506268 未加载
taneq超过 8 年前
Something that lists like this always seem to miss is locality of reference. You shouldn&#x27;t have to jump around all over a file (much less through several files) to follow the code that does a single thing.<p>The single responsibility principle should really be &quot;exactly one&quot; rather than &quot;not more than one&quot;. If a function is responsible for less than a whole thing, it shouldn&#x27;t be a function on its own.
评论 #12513772 未加载
keithb-超过 8 年前
&gt;&gt; Can be easily extended by any other developer<p>This is a tenet that will lead inexperienced developers astray. This &quot;rule&quot; is just too ambiguous. Extensibility is a fascination for object-oriented programmers but in my experience doesn&#x27;t have a lot of successful examples. Typically I have seen this manifest itself in either a Command+Composite style where every meaningful class is a composite of other SRP classes, or in a proliferation of interfaces that seldom have method definitions and are instead used to enforce coding standards or dependencies.<p>KISS is incompatible with this rule and you should kill this rule with fire because simple is not extensible. Perhaps when the goal is extensibility then should you consider other developers, but if you are developing a &quot;beige&quot; application then you should not consider extensibility. Instead, just assume that release management will handle changes, i.e. another developer will update rather than extend your class and that will be released in version 1.1.<p>Of course, to do this also means admitting that version 1.0 of your class was pretty much garbage and that it needed to be &quot;extended&quot;. Tough pill to swallow for some.
评论 #12506662 未加载
mosselman超过 8 年前
While some of these points have something to do with &#x27;clean&#x27; code or maintainable code or whatever, this copy-pasted list of very very shallow statements about code isn&#x27;t really useful. I expected there would at least be a few examples on how to implement these principles, perhaps in a &#x27;before and after&#x27;.<p>Maybe there should be a &#x27;click-bait&#x27; button on HN with which we can report things as such, along with posts such as &#x27;Why I won&#x27;t be using popular-technology-x ever again&#x27; and &#x27;10 things I hate about SQL&#x27;
评论 #12511074 未加载
pyrale超过 8 年前
&gt; 3. It should not be redundant<p>Not sure I agree with this one. While abstractions are a great way to reduce the length of code, sometimes they break readability. When you read code, sometimes, you feel like you don&#x27;t read a solution to your problem, but a way to solve your problem masked behind abstractions far removed from the domain concepts.<p>That&#x27;s why, sometimes, redundancy is better than the wrong abstraction.
评论 #12505805 未加载
z5h超过 8 年前
I believe an important quality is that code should TELL A STORY.<p>Consider a biography: you could simply collect facts about a person and write them in an arbitrary order and call it a biography. It could be a complete and accurate account, and still be impossible to read or follow.<p>Well written code is not only complete, but it also guides the reader through the logic.<p>Consider the difference between:<p><pre><code> statuses = [] reporter = Reporter.new jobs.each do |job| statuses &lt;&lt; job.complete &amp;&amp; !job.error end </code></pre> and<p><pre><code> job_statuses = jobs.map do |job| job.complete &amp;&amp; !job.error end job_status_reporter = Reporter.new </code></pre> In the first case, we see statuses declared. Statuses of what? Not yet clear. And the code that updates it is separated by unrelated code. Also, what will reporter be reporting? In the second case, map and better naming are used making it clear that we are getting a status for every job. Aha! I don&#x27;t even need to look at the implementation of the do block to understand what&#x27;s happening.
评论 #12509197 未加载
k2xl超过 8 年前
Clean code isn&#x27;t always the most efficient code, but I think that&#x27;s fine. Many times in my career I&#x27;ve found that you don&#x27;t always need the most optimal, efficient, and performant solution to get the job done, and that sometimes you need to sacrifice those attributes for clear, readable, understandable code. Good architecture usually supercedes speed of individual algorithms. Hence why you see almost every major language under the sun has been used at scale.<p>I remember when looking into neural nets, the basic python code to get one running was super easy for me to understand. However, I also realize that the optimal, most efficient methods of neural network libraries are way more complicated (and for good reason).
baron816超过 8 年前
Most of all, clean code should be easy to reason about.
评论 #12506063 未加载
评论 #12505869 未加载
kasey_junk超过 8 年前
Extensibility is important in some cases and a detriment in others. Most of the rules about extensibility came from environments where the code was running in many places at the same time (ie delivered to customers).<p>In a SaaS environment the code needn&#x27;t be extensible as there is only 1 copy of it. It is much more important for the code to be changeable, rather than extensible and in many cases the things you do to make code extensible make it harder to alter fundamentally.<p>Its important to understand that how you deliver your software is one of the biggest guiding factors in how you design your software and take that into account.
pif超过 8 年前
If written by someone else, clean code is: all and only the code that I personally won&#x27;t have to maintain :-)
swalsh超过 8 年前
Something I&#x27;ve noticed, in bad code if you need to learn something, or change something... Simply &quot;searching&quot; the code isn&#x27;t enough. Maybe there are multiple places that do a similar thing, maybe the names are weird, maybe it&#x27;s so condensed as to be unreadable. Whatever the cause... The only way to find the piece doing the thing is to step though it.<p>Good code, you can search it... even if the majority of it is unfamiliar. Find a piece, and say &quot;oh this is probably the spot&quot; with out ever executing it.
pif超过 8 年前
I recommend &quot;What Could Possibly Be Worse Than Failure?&quot; by Alex Papadimoulis (<a href="http:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;What_Could_Possibly_Be_Worse_Than_Failure_0x3f_" rel="nofollow">http:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;What_Could_Possibly_Be_Worse...</a>).<p>An excerpt: <i>We are practically the only industry where completion and success are synonymous. If the foundation of a one-year-old home is crumbling and its roof is plagued with leaks, would anybody actually call that a success? Despite being filmed and produced on budget, is there anyone who would not be ashamed to have Gigli on their filmography? Of course not! So why are the products we create – complex information systems that should last at least fifteen years – be held to a different standard?<p>Now think back those projects of yours. How many would you say are maintainable by someone with less business knowledge and a weaker grasp of the system’s design? How many will not snowball into an unmaintainable mess? How many do you truly believe could last fifteen years? I’ll bet that number is quite a bit lower than all of them.</i>
评论 #12506331 未加载
评论 #12506336 未加载
erlich超过 8 年前
Being able to simulate amnesia is my goal.<p>I write code that I come back to and don&#x27;t understand, and realise its terribly complex because I understood everything at the time, but could not imagine what it would be like coming back after a month to make a change, or for someone new to try to understand it.<p>Code is for people to understand.<p>Some people think its for being able to write good tests, eliminating all side-effects and shared state, for the computer to be able to run quickly and optimise, to be easy to read.<p>But its really just about being able to be understood. Most time will be spent in maintenance.<p>When you modify and debug, you are diving into the middle. You are not walking through the code base from the beginning and reading all the comments.<p>It needs to be understandable from all locations.<p>I&#x27;m a strong believer in micro-modules. Left pad et. al. I try to create small modules which do one thing well that I can trust and not have to think about.
drakonka超过 8 年前
If I&#x27;m remembering correctly this article seems to mostly paraphrase what the various developers Robert C. Martin interviewed for this book (&quot;Clean Code: A Handbook of Agile Software Craftsmanship&quot;) said...in mostly the same order.
abhas9超过 8 年前
Someone very experienced once told me: &quot;For one change in requirement there should me one change in your code. That&#x27;s a clean code. If you can achieve it without any change in code, it&#x27;s even better.&quot;<p>This quote always sticks in my mind :)
antoaravinth超过 8 年前
&gt;&gt; 5. Can be easily extended by any other developer<p>I can remember a instance, when I started my carrier. I was a JS developer. I wrote a module with Functors, Compose, Partial etc.<p>In code review my team told they didn&#x27;t understand anything and reading such a code is not pleasant for them. I was upset, I was thinking why my team is not happy with it.<p>Today I can make lot of sense; stick to the pattern&#x2F;design of your team. If your team follows &#x2F; loves functional programming in your project, stick to it. If not try to advice them why functional programming would be better than normal approach.<p>End of the day, all it matters is writing simple, elegant code which others can understand.
textmode超过 8 年前
With the exception of shell scripting which I&#x27;m writing every day, I remove &quot;code&quot; more than I write it. C is the language I deal with.<p>The easier it is to remove code from someone else&#x27;s program, the &quot;cleaner&quot; the code.<p>That&#x27;s my definition of &quot;clean code&quot;.<p>For example, I just had to edit the code for a text-only browser to remove a few &quot;features&quot;. For example, the author recently decided it would be a good idea to <i>non-interactively</i> access no-content refs in a page such as &quot;prefetch&quot; and other garbage.
评论 #12507352 未加载
k__超过 8 年前
The thing I learned from working with absolute beginners to mediocre developers is: good modularization&#x2F;isolation is the key to success.<p>Yes, most things in the article are nice to have, but you can&#x27;t teach everything in a month and people need to be prodcutive to be worth their money.
partycoder超过 8 年前
A piece of code should not know a lot about the system and should work under a restricted set of assumptions and preconditions.<p>When this rule is not respected:<p>1) You end up with lot of coupling<p>2) Unit tests end up with a lot of overhead
caub超过 8 年前
in the background image, the code would be better with arrow functions, or object shorthand methods
评论 #12508173 未加载
JustSomeNobody超过 8 年前
A &quot;Top X&quot; post on HN. And can there really be anything new said about qualities of clean code now that hasn&#x27;t already been said many ... many times before? Given that it is a &quot;Top X&quot; post, I don&#x27;t feel confident there will be.<p>I&#x27;m passing on this article.
评论 #12505587 未加载
评论 #12505676 未加载