7 ms·
I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think abou
by patresh 6y ago
I find that the fact that the functions min and max have the same name as the variables min and max increases cognitive load which makes it harder to think about it.
I find the following easier to read :
Math.min(Math.max(num, lower_bound), upper_bound)
- boxfire 6y agoI think this was posted purely for the limerick quality.
- hanoz 6y agoThere was a young coder whose hacks His manager often claimed lacked The requisite clarity For to clamp vars would he: Math.min(Math.max(number, min), max);
- drbacon 6y ago@jaffathecake had a problem of truncation and posted to Twitter his calculation. The gist of his attack was min( max( num, min ), max) yet refused to add any annotation.
- deleted 6y ago[deleted]
- jkaptur 6y agoIn a file of utility hacks near get(), post(), and ajax() // we alias at import // to keep all the code short min(max(number, min), max)
- gmfawcett 6y agoHaiku is nice, too: Cryptic sorcerer! "Math.min(Math.max(v, min), max);" his incantation.
- Dylan16807 6y agoThat doesn't really help. "Max" to enforce a "lower bound" is briefly halting.
- hyh1048576 6y agoI agree. But "min(-, constant_x)" should be thought of as "at most constant_x" and similarly for max. Maybe there's a way to make it more expressive.
- ncallaway 6y agoI think your "at most" language is pretty expressive. You could do that as an alias for `min` and `max` I think `at_most(at_least(num, lower_bound), upper_bound)` is much easier to understand instantly than `min(max(...))`. I'm tempted to make these aliases myself in some of my development actually. I find a pretty big conceptual difference between "I want to find the minimum point in this data", and "I want to restrict the range of this number" that giving them different names will probably help the readability of my code. (Of course, for `min(max(...))` I usually write a `clamp()` function to hide that for me, but someones I want to only clamp in one direction)
- dmurray 6y ago> (Of course, for `min(max(...))` I usually write a `clamp()` function to hide that for me, but someones I want to only clamp in one direction) You could make clamp work in only one direction too. clamp(number, None, upper_bound) or the idiomatic equivalent in your language of choice seems pretty readable.
- Symbiote 6y agoI would prefer it if the methods in java.lang.Math had been called "larger" and "smaller", instead of "min" and "max". I sometimes mix up "min" as "take the minimum" rather than "take the larger given this minimum".
- t0astbread 6y ago
- parliament32 6y agoI prefer "ceiling" and "floor", but yes, agreed.
- gugagore 6y agoThose also happen to be fairly common names for operations (rounding up or down to nearest integer).
- KONAir 6y agoI use ceil/floor (and make people use whenever I can) if something is going to happen when something hits the ceiling or drops to the floor. And avoid if it is just for clamping.
- Someone 6y agoEasy to remember, but may take some time to grasp: Arrays.sort( {lower_bound, num, upper_bound} )[1]; Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) (You can’t reduce it to the min/max call because it also works if you accidentally pass a lower bound that’s larger than the upper bound. Worst-case, the above takes 3 comparisons, unless at least two of the inputs are constants)
- chrisseaton 6y ago> Next challenge: teach the optimizer to make that almost as fast as the min/max way ;-) I did exactly this for my PhD! https://chrisseaton.com/phd/ https://chrisseaton.com/phd/
- TimTheTinker 6y agoFor those reading, this work went into TruffleRuby, which implements Ruby on top of Truffle/GraalVM.
- ford_o 6y agoWait, how is that not trivial?
- saagarjha 6y agoIt's not just a hardcoded optimization for that construct.
- chrisseaton 6y ago> how is that not trivial? It could be trivial to implement an optimisation which does this for that exact code. But what are you going to do? Hand-code an optimisation for every similar thing people could write? I implemented a general solution. So it also works through metaprogramming: [1, 2, 3].send(:sort).send(:[], 1) Through user-defined sorting order: [1, 2, 3].sort_by { |a, b| b <=> a }[1] When nested: [[1, 2].sort[1], 3].sort[0] And so on. Note that it also needs to be transparent to debuggers and profilers, it needs to handle multiple method redefinitions (for example what happens if someone redefines the sorting order for integers). It's not a pattern-matching optimization - it's partial evaluation enabled by a new kind of polymorphic inline cache.
- deleted 6y ago[deleted]
- loopz 6y agoI find the extra words wordy.
- amichal 6y agoAssuming we know (lower_bound <= upper_bound) I'd write: Math.max(lower_bound, Math.min(num, upper_bound)) Since i read right to left.
- biddlesby 6y agoIt’s amazing what a difference it makes when you just good names and formatting
- jacobolus 6y agoIt’s usually nicer to make a helper function: const clamp = (x, low, high) => Math.max(low, Math.min(x, high)); Then it can be easily used later via a descriptive name. e.g. color_component = clamp(color_component, 0, 255);
- deleted 6y ago[deleted]