3 ms·
Default arguments are used for sake of the DRY principle. Whenever it is highly likely that the majority of cases of application of a specific function are goin
by qwerty456127 3y ago
Default arguments are used for sake of the DRY principle. Whenever it is highly likely that the majority of cases of application of a specific function are going to use the same parameter value it can be considered reasonable to imply this parameter value as a default while allowing it to be specified explicitly whenever it make sense to tweak it. Using overloading to implement this pattern would be way more verbose, introduce unnecessary complexity and actually repeat some code.
- Detrytus 3y agoWhy would overloading repeat the code? It's typical to do it like that (in Java): public int doStuff(int a, int b) { //some code } public int doStuff(int a) { return doStuff(a, 7);} // so b=7 by default. No need to repeat actual business logic code. Of course that gets complicated if there are many parameters with default values.
- mattstir 3y agoI mean yeah, in a language without default arguments, it certainly is typical to use the only other way (overloading) to easily emulate that behaviour.
- 4hg4ufxhy 3y agoLet's be real, default arguments are most commonly used because the programmer is lazy and doesn't want to update the function call in 200 places.