Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • stCky: yeah so at least my mom has a pussy
      Today at 12:29:06 AM
    • Travas: your mom is a faggot
      January 17, 2018, 08:21:57 PM
    • stCky: mom wont let me meet internet strangers
      January 17, 2018, 04:20:53 PM
    • Saltyspade10: Moparmeetupppp
      January 16, 2018, 07:07:04 PM
    • stCky: but my car is japanese
      January 16, 2018, 07:06:25 PM
    • Saltyspade10: Mopar meet up?
      January 16, 2018, 07:05:59 PM
    • stCky: pfft my rb25det cefiro will eat ur tiny 1960s v8 for breakfast kid
      January 16, 2018, 07:01:30 PM
    • Travas: fag
      January 16, 2018, 01:16:43 PM
    • Oblit: ph*
      January 16, 2018, 12:34:04 PM
    • Oblit: pk balance
      January 16, 2018, 12:33:17 PM
    • Travas: racist bigot
      January 15, 2018, 09:33:12 PM
    • stCky: wheres the fun in that you immagint synthesizer
      January 15, 2018, 09:24:14 PM
    • Travas: no trump no kkk no fascist usa
      January 15, 2018, 09:11:29 PM
    • stCky: im lit
      January 15, 2018, 07:49:10 PM
    • Travas: hi
      January 15, 2018, 07:21:21 PM
    • runerunerune: hi
      January 15, 2018, 05:44:20 AM
    • shulker: anyone selling onyx? trade me in game ill negotiate an offer for like 5 trades per person
      January 15, 2018, 12:56:09 AM
    • Travas: o shit wadup
      January 14, 2018, 11:59:49 PM
    • stCky: pass me that blunt boi u know whats up
      January 14, 2018, 06:50:41 PM
    • Travas: TITS and ASS
      January 14, 2018, 02:43:33 PM

    Author Topic: If (condition) else vs if (!condition) return.  (Read 3989 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineSnow_

    • Member
    • ***
    • Posts: 161
    • Thanks: +0/-0
      • View Profile
    If (condition) else vs if (!condition) return.
    « on: July 29, 2016, 04:19:26 AM »
    What do you guys think is the best in terms of readability?

    Code: [Select]
    void doStuff() {

    if (condition) {
    foo1();
    foo2();
    foo3();
    foo4();
    foo5();
    foo6();
    } else {
    bar1();
    bar2();
    bar3();
    }
    }

    vs

    Code: [Select]
    void doStuff() {

    if (!condition) {
    bar1();
    bar2();
    bar3();
    return;
    }

    foo1();
    foo2();
    foo3();
    foo4();
    foo5();
    foo6();
    }

    I often choose to use the second way as it enables me to avoid an excess of indentation.
    Especially when the else block is rather short compared to the if block.
    RS2Ad banner

    OfflinePure_

    • Member
    • ****
    • *
    • Posts: 4,687
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #1 on: July 29, 2016, 08:25:37 AM »
    i prefer the prior if both blocks are long, otherwise if one is long and one is short i prefer the latter
    i won the forum
    RS2Ad banner

    Offlinelurtz1230

    • Member
    • ***
    • Posts: 118
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #2 on: July 29, 2016, 11:01:16 AM »
    i prefer the prior if both blocks are long, otherwise if one is long and one is short i prefer the latter

    This
    RS2Ad banner

    OfflineLothy

    • Member
    • ****
    • *
    • *
    • Posts: 7,006
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #3 on: July 29, 2016, 04:12:42 PM »
    I often prefer the second approach for the same reason - reducing nested code (and the indentation this implies).
    But to be honest, oftentimes it doesn't really matter when you're programming on a 23 inch monitor that can display more than 200 characters per line.

    Some schools of thought expressly forbid the second approach through a 'single entry single exit' rule.
    <&Speljohan_> i wouldnt want to live in a society where Mopman isnt monitored 24/7

    Offlinerunescape sucks

    • Member
    • ****
    • Posts: 2,797
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #4 on: July 29, 2016, 05:15:57 PM »
    I'm against using return and break statements really

    Against in general? Or against in similar situations? Both can be really helpful.

    I prefer the 2nd. It's just a habit at this point, and i find it easier to read.
    (click to show/hide)

    OfflineWhackatre

    • u wan buy dvd?
    • Member
    • ****
    • *
    • Posts: 1,780
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #5 on: July 29, 2016, 10:24:18 PM »
    the latter
    [20:14:10] <dicklord> your to smart :'( You make botserv bots say something you enjoy and want to go to the boards.

    If you're looking for a supreme text editor for everything look no further than Emacs.

    OfflinePure_

    • Member
    • ****
    • *
    • Posts: 4,687
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #6 on: July 30, 2016, 06:05:40 AM »
    I'm against using return and break statements really

    Against in general? Or against in similar situations? Both can be really helpful.

    I prefer the 2nd. It's just a habit at this point, and i find it easier to read.
    No, if you Google it, you'll see why.
    so you have never written a method that returns a value? since you're against return??
    i won the forum

    OfflinePure_

    • Member
    • ****
    • *
    • Posts: 4,687
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #7 on: July 30, 2016, 06:08:47 AM »
    I'm against using return and break statements really

    Against in general? Or against in similar situations? Both can be really helpful.

    I prefer the 2nd. It's just a habit at this point, and i find it easier to read.
    No, if you Google it, you'll see why.
    so you have never written a method that returns a value? since you're against return??
    No, I try to avoid just using return on its own to exit a method.
    But I'm more strict on using break statements - those things are bad!
    https://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from/118793#118793
    tldr this isnt 1980 anymore
    i won the forum

    OfflineFat Cookies

    • Moparscape Vet
    • Global Moderator
    • *****
    • *
    • Posts: 3,834
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #8 on: July 30, 2016, 09:44:46 AM »
    All depends on the context: the first one implies that you are not really that sure what the condition will evaluate to, perhaps a 50/50 split. The latter style gives a stronger impression that foo is normal behaviour and bar is some sort of exceptional circumstance such an error.





    OfflineJustin Bieber

    • Member
    • ****
    • Posts: 2,941
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #9 on: July 30, 2016, 03:42:45 PM »
    I'm against using return and break statements really

    Against in general? Or against in similar situations? Both can be really helpful.

    I prefer the 2nd. It's just a habit at this point, and i find it easier to read.
    No, if you Google it, you'll see why.
    so you have never written a method that returns a value? since you're against return??
    No, I try to avoid just using return on its own to exit a method.
    But I'm more strict on using break statements - those things are bad!
    https://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from/118793#118793
    tldr this isnt 1980 anymore
    You wouldn't even be able to use return like that in a new fancy functional language, so while all that low level detail explains the origin of the convention it's not relevant to style and readability. imo it's not good style to have stuff jumping all over the place - it's nice to be able to read a block of code and know exactly what it returns under which circumstances, without having to worry about the code 20 lines up that might return early. I suppose imperative languages don't always make this easy =/

    Offlinelare69

    • Member
    • ****
    • *
    • Posts: 5,321
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #10 on: August 17, 2016, 02:50:01 PM »
    when i program in Scala i prefer the first while in Java the latter
    hi. check out luna:)

    OfflineAmbokile

    • Member
    • ****
    • Posts: 3,009
    • Thanks: +0/-0
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #11 on: August 17, 2016, 05:21:31 PM »
    You wouldn't even be able to use return like that in a new fancy functional language, so while all that low level detail explains the origin of the convention it's not relevant to style and readability. imo it's not good style to have stuff jumping all over the place - it's nice to be able to read a block of code and know exactly what it returns under which circumstances, without having to worry about the code 20 lines up that might return early. I suppose imperative languages don't always make this easy =/

    I'd say it's probably easier to see exactly which value is being returned where in the second example.

    Code: [Select]
    function someFunction () {
        let val = 17;

        if (some_condition == true) {
            return val;
        }

        val = val + 5;
        return val;
    }

    You know that if some_condition is true, val will be immediately returned based on all the actions above that conditional.

    In this example, however, I don't think I can be quite so sure:

    Code: [Select]
    function someFunction () {
        let val;;

        if (some_condition == true) {
            val = 17;
        } else {
            val = 22;
        }
       
        return val;
    }

    You can't be sure just from reading the conditional that the code inside is what defines the return value.

    OfflineImNuckinFuts

    • First Time Poster
    • *
    • Posts: 1
    • Thanks: +10/-10
      • View Profile
    Re: If (condition) else vs if (!condition) return.
    « Reply #12 on: June 15, 2017, 12:24:59 AM »
    The latter is bad practice because if somebody else were to edit the function and wanted to add code to "the end" they'd have to find and pinpoint the return statement in the if statement rather than simply the end of the function itself.

    OfflineKByte

    • First Time Poster
    • *
    • Posts: 2
    • Thanks: +10/-10
      • View Profile
      • KomodoByte
    Re: If (condition) else vs if (!condition) return.
    « Reply #13 on: August 16, 2017, 05:20:53 PM »
    I prefer the latter because it feels cleaner. Not sure about the actual programming differences.

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal