5 ms·
I wonder if the first one was meant to be a -= a * a; which evolved from a *= a; after the author thought "I need to subtract its square from itself
by userbinator 9y ago
I wonder if the first one was meant to be
a -= a * a;
which evolved from
a *= a;
after the author thought "I need to subtract its square from itself, not just square it". I can see how this could occur in numerical code.
I've noticed there are two opposing schools of thought on issues of "insane code" like this; on one side there's "use the language to the fullest if it makes sense to", and on the other is "avoid anything that might be the slightest bit confusing". I think that taking either of those to their logical conclusion is not a good idea --- in the former case, you'll end up with extremely dense and (initially) difficult-to-understand code, but the latter case will cause a gradual degradation of code into something approaching the verbosity of Asm, but with none of the benefits (e.g. "ternary operators are confusing, don't use them; multiple levels of precedence are confusing, so always fully parenthesise expressions; nested parentheses are hard to read, so don't use them either and only do one operation per statement; boolean expressions are confusing, so always use if/else; nested if/else are confusing, so don't nest and always refactor to use a function call, etc. etc.)
It does seem that different languages vary in where they are on this scale, with more "exotic" ones like APL tending highly towards the former (interesting related discussion: https://news.ycombinator.com/item?id=13565743 https://news.ycombinator.com/item?id=13565743 ) and C#/Java towards the latter. The ideal is probably somewhere in the middle.
- lmm 9y agoIf you're not going to use it you're better off using a language without it - otherwise you pay all the costs (because you have to understand it to be able to read code, e.g. libraries you use) but get none of the benefits.
- chii 9y ago> a gradual degradation of code into something approaching the verbosity of Asm there's a simple rule of thumb - if you can't immediately and unabmiguously deskcheck the expression as it is written, it's too complicated and should be simplified.
- ipnon 9y agoPride, unfortunately, gets in the way of code being understandable instead of impressive.
- waterlink 9y agoDepends on what kind of pride we are talking about. I know developers who hold a lot of pride in how readable and understandable their code is. It is a pleasure to work with them and said code.
- metaobject 9y ago"I've noticed there are two opposing schools of thought on issues of "insane code" like this; on one side there's "use the language to the fullest if it makes sense to", and on the other is "avoid anything that might be the slightest bit confusing". This is a false dichotomy. How about "use the language to it's fullest, within reason (avoiding things like multiple side effects per source line), but when non-obvious code is written, document it." At least, I'd say that describes my approach.
- warrenm 9y agoPretty sure that's why he said, "The ideal is probably somewhere in the middle." :)
- eeZah7Ux 9y agoI had to test it - it's effectively subtracting a^2 http://rextester.com/FDFE11220 http://rextester.com/FDFE11220
- Splines 9y agoI'm of the opinion that you should express your intent as clearly as possible and let the optimizer handle making your code fast. It should be rare that writing "dense" code actually gives you performance wins.