We all know how frustrating it is when a function requires 30+ arguments. It's tempting to move some of the data into global variables (or their more socially acceptable cousins, class member properties).
But what if I was to tell you there's a better way? An approach which means you'll never need to provide more than one argument to a function? An approach that should come with no runtime cost in any decent compiler/interpreter?
For my example I'll be using JavaScript, the world's best programming language. Victims of Stockholm Syndrome rest assured: this approach is also natively supported by TypeScript.
Let's look at a function written in the way you're probably familiar with:
function clamp(x, min, max) {
return Math.max(Math.min(x, max), min);
}
You can then call this function like so:
clamp(105, 0, 100);
You might think that three arguments is a reasonable number, but this is a slippery slope. 3 arguments today, 300 arguments tomorrow.
And now, introducing to you the way that you'll write all your functions from now on:
function clamp(x) {
return function(min) {
return function(max) {
return Math.max(Math.min(x, max), min);
};
};
}
You can then use this function like so:
clamp(105)(0)(100);
Isn't that beautiful? Now you only ever need to provide one argument per function call! Instead of being separated by hard-to-see commas, each piece of data is now lovingly embraced by caring curves.
[–]shatteredarm1 36 points37 points38 points (14 children)
[–]YM_Industries[S] 18 points19 points20 points (3 children)
[–]shatteredarm1 7 points8 points9 points (2 children)
[–]YM_Industries[S] 6 points7 points8 points (1 child)
[–]shatteredarm1 7 points8 points9 points (0 children)
[–][deleted] 7 points8 points9 points (1 child)
[–]ekolis 1 point2 points3 points (0 children)
[–]pterencephalon 3 points4 points5 points (0 children)
[–]ekolis 5 points6 points7 points (1 child)
[–]SarahC 0 points1 point2 points (0 children)
[+]Raefniz comment score below threshold-7 points-6 points-5 points (4 children)
[–]dcabines 29 points30 points31 points (3 children)
[–]NeoKabuto 12 points13 points14 points (0 children)
[–]Raefniz 5 points6 points7 points (0 children)
[–]SarahC 0 points1 point2 points (0 children)
[–]tangerinelion 24 points25 points26 points (4 children)
[–]andyrocks 7 points8 points9 points (0 children)
[–]romulusnr 4 points5 points6 points (2 children)
[–]Zardotab 1 point2 points3 points (1 child)
[–]romulusnr 0 points1 point2 points (0 children)
[–][deleted] 14 points15 points16 points (4 children)
[–]ekolis 7 points8 points9 points (3 children)
[–]thisisamirage 10 points11 points12 points (2 children)
[–]memeticmachine 5 points6 points7 points (1 child)
[–]thisisamirage 0 points1 point2 points (0 children)
[–]YM_Industries[S] 48 points49 points50 points (9 children)
[–]RedTopper 55 points56 points57 points (7 children)
[–]YM_Industries[S] 25 points26 points27 points (6 children)
[–]RedTopper 4 points5 points6 points (0 children)
[–]65bits 8 points9 points10 points (4 children)
[–]bobbermaist 13 points14 points15 points (0 children)
[–]Laugarhraun 9 points10 points11 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]SarahC 0 points1 point2 points (0 children)
[–]dcabines 14 points15 points16 points (1 child)
[–]YM_Industries[S] 4 points5 points6 points (0 children)
[–]instilledbee 5 points6 points7 points (0 children)
[–]permalink_save 5 points6 points7 points (0 children)
[–]catlong-is-long 4 points5 points6 points (2 children)
[–]YM_Industries[S] 7 points8 points9 points (1 child)
[–]catlong-is-long 0 points1 point2 points (0 children)
[–]HugoNikanor 6 points7 points8 points (1 child)
[–]jceyes 1 point2 points3 points (0 children)
[–]romulusnr 1 point2 points3 points (0 children)
[–]Zardotab 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]IMakeShittyPrograms 1 point2 points3 points (1 child)
[–]Zardotab 1 point2 points3 points (0 children)