10 ms·
Programming Idioms
- reaperducer 5y agoIs it problematic that the same link was submitted by the same user 49 days ago?
- mkl95 5y agoHN invited me to repost it. I thought it was a good idea.
- Shared404 5y agoI concur, I didn't see it last time and am glad I did this time.
- dang 5y agoThis is in the FAQ: https://news.ycombinator.com/newsfaq.html#reposts https://news.ycombinator.com/newsfaq.html#reposts.
- mdp2021 5y agoOfficial practice (see FAQ): please also consider page: https://news.ycombinator.com/invited https://news.ycombinator.com/invited as reported by the unofficial https://github.com/minimaxir/hacker-news-undocumented https://github.com/minimaxir/hacker-news-undocumented
- besnn00 5y agoreposting is permitted while unnoticed if not done too often (once in two weeks would be ok imo)
- Miiko 5y agoThe random idiom I got was: > Idiom #120 Read integer from stdin > Read an integer value from the standard input into variable n int n[15]; fgets(n, 15, stdin); Really?
- akomtu 5y agoYes. You asked for "an integer" from stdin? Here's your integer. Specify constraints better next time. (That's probably how the upcoming AI-assisted code gen tools will look like).
- WJW 5y agoThe longer I look at this example, the more weirdness I spot: - There are no standard integer types that take 15 (decimal) digits to represent. - The array contains ints instead of chars - Why would you use fgets() instead of just gets()? (Though I don't touch C very often so perhaps that is considered proper style) - Obviously no conversion of the digits into else, let alone specifying a base or handling a `0x` prefix for hexadecimal or a minus sign for negative numbers.
- leetcrew 5y agogets is inherently unsafe unless the input is guaranteed (externally) to never overflow the buffer.
- jhgb 5y ago> Why would you use fgets() instead of just gets()? I assume it's because gets() ranks as "-10: It's impossible to get right" on Rusty's API Design Manifesto? (http://sweng.the-davies.net/Home/rustys-api-design-manifesto http://sweng.the-davies.net/Home/rustys-api-design-manifesto)
- WJW 5y agoTIL. I have been leading a sheltered life in languages with garbage collectors that obscured the true horror of gets() from me.
- mpweiher 5y agoman gets ... SECURITY CONSIDERATIONS The gets() function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program's func- tionality through a buffer overflow attack. It is strongly suggested that the fgets() function be used in all cases. (See the FSA.)
- worik 5y agoNo Swift? I like these projects. Fun
- nearmuse 5y agoPeople make these for autodidactic purposes, you can't expect them to always be interesting or even meet any expectations.
- val_deleplace 5y agoYou may upvote the Swift request here: https://github.com/Deleplace/programming-idioms/issues/30 https://github.com/Deleplace/programming-idioms/issues/30
- maxk42 5y agoSome of the examples don't do what the idiom says. Some aren't even close to good answers. Too many to rely upon this.
- val_deleplace 5y agoYep! Curation is though.
- wmu 5y agoThere's a similar project http://rosettacode.org/wiki/Rosetta_Code http://rosettacode.org/wiki/Rosetta_Code.
- diogenesjunior 5y agoI made something similar myself, although it's only printing. https://github.com/FormerlyChucks/jello https://github.com/FormerlyChucks/jello
- PrincessJas 5y agoYour github username is a nazi, alt-right dogwhistle. Mods, please handle this.
- thom 5y agoSurprisingly many failures of reading comprehension in the implementations here: https://programming-idioms.org/idiom/184/tomorrow https://programming-idioms.org/idiom/184/tomorrow I’ve always found it interesting to consider the simplest possible spec you could give 100 programmers and receive no bugs in return.
- ghoward 5y ago> I’ve always found it interesting to consider the simplest possible spec you could give 100 programmers and receive no bugs in return. There is only one or two https://pubs.opengroup.org/onlinepubs/9699919799/utilities/true.html#top https://pubs.opengroup.org/onlinepubs/9699919799/utilities/t... and maybe https://pubs.opengroup.org/onlinepubs/9699919799/utilities/false.html#top https://pubs.opengroup.org/onlinepubs/9699919799/utilities/f... . Even "Hello, World!" is done wrong more often than not. An example: you should check for errors from `printf()`, as in [1]. [1]: https://stackoverflow.com/questions/12355758/proper-hello-world-in-c/12355781#12355781 https://stackoverflow.com/questions/12355758/proper-hello-wo...
- remexre 5y agoNot even both of those; I thought there famously existed some way to make GNU true return non-zero. EDIT: Yep, https://github.com/coreutils/coreutils/blob/master/src/true.c#L67-L69 https://github.com/coreutils/coreutils/blob/master/src/true....
- ghoward 5y agoHa! So it is impossible! Seriously, though, thank you for the link. That is good to know!
- Someone 5y agoBut that’s a complex program, with command-line parsing, locale usage, etc. No wonder it has bugs :-) https://en.wikipedia.org/wiki/IEFBR14 https://en.wikipedia.org/wiki/IEFBR14 was a one-byte program, but that had a bug, so it had to be doubled in size. And yes, it still exists (https://www.ibm.com/docs/en/zos-basic-skills?topic=utilities-iefbr14-utility-do-almost-nothing https://www.ibm.com/docs/en/zos-basic-skills?topic=utilities...). I wouldn’t know how large it is nowadays.
- blunte 5y agoI see a lot of people taking issue with the idioms presented, and rightfully so in many cases. Add the ability for people to improve or debate the solutions. Ultimately we should have a large curated cookbook (with additional variant selections and associated recipe variants). The most important human element of programming is knowing what to build (and what pieces to build to make the bigger thing). How often do I have to lookup ways to read a file in Ruby?... most times I need to read a file, I have to refer to the different approaches. I just don't do that often enough to remember everything. What I do know is, "this will be a lot of data, so I need to read it line by line or in chunks". That should be all you need to know, and then you pull up a recipe.
- tialaramex 5y agoFor the languages that have considerable feature velocity (so let's say I'll include even C++ and Python there but not C) you'd want to have a way to update idioms to match the current language. You might in some cases want to split out a previously idiomatic solution too. For example IntoIterator wasn't defined for Rust arrays, and then one day Rust 1.54 implemented IntoIterator on arrays (and hacked things so that this doesn't cause old code to do something unexpected, Rust 2021 will remove that hack since old code wouldn't claim to be Rust 2021, thereby unlocking the new behaviour for syntax that once meant something different). Anyway this changes the idiomatic way to do various trivial array operations, since previously you'd have created a reference to the array, that has IntoIterator so that you can iterate it. That still works, but it's no longer idiomatic. for &x in &[2, 4, 6, 8, 10] /* needed until 1.54 */ for x in [2, 4, 6, 8, 10] /* makes more sense */
- throwaway81523 5y ago> Ultimately we should have a large curated cookbook (with additional variant selections and associated recipe variants). We have that, rosettacode.org .
- val_deleplace 5y agoIt's already possible and encouraged to improve the solutions. The "edit" button shows up on computer browsers, not on mobile if the screen is small. There's no "debate" capabilities, though.
- kukx 5y agoI found out that a good way to learn idioms for a given language is to do some simple katas on codewars.com and then review the most upvoted solutions.
- xorvoid 5y agoNot so much idioms as much as APIs imho
- w1nter 5y agoTIL many programming languages have built-in support for complex types (e.g. 2i in Ruby, 2j in Python) https://programming-idioms.org/idiom/77/complex-number https://programming-idioms.org/idiom/77/complex-number
- copperkw 5y agoA project with similar goal I worked on with others a few years ago https://101wiki.softlang.org/ https://101wiki.softlang.org/
- twodave 5y agoAm I the only one bothered that many of these implementations of idioms are often not idiomatic according to the language? e.g. in C# the idiomatic way to instantiate something is typically to use the var keyword (unless the type of its value can't be inferred). And the idiomatic way to format a string is to use the $ special character, not string.Format... I suspect other languages
- gwbas1c 5y agoC# has evolved quite a bit over 20ish years. I suspect those posts predate $. Also: some people really oppose "var." I don't understand why, especially when newer languages like Swift and Rust really encourage similar idioms. Something something = new Something () is usually a smell to me. I personally find "var" much easier to work with.
- wyldfire 5y ago> Something something = new Something () is usually a smell to me. var something = new Something() ... this one in C# is probably using type inference? Or maybe it doesn't matter in C# because it has a dynamic type anyways? Yes, Rust permits type inference but sometimes it can't infer it and you have to specify it. And sometimes it's clearer to be explicit about the type. C++ permits it too (auto) but depending on the team it can be pretty unpopular except for maybe verbose type names/iterator types, etc.
- hvidgaard 5y agoThe compiler can almost trivially get the type. As long as the type on the right side is resolved it's a single pass to resolve the type of the left side. Only rarely is the compiler unable to and you will get an error and have to provide it yourself.
- Thiez 5y agoThese days `Something something = new();` is also allowed.
- svnpenn 5y agoI have a similar site here https://89z.github.io/autumn https://89z.github.io/autumn
- deleted 5y ago[deleted]
- apples_oranges 5y agoNSString *s=@(i).description; is not how I would convert an integer to string in objective-c!