4 ms·
Many folks commented on the difference between x /=2; and x = x / 2; I object to the combined operator (x /= 2) because it is ill-conceived as a langua
by JoeAltmaier 14y ago
Many folks commented on the difference between
x /=2;
and
x = x / 2;
I object to the combined operator (x /= 2) because it is ill-conceived as a language feature. It only works for a half-dozen cases the language writer chose. I can't extend it to my operations. Why would I do that? Lets assume instead we define '@' as a symbol for 'LHS' (Left-Hand-Side). It now looks like this:
x = @ / 2;
How does that add value? Imagine 'x' is '(int)(aRec[RecIndex(p->handle)]->itemCountRef)'
I know, could do that already with /= 2; But consider we wanted to do
x = x / (x+1);
No help in C. But with '@' it becomes
*(int*)(aRec[RecIndex(p->handle)]->itemCountRef) = @ / (@+1);
Much clearer than the alternative
*(int*)(aRec[RecIndex(p->handle)]->itemCountRef) = *(int*)(aRec[RecIndex(p->handle)]->itemCountRef) / (*(int*)(aRec[RecIndex(p->handle)]->itemCountRef)+1);
Sure, you can add declarations of intermediate values in C now. But I'm not sure that adds clarity in the same way.
- bonzoesc 14y ago> How does that add value? Imagine 'x' is '(int)(aRec[RecIndex(p->handle)]->itemCountRef)' That's a gnarly code smell and a Law of Demeter[1] violation. I suspect you could find a better way to express that operation without requiring that a single line of your code know so much about so many different things (aRec, aRec members, the return value of RecIndex, p, aRec member -> itemCountRef). 1: http://c2.com/cgi/wiki?LawOfDemeter http://c2.com/cgi/wiki?LawOfDemeter
- njs12345 14y agoI guess it still adds value (potentially? Maybe less so with IDEs and maybe not worth the language complexity tradeoff) if you use very long variable names, which do help somewhat with readability.
- Munksgaard 14y agoI think you're missing the point.
- bonzoesc 14y agoAm I? The language doesn't need to make affordances for bad code, bad code needs to be refactored into better code.
- JoeAltmaier 14y agoOh for heaven's sake, use some other complex reference then.
- reycharles 14y agoWhat would this mean? x = @ + (y = @+1) Would this be legal? x = 1 + (@ = @ - 1) etc...
- JoeAltmaier 14y agoBoth could have reasonable definitions. Also a serious concern; even in C simple expresions remained undefined for a decade e.g. foo(i, i++, i--) was implementation-defined for years. For that matter, in the 1st C implementation the combined-assignment operators were defined this way: i =+ 1; until they noticed it was ambiguious. C has a long history of ambiguity. So maybe its good that C was no more ambitious that it was.
- JoeAltmaier 14y agoWhy stop there? Add x = (@++? @ : @ /= 2);