4 ms·
I think you captured definitions really well, but I'm not sure you captured the spirit of a programming idiom. The spirit is essentially pattern-like, that the
by geoelectric 7y ago
I think you captured definitions really well, but I'm not sure you captured the spirit of a programming idiom.
The spirit is essentially pattern-like, that the specific syntax has a higher meaning in that language such that as soon as you see its shape you know what's up. Moreover, if you don't see that shape it doesn't register as "native" for that language.
In your explanation, the spirit is "entry point is main." It's true the mechanics are unusual to Python, but that's not necessary and not all that counts. If I did the name/main comparison some other way it wouldn't be idiomatic because that very specific construct is the idiom.
Some examples in other languages that aren't that hard to read mechanically:
param = param || default; // idiomatic for ES5 default
let bool = !!(whatever) // idiomatic for boolean coercion in JS
reversed_list = forward_list[::-1] # idiomatic for sequence reversal in Python
x = x >> 1 // idiomatic for x/2 in C
In some of those cases there are practical realities that also drive them (the bitshift divide in particular comes from embedded and other speed concerns) but they aren't constructs that you see elsewhere very much outside that language even if they're valid. That's what makes them idiomatic to that language.
- saagarjha 7y ago> the bitshift divide in particular comes from embedded and other speed concerns Please don't do this unless you are using a really bad toolchain: most compilers will optimize x /= 2 for you.
- voltagex_ 7y agohttps://godbolt.org/z/nwk0Jm https://godbolt.org/z/nwk0Jm and https://godbolt.org/z/bGcD4Y https://godbolt.org/z/bGcD4Y show some differences - someone much better at assembly will have to explain this one.
- klipt 7y agohttps://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/ https://www.geeksforgeeks.org/left-shift-right-shift-operato... "The left shift and right shift operators should not be used for negative numbers. The result of is undefined behaviour if any of the operands is a negative number." Might be one reason?
- saagarjha 7y agoShifting by one and dividing by 2 aren't the same for signed integers, but the distinction goes away if you change the parameter to be unsigned: https://godbolt.org/z/GPJsdQ https://godbolt.org/z/GPJsdQ https://godbolt.org/z/m-rS90 https://godbolt.org/z/m-rS90
- nwallin 7y agoThe division case is optimized for size. The code for using a division instruction is smaller than the code for a shift followed by the magic to make it well behaved for negative integers. So it uses the division instruction. In general, always use -O2. -Os is only a good idea if you're on embedded and a -Os program fits in memory and -O2 doesn't. There really aren't any other cases where -Os is better.
- geoelectric 7y agoGood point! I think that's true of a lot of older "optimization" techniques--if they're that good, a compiler does them for you. I'm curious whether any compilers have specific rules to "re-optimize" obsolete practice. Like, assuming a platform does have a cheaper way to do /=2, do any compilers recognize that a >>1 in a given context is meant to be a div 2 and replace the code?
- viklove 7y agolet bool = !!(whatever) Yuck let bool = Boolean(whatever)
- geoelectric 7y agoI don't wholly disagree with you, but I see !! in the wild a lot more than Boolean() and !! is still a JS idiom even if it's not a great one.
- partyboat1586 7y agoWoah, slow down there governor. Don't you be bringing your filthy verbose Java idioms into our precious JavaScript. My father was a ECMAScript programmer and his father before him was a perl hacker. We don't take to kindly to extra unecessary characters around these parts.
- qroshan 7y agoor the first idiom that I fell in love with while (dest++ = src++) I'm sure I've botched it (but I will not google that)