guard clause style static if

rikki cattermole rikki at cattermole.co.nz
Sat Jul 7 11:56:40 UTC 2018


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.

"A method has conditional behavior that does not make clear what the 
normal path of execution is"

    void bar (T ...) (T args) if (T.length == 0)
    {
       return;

       writeln (args [0]);
       return bar (args [1 .. $]);
    }

    void bar (T ...) (T args) if (T.length > 0)
    {
       writeln (args [0]);
       return bar (args [1 .. $]);
    }

(you meant T I suspect, as args is a runtime thing, but T is compile 
time, just like static if).


More information about the Digitalmars-d-learn mailing list