RE: How to write better code with Guard Clauses

avatar

You are viewing a single comment's thread:

Funny, I never knew this was called a guard clause. I seem to use them more often now than ever in this new era of love for weakly-typed languages.

stuff like

function whatever(value){
    if (isNaN(value))
        return false;
    ...
    ...
    return true;
}

I wish Javascript were more like this (at least optionally):

private function whatever(value:Number):Boolean{
    // Throws an error if passed value is not of type Number
    ...
    return true;
}

With languages like JS I spend most of my time trying to debug where the hell the issue is even coming from. I suppose some people like the flexibility of not declaring types, though. I know you can use something like TypeScript but it would be nice if one could optionally static type and run it natively in the browser.

But I digress... Nice to know what it's called a guard clause!



0
0
0.000
1 comments
avatar

I have heard it termed as the happy path. Where as you work your way down the function you return out the "un-happy" steps.

0
0
0.000