On Saturday, 7 July 2018 at 11:56:40 UTC, rikki cattermole wrote:
On 07/07/2018 11:44 PM, kdevel wrote:
On Saturday, 7 July 2018 at 11:29:35 UTC, rikki cattermole
wrote:
static if (args.length == 0)
return;
else {
writeln (args [0]);
return bar (args [1 .. $]);
}
That's not guard clause style [1][2].
[1]
https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html
[2] http://wiki.c2.com/?GuardClause
Neither was your original example.
My example was
void bar (T ...) (T args)
{
static if (args.length == 0) // 3
return; // 4
writeln (args [0]);
return bar (args [1 .. $]);
}
The guard clause is in lines 3 and 4.
"A method has conditional behavior that does not make clear
what the normal path of execution is"
In my example one immediately spots the "normal path of
execution". Your
proposal
void bar (T ...) (T args)
{
static if (args.length == 0)
return;
else {
writeln (args [0]);
return bar (args [1 .. $]);
}
}
leads to unreadable arrow code [3].
void bar (T ...) (T args) if (T.length == 0)
{
return;
[removed]
}
void bar (T ...) (T args) if (T.length > 0)
{
writeln (args [0]);
return bar (args [1 .. $]);
}
Interesting alternative but using template constraints introduces
code repetition: If you want to add another special case, say
length == 4, you have to repeat the logical complement in the
"else" branch:
void bar (T ...) (T args) if (T.length == 0)
{
return;
}
void bar (T ...) (T args) if (T.length == 4)
{
// some code
}
void bar (T ...) (T args) if (T.length > 0 && T.length != 4)
{
writeln (args [0]);
return bar (args [1 .. $]);
}
[3] https://blog.codinghorror.com/flattening-arrow-code/