home.social

#guardclauses โ€” Public Fediverse posts

Live and recent posts from across the Fediverse tagged #guardclauses, aggregated by home.social.

  1. I think that the `if` block of an if/else statement should check for positive "happy path" conditions and the else blocks should handle exceptions.

    bad:

    if (!isExpected) handleUnexpected();
    else handleExpected();

    good:

    if (isExpected) handleExpected();
    else handleUnexpected();

    Especially if you drop the `else`; like with #guardClauses or #earlyReturns.

    It makes the #code harder to read and fold in the IDE; saving a level of indentation is not worth it!

    #javaScript #programming #webDev

  2. I think that the `if` block of an if/else statement should check for positive "happy path" conditions and the else blocks should handle exceptions.

    bad:

    if (!isExpected) handleUnexpected();
    else handleExpected();

    good:

    if (isExpected) handleExpected();
    else handleUnexpected();

    Especially if you drop the `else`; like with #guardClauses or #earlyReturns.

    It makes the #code harder to read and fold in the IDE; saving a level of indentation is not worth it!

    #javaScript #programming #webDev

  3. I think that the `if` block of an if/else statement should check for positive "happy path" conditions and the else blocks should handle exceptions.

    bad:

    if (!isExpected) handleUnexpected();
    else handleExpected();

    good:

    if (isExpected) handleExpected();
    else handleUnexpected();

    Especially if you drop the `else`; like with #guardClauses or #earlyReturns.

    It makes the #code harder to read and fold in the IDE; saving a level of indentation is not worth it!

    #javaScript #programming #webDev

  4. I think that the `if` block of an if/else statement should check for positive "happy path" conditions and the else blocks should handle exceptions.

    bad:

    if (!isExpected) handleUnexpected();
    else handleExpected();

    good:

    if (isExpected) handleExpected();
    else handleUnexpected();

    Especially if you drop the `else`; like with or .

    It makes the harder to read and fold in the IDE; saving a level of indentation is not worth it!

  5. I think that the `if` block of an if/else statement should check for positive "happy path" conditions and the else blocks should handle exceptions.

    bad:

    if (!isExpected) handleUnexpected();
    else handleExpected();

    good:

    if (isExpected) handleExpected();
    else handleUnexpected();

    Especially if you drop the `else`; like with #guardClauses or #earlyReturns.

    It makes the #code harder to read and fold in the IDE; saving a level of indentation is not worth it!

    #javaScript #programming #webDev

  6. Unless the #programming #language #compiler recognizes declarative predicates and flattens them into if-then-else structures (which turn into if-goto assembler), this could lead to a huge stack full of procedures that are just idling about, waiting for a nested procedure to get released.

    It's the exact opposite of #guardClauses which short-circuit execution by failing fast, and return to their caller quickly, freeing that stack frame instantly.