4 ms·
I can't give you a hard reason. I guess when I learned C all examples were like c=yesno?1:2. So I guess I stuck to it and never thought about triggering an acti
by maxxxxx 9y ago
I can't give you a hard reason. I guess when I learned C all examples were like c=yesno?1:2. So I guess I stuck to it and never thought about triggering an action with side effects that way.
- TeMPOraL 9y agoI see. So as an example of doing both, consider following Java pseudocode (simples example that comes to my mind now): Foo value = someCondition() ? map1.computeIfAbsent("key", defaultValueProvider) : map2.computeIfAbsent("key", defaultValueProvider); This both gets a value and changes state of the map; which map is queried/altered is determined by the condition. I personally don't mind such code, and prefer it to if/else version.
- Mithaldu 9y agoShouldn't that be: Foo value = ( someCondition() ? map1 : map2 ).computeIfAbsent("key", defaultValueProvider); Or some kind of moral equivalent?
- TeMPOraL 9y agoCould be, though this is an artifact of a simplified example that I didn't mean to be relevant here. For clarity, let's rewrite it as: Foo value = someCondition() ? map1.computeIfAbsent("key", defaultValueProvider) : list1.get(42);
- Mithaldu 9y agoHonest answer. :) Rephrased: There is no reason. It's just tradition for you.
- maxxxxx 9y agoI have been doing this for a while and I am trying to keep things simple and consistent now. I used to do "elegant" code that used a ton of language features but now my PHP or C# look almost the same.