Style-related explanation
While the first form is shorter, it would probably be depreciated by style checkers. It's also more straightforward¹. For example:
IEnumerable<TResult> LoadEverything<T1, TResult>(
IEnumerable<T1> baseSet, bool includeChecked, bool useDefaults = false,
TResult defaultValue = null)
{
}
is hard to understand, but still easier than:
IEnumerable<TResult> LoadEverything<T1, TResult>(
IEnumerable<T1> baseSet, includeChecked, useDefaults = false: bool,
TResult defaultValue = null)
{
// Does bool apply to includeChecked and useDefaults? Or maybe to baseSet too?...
}
So you gain a few seconds typing less with your approach in easy cases, but you're sure that such syntactic sugar will cost a lot in terms of time spend reading and understanding code when abused.
Parsing issues
Parsing such source code is more difficult too.
Refactoring issues
Also, changing and refactoring code may be also difficult. Taking the example above, let's ask the IDE to change the order of the parameters, by reverting baseSet
and includeChecked
. If the IDE is smart enough, it will do it correctly. If it's not, it will produce code which cannot be compiled. The same mistake can be done if you do the refactoring manually.
IEnumerable<TResult> LoadEverything<T1, TResult>(
includeChecked, IEnumerable<T1> baseSet, useDefaults = false: bool,
TResult defaultValue = null)
{
// What's wrong? Why can't this piece of code compile?
}
Typeless parameters case
Finally, if your language accepts typeless parameters, it becomes impossible to implement. The code:
array<TResult> LoadEverything<T1, TResult>(
baseSet, bool includeChecked, bool useDefaults = false,
TResult defaultValue = null)
{
var set = cast(baseSet to array<T1>);
// [...]
}
will still work, while:
array<TResult> LoadEverything<T1, TResult>(
baseSet, includeChecked, useDefaults = false: bool,
TResult defaultValue = null)
{
var set = cast(baseSet to array<T1>);
// [...]
}
makes no sense: if baseSet
is a boolean, how can it be converted to an array?
¹ By the way, you have a similar case when declaring variables of same types. A shorter form is: int a = 3, b = 5, c = 1;
, but in most cases, you would see three lines, one declaration per line, every declaration having its own type.