r/shittyprogramming • u/YM_Industries • Aug 03 '21
Reducing function arguments
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.
49
u/YM_Industries Aug 03 '21
/uj This post was inspired by a blog post I once read which unironically encouraged this style of coding. At least I think I read it, maybe it was just a bad dream. I sure hope so.
39
u/shatteredarm1 Aug 03 '21
It's a lot cleaner just to take in a single array parameter:
15
u/dcabines Aug 03 '21
Lets turn verbs into nouns this time.
5
u/catlong-is-long Aug 04 '21
Why not
bonus points if you pass a protocol buffer, extra bonus points for XML
25
u/tangerinelion Aug 03 '21
Not where I thought this was going, well done.
I was expecting a class ClampArguments. If it were Java, I'd also expect a ClampArgumentsBuilder.
13
u/[deleted] Aug 03 '21
You've just discovered currying. It has its place.
7
u/instilledbee Aug 03 '21
Thanks I hate it
5
u/permalink_save Aug 03 '21
I thought this was going to get into abusing contexts. I've had to deal with a codebase where they just passed arguments around in this context blob, like it wasn't dealing with a web request or anything else you'd reasonably use contexts with, just a CLI program that was config and CLI arg driven. It was absolute hell trying to navigate the codebase and figure out where values came from. I ended up throwing the entire codebase away and writing a new one.
Thanks for the PTSD.
7
u/HugoNikanor Aug 03 '21
\uj This is actually how Haskell works.
For example
is just shorthand for
Difference being that haskell doesn't use parenthesis when calling the function, so it would be called as clamp 0 10 5
This allows some nice stuff, such as
2
u/jceyes Aug 03 '21
Put x last. Then arguably the partial evaluation could be useful
2
u/romulusnr Aug 03 '21
Here I was expecting closures.
I remember when a dev at a contract i once did discovered closures, and he wouldn't stop talking about them.
2
u/Zardotab Sep 16 '21
Lisp fan, eh?
2
u/[deleted] Jan 11 '22
Slightly better:
function clamp(x) {return (min)=>(max)=>Math.max(Math.min(x,max),min);}3
u/IMakeShittyPrograms Aug 03 '21
Great! I can now be shittier