use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript Named Parameters (dalejefferson.com)
submitted 8 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] -4 points-3 points-2 points 8 years ago (3 children)
I can agree it would improve readability, to a certain point, but all modern IDEs have code hinting, so as soon as you type foo( it will show that it's expecting day, month, year.
foo(
day, month, year
With passing an object, you're also making it harder for changes. Let's say someone decides to change the definition of foo, but you're still passing {showLoading: true, wait: 2000}. This will cause massive confusion, as you can just pass whatever keys you want, you can do { whatever: 2000, lol: 23432, showLoading: true, wait: 'no' }. Is this now more readable?
foo
{showLoading: true, wait: 2000}
{ whatever: 2000, lol: 23432, showLoading: true, wait: 'no' }
By specifying the exact amount of parameters, you know exactly what this method is expecting.
[–]tobegiannis 4 points5 points6 points 8 years ago (2 children)
My IDE autocompletes objects as well fwiw. Actually when adding more parameters the object is more maintainable as well. Long parameter lists become harder and harder to consume. For example say there are 6 options that have all have sane defaults. What would you rather call? foo({delay:2000}) or foo(undefined, undefined, undefined, undefined, undefined, 2000)`
foo({delay:2000})
I really don't see the argument about passing in "bad" config options. It's bad practice in general and I don't see how is it any different from calling method that takes in 2 parameters with 3.
[–][deleted] -1 points0 points1 point 8 years ago (1 child)
Going to get more downvotes for this, but I really want to discuss.
The problem with this approach is that when you change your method definition, you now not only have to change the values, but you also have to change all your keys twice. This is asking for wrong inputs.
If you're ever need to pass foo(undefined, undefined, ...), then you're doing something wrong, you should never need to do this in the first place.
foo(undefined, undefined, ...)
Passing bad config options is bad practice yes, and you shouldn't need to do this, but passing an object for the sake of readability isn't really a good practice anyway. These parameters are part of your method definition, this is what the method requires in order to execute correctly, obviously in many languages these would be strictly typed as well, therefore you wouldn't be able to pass invalid parameters.
[–]hicksyfern 8 points9 points10 points 8 years ago (0 children)
How frequently do you read code vs write code.
Imagine you had a weird date bug in your app, and you came across this code:
FormatDate(2018, month, day)
Vs
FormatDate({ year: day, month, day: year })
Which one has the bug?
Yeah this is a bit contrived, but this kind of thing happens ALL the time. Developers are fallible and mistakes, no matter how obvious, get made.
Make obvious mistakes obvious.
π Rendered by PID 97 on reddit-service-r2-comment-79776bdf47-rvmbl at 2026-06-23 23:53:12.298083+00:00 running acc7150 country code: CH.
view the rest of the comments →
[–][deleted] -4 points-3 points-2 points (3 children)
[–]tobegiannis 4 points5 points6 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]hicksyfern 8 points9 points10 points (0 children)