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.

Programming Idioms

532 pointsby hazboover 5 years ago

38 comments

c3534lover 5 years ago
These really aren&#x27;t idioms. An idiom in natural language is a phrase or saying whose meaning cannot be understood without someone telling you what it&#x27;s supposed to mean (that is, you can&#x27;t figure it out just by reading the words).<p>In programming, an idiom is a common, accepted way of accomplishing a task which you wouldn&#x27;t figure out to do all on your own and whose meaning isn&#x27;t obvious to programmers of other languages.<p>I suppose you could argue that certain idioms are more or less universal: using &quot;i&quot; as the index in a loop isn&#x27;t obvious even if you know a lot of math (why not use j?).<p>But no one who doesn&#x27;t program Python is going to figure out the first thing you do in any new Python project is write the line:<p><pre><code> if __name__ == &quot;__main__&quot;: main() </code></pre> The fact that it&#x27;s impenetrable if it&#x27;s not first explained to you that you should do that is what makes it idiomatic.<p>Idiomatic also means particular to a specific person or group. If your website is based on comparing the same idiom in multiple languages, that&#x27;s a good indication it&#x27;s not actually idiomatic. That&#x27;s probably just a common task.
评论 #21082427 未加载
评论 #21084251 未加载
评论 #21081963 未加载
评论 #21082637 未加载
评论 #21082135 未加载
评论 #21082441 未加载
评论 #21084914 未加载
评论 #21084876 未加载
评论 #21082399 未加载
评论 #21083169 未加载
addictedcsover 5 years ago
First example I&#x27;ve checked is wrong [1]. Shuffle a list, with a static Random object initialization. Using a static random object is not thread-safe in .NET.<p>&quot;One common solution is to use a static field to store a single instance of Random and reuse it. That’s okay in Java (where Random is thread-safe) but it’s not so good in .NET – if you use the same instance repeatedly from .NET, you can corrupt the internal data structures.&quot; [2]<p>Very often the very same resources which claim to be the source of truth provide bad code examples.<p>[1] <a href="https:&#x2F;&#x2F;www.programming-idioms.org&#x2F;cheatsheet&#x2F;Csharp" rel="nofollow">https:&#x2F;&#x2F;www.programming-idioms.org&#x2F;cheatsheet&#x2F;Csharp</a><p>[2] <a href="https:&#x2F;&#x2F;codeblog.jonskeet.uk&#x2F;2009&#x2F;11&#x2F;04&#x2F;revisiting-randomness" rel="nofollow">https:&#x2F;&#x2F;codeblog.jonskeet.uk&#x2F;2009&#x2F;11&#x2F;04&#x2F;revisiting-randomnes...</a>
评论 #21081510 未加载
评论 #21081036 未加载
评论 #21082049 未加载
评论 #21082241 未加载
kerkeslagerover 5 years ago
I&#x27;d like to draw people&#x27;s attention away from this and to Rosetta Code[1].<p>Programming Idioms has a slightly more modern interface, but what matters more is the content. Rosetta Code has <i>more</i> content and <i>better</i> content. If you don&#x27;t like Rosetta Code&#x27;s interface, I&#x27;d rather you tried to improve Rosetta Code rather than splintering the already-small community of people producing this sort of content.<p>[1] <a href="https:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Rosetta_Code" rel="nofollow">https:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Rosetta_Code</a>
评论 #21085921 未加载
评论 #21084779 未加载
markildover 5 years ago
Cool project.<p>I&#x27;m a bit reluctant to trust the quality of this, however.<p>The first example I looked up was &quot;Format date YYYY-MM-DD&quot; in Python, which wanted me to use &quot;isoformat()&quot;, which produces something else.<p>It was easy to correct, but I&#x27;d like to see a more &quot;democratic&quot; approach to getting correct examples, not just taking the latest one as fact.<p>edit: Sorry, more precise critique of the usage of &quot;isoformat&quot; would be &quot;which _might_ produce something else&quot; (it depends on the resolution of info in the date object).
评论 #21080846 未加载
评论 #21081516 未加载
评论 #21080844 未加载
评论 #21080862 未加载
tompover 5 years ago
Hm... <i>Idiom #186 Exit program cleanly</i> for <i>Python</i>:<p><pre><code> import sys sys.exit(0) </code></pre> Well, I guess I&#x27;ve been reinventing the wheel for my whole career... and thank god I did, my wheel is not only more performant, but also shorter and produces cleaner code (in my subjective opinion). As simple as:<p><pre><code> # nothing</code></pre>
评论 #21081243 未加载
评论 #21085491 未加载
评论 #21082157 未加载
coldteaover 5 years ago
If people didn&#x27;t re-invent the wheel we&#x27;d still be using wheels made of rock like the Flintstones (or at best wooden wheel, like 4 millennia ago).
评论 #21081983 未加载
评论 #21082299 未加载
评论 #21081849 未加载
评论 #21083714 未加载
ukjover 5 years ago
The reason I re-invent wheels is because I don&#x27;t know the idiom I need to Google for.<p>I know what I need. I don&#x27;t know what it&#x27;s called in English. It&#x27;s faster to re-implement it than to learn the vocabulary.
评论 #21081239 未加载
评论 #21081211 未加载
评论 #21081680 未加载
评论 #21081787 未加载
yellowappleover 5 years ago
The C implementation (and probably most of the other implementations) of Idiom #41 (reverse a string) doesn&#x27;t correctly handle UTF-8; it just reverses an array of bytes. Normally I wouldn&#x27;t take objection to this, but:<p>1) The problem description specifies &quot;Each character must be handled correctly regardless its number of bytes in memory.&quot;, for which the C implementation rather plainly fails.<p>2) UTF-8 is so ubiquitous nowadays that any string-manipulating function that fails to correctly handle multibyte UTF-8 characters ought to be considered broken.
suixoover 5 years ago
I love the &quot;check if integer addition will overflow&quot; in Python:<p>def adding_will_overflow(x, y): return False<p>I have no idea if it&#x27;s actually true but I find that funny.
评论 #21083881 未加载
dahartover 5 years ago
Do these count as “idioms”? Are these problems commonly messed up? I sampled a dozen or so and all the ones I saw are snippets for doing really basic tasks, but didn’t seem particularly idiomatic, nor like tasks that cause a lot of bugs...
评论 #21081292 未加载
cr0shover 5 years ago
If you&#x27;re going to list Fortran, COBOL, Prolog, Ada, Lisp, etc - I&#x27;m of the opinion that you need to list BASIC as well.<p>You list VB - but you don&#x27;t say if this is VB.NET, or prior VB versions (3,4,5,6)? VB.NET isn&#x27;t quite identical to the others...<p>...and VB is nothing like plain-old BASIC (and later dialects). In fact, you might lump them all under BASIC, as VB owes more to BASIC than being it&#x27;s own thing (though VB.NET seems more a name than being BASIC - just my opinion, though, as a long time ago VB3-6 coder - I&#x27;d call it &quot;BASIC-ish&quot;).<p>Other than that - this looks interesting. UI could use a bit more work, but seems like a good start.
thsealienbstrdsover 5 years ago
Quality of the site aside, this concept would be great with JSON-API or something so it can be integrated with &lt;insert favorite editor&#x2F;IDE here&gt;.
velcrovanover 5 years ago
Hey, it&#x27;s a neat project. Of course it could use improvements and a few disclaimers, but hey. Good on you for making it.<p>Added a request for Racket support!
krappover 5 years ago
The wheel is, in terms of elegance and function, a perfect machine - it can&#x27;t be simplified, nor can it be improved upon. The point of the idiom being that any attempt to do so will either lead back to the wheel, or an inferior, unnecessarily complex alternative.<p>The problem with applying this to programming is the implication that there are languages, frameworks, patterns, and idioms which are equivalent to the wheel, in that there always exists one objectively correct, perfect solution which can&#x27;t be improved upon. I don&#x27;t believe programming has the equivalent of a wheel, yet.<p>What people tend to mean when they say &quot;don&#x27;t reinvent the wheel&quot; in the context of programming is either &quot;don&#x27;t waste time writing code that already exists and is <i>adequate</i>&quot; or &quot;don&#x27;t use $X because I like $Y.&quot;
alkonautover 5 years ago
I think what happens with this is too much context is lost and in the end you can&#x27;t really describe what the &quot;right way&quot; to do things is.<p>Is the right way to delete a file<p><pre><code> result = os.deletefile(path) </code></pre> or is it<p><pre><code> try { os.deletefile(path) } catch (ex) { log(ex) } </code></pre> and what is the difference? Both may be valid and idiomatic <i>in the same language</i>. With more context such as a forum dialog or a StackOverflow question, these things are usually ironed out. But trying to do the same thing in Haskell (where you might have monadic error handling) or java (where you might have some exception handling chain) is very different even though the same function call is used in the end to actually delete a file.
grumpy8over 5 years ago
I&#x27;d look into &quot;webdesign-idioms&quot;
评论 #21081366 未加载
inlinedover 5 years ago
It seems like the checklist blurs the lines between a language and standard libraries, something that shouldn’t be controversial but should be noted.<p>The checklist also seems to favor dynamic languages. E.g. does Go deserve to miss a checkmark because you can’t determine if a variable exists at runtime (only at compile time)? I can sorta see the source inclusion checkbox, but it’s not entirely clear from the example if this is testing for a macro support, AOP, or dynamic loading of libraries.
blobsover 5 years ago
The idea is great. But the low quality of many code examples puts me off. For me it would only work if the examples shown are made by a selection of top level developers. Then it would be really nice actually.<p>About the site design, the little menu on the left should better not move the content window to the right. Now, when you scroll down in &#x27;all idioms&#x27; for example, the list is right aligned and left has useless whitespace. A horizontal menu or dropdown would be better I guess.
val_deleplaceover 5 years ago
This submission being popular in HN caused an interesting traffic spike in Programming Idioms. Here is a detailed recital of the technical and financial consequences: <a href="https:&#x2F;&#x2F;medium.com&#x2F;google-cloud&#x2F;surviving-traffic-spike-from-hacker-news-my-dreaded-google-cloud-invoice-6b940dd9eba6" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;google-cloud&#x2F;surviving-traffic-spike-from...</a>
self_awarenessover 5 years ago
This site would really gain by having an upvote mechanism, this way users would know what is the &quot;best&quot; way of doing things. Plus, needs more languages.
评论 #21080948 未加载
评论 #21080932 未加载
banachtarskiover 5 years ago
I took a gander at a bunch of the C++ ones. They are pretty horrible. It looks like direct translations from generic snippets (just a syntax transformation).
svnpennover 5 years ago
I run a similar site:<p><a href="https:&#x2F;&#x2F;cup.github.io&#x2F;autumn" rel="nofollow">https:&#x2F;&#x2F;cup.github.io&#x2F;autumn</a>
tabtabover 5 years ago
Fancy data structure manipulation perhaps should be part of a library (API) and not &quot;built in&quot; to the base language. The best languages make it easy to use vast API&#x27;s rather than hard-wire features into syntax.
nmcaover 5 years ago
The first one I saw was wrong, suggesting map over list comprehension in python...
评论 #21082488 未加载
Tade0over 5 years ago
I applaud the effort, but some examples (in JS at least) appear to be outdated.
评论 #21081009 未加载
varjagover 5 years ago
<a href="https:&#x2F;&#x2F;www.programming-idioms.org&#x2F;idiom&#x2F;117&#x2F;get-list-size&#x2F;1996&#x2F;c" rel="nofollow">https:&#x2F;&#x2F;www.programming-idioms.org&#x2F;idiom&#x2F;117&#x2F;get-list-size&#x2F;1...</a><p>lol
treeformover 5 years ago
I was going to fill in the nim stuff, but:<p>Sorry, [nim] is currently not a supported language.
kbensonover 5 years ago
Well, I knew it was only a matter of time before someone made this. This was one of my favorite &quot;some day I&#x27;ll have time and can make this site&quot; ideas.
mettamageover 5 years ago
This is a great place to see if I have conceptual knowledge gap. Which is why I am asking: where are the metaprogramming idioms?
stellaloover 5 years ago
It’s funny how the most empty row in the coverage table is “matrix multiplication” — only the FORTRAN implementation is there
arcticwombatover 5 years ago
Eh, what&#x27;s wrong with Rosetta Code? A metric ton of examples in a metric ton of languages.<p>What&#x27;s better about this one?
simplyconnectedover 5 years ago
Been wanting this for a while. How else can one communicate the power of ciso8601 over step time?
hota_maziover 5 years ago
More recipes than idioms.
programminggeekover 5 years ago
Reinventing the wheel is a good idea. It&#x27;s how you learn. Programmers who say &quot;don&#x27;t reinvent the wheel&quot; don&#x27;t understand how the craft of wheel making works.
评论 #21080887 未加载
stOneskullover 5 years ago
i like looking at how other languages do things. i&#x27;m pushed away from Go and pulled toward Rust, the two languages i&#x27;ve been thinking of getting deep into. like the currying example. that move keyword seems pretty nice.
tmshover 5 years ago
substring for c++ and other languages needs an j-i+1.
BucketSortover 5 years ago
Reminds me of <a href="http:&#x2F;&#x2F;www.rosettacode.org&#x2F;wiki&#x2F;Rosetta_Code" rel="nofollow">http:&#x2F;&#x2F;www.rosettacode.org&#x2F;wiki&#x2F;Rosetta_Code</a>
评论 #21080817 未加载
评论 #21080747 未加载
评论 #21083066 未加载
评论 #21080823 未加载
评论 #21081439 未加载
trampolinerover 5 years ago
Would be great for the hoards of &quot;self taught&quot; programmers out there, who bluntly refuse to understand patterns (design or programming).
评论 #21080854 未加载
评论 #21081021 未加载