TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Clever vs. Insightful Code

156 pointsby goranmoominalmost 4 years ago

14 comments

theshrike79almost 4 years ago
A paraphrased translation from Lieutenant General Aksel Frederik Airo from Finland:<p><pre><code> You need to go to shit-ton of schools to be able to make simple decisions. Otherwise you&#x27;ll start to invent all kinds of over-complicated solutions. Everything in war is simple. The guy in the front lines is the one at war. You can&#x27;t win in a war unless you give simple orders. The people on top need to be able to give commands that are so simple that even the man in the front lines understands. Everything is as simple as possible. To understand that, you need to go study a lot. </code></pre> It&#x27;s not easy to do things the simple way, being clever is the easy path. Knowing when you need to be clever and when simple is enough is what separates experience from greenhorns.
评论 #27420924 未加载
drewcooalmost 4 years ago
When I advise people to write simpler code I do mean &quot;not clever.&quot; Nor do I mean &quot;insightful.&quot; In fact, please make it so obvious that in reading it I don&#x27;t gain&#x2F;need&#x2F;curse&#x2F;applaud any insights.<p>Make it simple enough that a brand new completely green junior member of the team immediately understands it on first reading.<p>And if you can&#x27;t make the code completely obvious (e.g. the sorting example), just add a comment. A simple one.<p>None of this is about the creator&#x27;s cleverness or deep understanding or speed or what tooling can do. It&#x27;s not about the creator or the machine at all. Simple code is about clarity for all the people who will read and (hopefully not) debug it later.
评论 #27418955 未加载
评论 #27418925 未加载
评论 #27418922 未加载
评论 #27419763 未加载
评论 #27420402 未加载
评论 #27420179 未加载
评论 #27419871 未加载
评论 #27421447 未加载
评论 #27419212 未加载
评论 #27419568 未加载
contrastalmost 4 years ago
While the author is suggesting some good techniques, I suspect that if your code is being criticised for being “clever”, you’re unlikely to improve the situation by arguing that in fact it’s “insightful”.<p>In my view, the most important aspect of what we mean by “clever code” is that the “cleverness” primarily serves to demonstrate how clever the programmer is. In other words, in some sense it obfuscates what the code is actually doing or why certain choices have been made, without delivering sufficient benefit to outweigh the cost to readability and maintainability.<p>To use their bucket sort example, probably you’d want to stick a quick comment in the code that bucket sort has been chosen (and why). That helps with readability. But there’s nothing “clever” about choosing a standard algorithm to solve a problem. Absolutely, making the right choice demonstrates insight - but it also demonstrates genuine cleverness.<p>Really it boils down to how it doesn’t matter whether the code is being called “clever”, “insightful”, or “amazing”. It matters whether a) the tone is dripping with sarcasm, or b) the tone is respectful but the feedback also contains a warning to format and comment for readability.
评论 #27420118 未加载
评论 #27421443 未加载
评论 #27419972 未加载
pmichaudalmost 4 years ago
One practical lesson I’ve learned about how to keep code simple is that possibly the most important factor to simplicity is staying on the same abstraction layer, ie. not accidentally slipping up or down the layers within the same code “stanza.”<p>If I’m trying to read a function that is ostensibly about, say, user auth, but what I’m actually reading is about database error handling and bitwise operations on a user role bitfield, something has gone wrong in terms of simplicity. A dumb example is “How do you drive a car?” The right answer is something like “Gas goes, Brakes slows, Wheel turns.” The wrong answer is something like “Ok well, you see there’s a drivetrain... oh and a spark plug, and then...”<p>In my experience very few code blocks need to be legit complex, eg. the core algorithm of some novel processing you’re doing on the data that came from a paper in the relevant field. Those unusual blocks can be sequestered and documented straightforwardly, with clear variable names and a comprehensive test harness around it.
评论 #27445364 未加载
chiialmost 4 years ago
instead of classifying it as &#x27;clever&#x27; and &#x27;insight&#x27;, i would classify it as knowledge in either the problem domain, or the solution stack&#x2F;tech.<p>Duff&#x27;s device uses knowledge of the solution tech (C language features). The sorting bucket example uses knowledge of the problem space - that you are sorting birth years of today&#x27;s population.<p>Some code would have both incorporated - e.g., the fast inverse square root of Quake fame. It requires knowledge of how floating point gets represented, how longs get represented and what happens when you cast between them. It requires knowledge of logarithms and how exponentiation is related to logarithms, and how newton&#x27;s method, with a good starting guess is quite accurate.<p>As for clever code - the more knowledge (of either kind) you need to know, the &quot;worse&quot; it is for someone to maintain it. As a corollary, if you comment on the code, comment should include both any and all domain knowledge, as well as tech stack knowledge, and reduce the burden on the reader&#x27;s mind.
sfvisseralmost 4 years ago
I still stand by this: simplicity is a function on familiarity more than anything else.<p>Twenty lines of procedural go code isn’t simpler than a one-liner of Closure. It might be, to some people.
评论 #27421481 未加载
评论 #27420803 未加载
j_wtf_all_takenalmost 4 years ago
I really hate that saying &quot;its simple until you make it complicated&quot;.<p>No! I really like the distinction between &quot;simple&quot; and &quot;easy&quot;.<p>Writing a little script that does some job is easy. Then you add lots of extra conditions and edge cases and exceptions because damn, its not as easy as you thought, so the script becomes complicated. After a while you figure out what the problem domain is, think of some smarter rules making the thing complex. Then you dumb down those rules, throw out unnecessary stuff, streamline things, realize that multiple specifically covered edge cases can actually be covered by changing some structure in your code etc - and finally, finally that all makes the thing simple.<p>Simple isn&#x27;t dumb. Simple is so elegant and smart that it looks dumb.
评论 #27424635 未加载
GuB-42almost 4 years ago
I am the only one to actually like reading clever code?<p>When I see something like Duff&#x27;s Device, my first reaction is a big WTF, like everyone else, but then, I sit down, try to understand, and when I get it, I am happy because I learned something. In fact, seeing Duff&#x27;s Device is really what help me understand switch&#x2F;case in C. And Duff&#x27;s Device is certainly outdated, but it is by no mean &quot;bad&quot;. If you need to unroll a loop by hand, that a good way to do it, it is just that you don&#x27;t need to unroll loops anymore.<p>So if you write code I have to read later, please don&#x27;t hold back, use every clever trick you have. A small comment would be appreciated but not necessary. This way, I can read your code and think &quot;I learned something today&quot;.<p>Does it slows down comprehension? Of course it does... once. That&#x27;s the cost of learning, but it is an investment, next time, I&#x27;ll be faster.<p>I am really talking about the cleverness the article disapproves of. I also value insight, but here, I agree with the article.<p>I know a common saying is &quot;newbies should understand your code at first glance&quot;. I only agree when by newbies, it means newbies on the project with regards to domain-specific knowledge. But when it comes to general principles, including knowledge of the programming language, code should be written for the highest level of expertise you can reach (but no further, you have to understand what you a doing!). I mean, if you are not using the full power of your tools, not only you are dragging yourself down, but the newbie you are trying to be kind with will not learn anything new.
评论 #27445305 未加载
gsliepenalmost 4 years ago
A great source of both clever and insightful code can be found at <a href="https:&#x2F;&#x2F;codegolf.stackexchange.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;codegolf.stackexchange.com&#x2F;</a>. Code golf is the sport of writing the smallest amount of code that solves a problem. It requires both clever use of syntax to write short code, but also insight in how to reduce the problem to something much simpler than it at first appears. For example, take this problem that checks if the given color name is one of the colors of the rainbow, where the insight is that you can create a perfect hash function to solve it: <a href="https:&#x2F;&#x2F;codegolf.stackexchange.com&#x2F;questions&#x2F;214678&#x2F;is-it-a-rainbow-color" rel="nofollow">https:&#x2F;&#x2F;codegolf.stackexchange.com&#x2F;questions&#x2F;214678&#x2F;is-it-a-...</a>
lamontcgalmost 4 years ago
I think &quot;clever&quot; is being applied at one level here and &quot;insightful&quot; is being applied at another.<p>You can write code at the equivalent of an 8th grade reading level like Hemingway wrote.<p>Each sentence can can be basic using a small vocabulary. Put together the whole novel can have depth and be insightful.<p>Its the difference between Hemingway and an actual random 8th grader.
brundolfalmost 4 years ago
I don&#x27;t think &quot;good&quot; vs &quot;bad&quot; has anything at all to do with &quot;platform&quot; vs &quot;problem&quot;.<p>It&#x27;s really a question of &quot;essential or extremely useful to the project goals&quot; vs &quot;fun&#x2F;satisfying for the person being clever&quot;. That&#x27;s the distinction we should focus on.
29athrowawayalmost 4 years ago
If you change it slightly you can make it more readable:<p><pre><code> def is_unique(elements): unique_elements = set(elements) has_only_unique_elements = len(unique_elements) == len(elements) return has_only_unique_elements </code></pre> The problem was using identifiers like &quot;l&quot;, and trying to make everything a one-liner.<p>Adding a docstring or comment can help too.<p>Is that still &quot;clever code&quot; after these few changes? I would say no.
评论 #27420774 未加载
评论 #27421018 未加载
ano88888almost 4 years ago
The example given about the 120 years old assumption will very likely be broken in the next 10 to 20 years... Perhaps it is better and safer to just stick to simple code.
评论 #27421007 未加载
imvetrialmost 4 years ago
Clever code vs Insightful Code.<p>Code is a configuration.<p>Configuration is no code.<p>Clever code vs Insightful code vs No code
评论 #27420783 未加载