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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

“Clean code” isn't actually clean

73 点作者 _ncxu将近 8 年前

20 条评论

FrankyHollywood将近 8 年前
Most guru&#x27;s take an &#x27;all or nothing&#x27; approach, this goes beyond programming, look at food-guru&#x27;s &quot;eat only carbs, never eat refined sugar&quot; etc. This is for me the same as &quot;always do TDD, never use static methods&quot;.<p>People without experience often look at guru&#x27;s because they can not yet think for themselves.<p>I&#x27;m a developer in my 40s, have done a lot of projects in a lot of different companies. Having this experience gives me intuition about what to do in what situation. I just (think I) know when I should write a unit test and when not.<p>The &#x27;all or nothing&#x27; attitude is created by people who want to be guru&#x27;s and is followed by people who want to have some guidance.<p>This is not a bad thing, it takes time to shape yourself as a programmer. This takes years: <a href="http:&#x2F;&#x2F;www.norvig.com&#x2F;21-days.html" rel="nofollow">http:&#x2F;&#x2F;www.norvig.com&#x2F;21-days.html</a><p>Learn from the guru&#x27;s, and be consious, people preaching an &#x27;all or nothing&#x27; view of the world are NEVER right :)
评论 #15122989 未加载
评论 #15122705 未加载
flohofwoe将近 8 年前
IME the &#x27;best&#x27; code solves an existing problem with the least amount of code, is human-readable, and easy to throw away (instead of being &#x27;future proof&#x27;).<p>Of course this &#x27;easy to throw away&#x27; will also lead to a slow replacement process of &#x27;good code&#x27; (that&#x27;s easy to replace) with new &#x27;bad code&#x27; (that&#x27;s incrementally harder to replace) and eventually each software project will end up in the same broken mess, no matter how well it started ;)
评论 #15122529 未加载
评论 #15122713 未加载
评论 #15122546 未加载
gfiorav将近 8 年前
This just seems like poor justification for laziness.<p>You need to keep a project clean. This means profound refactors when concepts for the product change. Some rules are:<p>- Keep it readable<p>- Don&#x27;t over engineering<p>- Easy to remove<p>- Good tests (complete, min stubbing, interface oriented. Never refactor and change tests at the same time.<p>A lot of &quot;veterans&quot; are just resented with new paradigms and too egotistical to stay relevant. It takes effort to maintain a clean project, many will try to say &quot;it&#x27;s not worth it&quot;. That&#x27;s just out of the question, it&#x27;s your job, it doesn&#x27;t have to be always pleasant or easy. If you don&#x27;t suffer for it a bit now, someone else will in the future (maybe clients or users).<p>This is what I like about open source projects, it teaches maintaince discipline. In the end, you can sum up all rules into one:<p>- Make it easy to maintain
评论 #15123044 未加载
评论 #15122598 未加载
评论 #15124935 未加载
jack9将近 8 年前
So no data, no examples, no anything. Full of such wisdom as &quot;Testing isn&#x27;t as important as it might seem but it&#x27;s still important&quot;<p>The only thing actually articulated was &quot;you don&#x27;t need 100% test coverage&quot;, which is not contrary to existing practice (afaik). I&#x27;m not sure the singular point nested in platitudes can be useful to me.
评论 #15122528 未加载
BigJono将近 8 年前
On my side projects at home, I can&#x27;t even remember the last time I had to slow down or got stuck on maintenance&#x2F;integration type stuff (as opposed to new functionality or algorithms, which don&#x27;t rely on existing code). And I have a couple of fairly big ones, it&#x27;s not all &quot;Todo list&quot; stuff.<p>It&#x27;s happened constantly at work though, throughout multiple jobs. Sometimes I&#x27;ve been so stuck I&#x27;ve got basically nothing done for days or weeks at a time. Sometimes that happens a few times in a row and I feel like I&#x27;m an inch away from being fired for incompetence. This even happens on relatively small projects. It&#x27;s not confined to big 10 year old monstrosities.<p>The sad thing is, I work for startups, which should be all about lean, clean code and making big changes rapidly to respond to business needs.<p>I think a lot of it comes down to how hard it is to work on other&#x27;s code. I don&#x27;t think any of the conventional wisdom is a solution to this problem either. I haven&#x27;t noticed any discernable difference between projects with big fuck off linters, or 50 page style guides. It&#x27;s not a mish mash of semi-colons that&#x27;s causing trouble (although consistency is nice).<p>&quot;Dirty code&quot; is really the crux of the problem. But like the author of this article, I don&#x27;t always agree with what the conventional wisdom says about writing clean code.<p>To me, the most important part of keeping your code clean is always having the minimal solution to the problem. Your code shouldn&#x27;t do one thing extra it doesn&#x27;t need to. Half of this is YAGNI, half of it is being a good enough developer to come up with simple solutions that aren&#x27;t too fancy.<p>On that note, don&#x27;t get too fancy, and don&#x27;t take DRY too seriously. Whatever complicated mess of higher order functions you&#x27;re writing probably isn&#x27;t going to phase other experienced devs in a vacuum, but combined with all the other icky parts of the code base and it might just be the straw that breaks the camel&#x27;s back. If you need to write twice as many lines to make it more grokable, that&#x27;s fine.<p>Speaking of YAGNI, people should be applying it somewhat to dependencies as well as functionality. It&#x27;s just far too easy for a simple React project to blow out in complexity because of an over-reliance on other&#x27;s code. Sometimes it&#x27;s easier to build something out of code than NPM Lego.
评论 #15123255 未加载
评论 #15122574 未加载
raverbashing将近 8 年前
Criticizing code for not being &quot;clean code&quot; is 70% of the time &quot;I don&#x27;t like this code for various reasons&quot;<p>Not to mention of the pedantism of one of the biggest proponents of clean code.<p>So, in essence, worry that your code has a certain quality but in the end all projects will end up in the trash can. It&#x27;s software, not the Mona Lisa.
darawk将近 8 年前
I think the problem here is that &#x27;clean&#x27; is an umbrella term for a bunch of different things. To sum them it is: &#x27;how does this code make me feel inside&#x27;. But what goes into that are: readability, extensibility, maintainability, testability, clarity, correctness, simplicity, and yes, aesthetics. Some of these are <i>objectively</i> always good, and some of them are contingently good.<p>For instance, a simple algorithm for distributed consensus is probably not correct, and you should rightly not view it as &#x27;clean&#x27;. Some things simply aren&#x27;t simple. The goodness of simplicity is contingent on the complexity of the problem.<p>But aesthetics is at the final end of the spectrum: it&#x27;s purely subjective. And yet we wrap all these things up under the term &#x27;clean&#x27;, and so it is confusing. People will defend their notion of cleanliness using the objective standards, and then apply it to the subjective cases.
panic将近 8 年前
Given how young the practice of programming is, I doubt anyone alive today knows how to write truly good code. Just look at the kind of code people used to write 30 or so years ago. It&#x27;s interesting to think about: what will &quot;clean code&quot; look like in 2047?
评论 #15122597 未加载
aidenn0将近 8 年前
RE: Automated testing.<p>Modulo certain forms of static analysis, your code is ultimately only as good as it is well tested[1]. Non-automated tests do count here, but for long-maintained codebases the cost of automating pays for itself very quickly.<p>1: Note that pretty much all code has been informally integration tested (A &quot;can I run it&quot; type of smoke-test is a very simple integration test). Similarly all compiled languages (and any interpreted languages that parse an entire file before running code) has <i>some</i> static analysis, as syntax errors will be caught. Unless you check in code without compiling or running it, you are already doing some testing.
devnonymous将近 8 年前
I don&#x27;t understand the negativity in the top voted comments here! It&#x27;s almost as though I read a completely different article than the one being commented on.<p>The comments here are being dismissive and then reiterating what the post said! Here&#x27;s the summary from the post :<p>&gt; So I stopped worrying about whether my code is perfect. And I just accepted that if I can&#x27;t see any immediate flaws with the code, and if all the tests pass (whether automated or manual), then it&#x27;s fine. And I trusted that if I ever come across a bug, I can fix it.<p>Why, specifically, is that a bad thing?
flor1s将近 8 年前
I love the story about a group of American car manufacturer executives visiting factories of Japanese car manufacturers in the 80&#x27;s. After returning to the US they were asked about their visit and they remarked that the Japanese did not show them their real factories, as the Japanese factories were too clean according to the American executives. As it turns out, a clean work environment (and that includes source code) has benefits if you are doing logical work.
AstralStorm将近 8 年前
Sometimes the warts are just warts and mistakes, sometimes there is a deep undocumented reason for it. Which indicates a problem as reasons like that should be explicitly documented.<p>And yes, often clean code actually is finished. Mess is often produced by wrong architecture or rushed development which means not finished if maintainability is in your definition of code being done. What the author probably meant is concise code instead.
chris_wot将近 8 年前
There is something really cool about looking at a large enough codebase, and just <i>reading</i> it. Then when you absorb it, then you might see a way to refactor it. I did this with the error handling code in the VCL module of LibreOffice. Frankly, I just did some renaming and a small bit of logic changes, but it just made the code far easier to understand and work with.
hasenj将近 8 年前
Promising title, but not enough content.
arkh将近 8 年前
Shitty documented code beats clean undocumented code for maintenance. Maybe we need more &quot;how to document my codebase&quot; tutorials than &quot;how to reinvent the wheel in this shinny new language&quot; ones.
dovdovdov将近 8 年前
Attended a couple of clean code trainings, always wrote the shittiest code for a week or two afterwards. ¯\_(ツ)_&#x2F;¯
评论 #15122554 未加载
Izmaki将近 8 年前
Essentially you are recommending that people slack off as soon as they face trouble instead of spending time making sure they don&#x27;t break stuff. Had you mentioned Continuous Integration and many commits per hour I would agree that tests and nurture is less important but you seem to base your thoughts on the laziness of old people.
atdt将近 8 年前
What&#x27;s the fence story?
评论 #15122492 未加载
nicky0将近 8 年前
YAGNI?
评论 #15122604 未加载
Walkman将近 8 年前
This guy mix and match totally unrelated things.<p>- clean != finished<p>- clean != perfect<p>- ugly is not clean<p>- you can understand a code and find it still ugly<p>What is he actually talking about?<p>&gt; Automated tests are not that important<p>I agree with the content of this section, but the title is bad.