Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Cole1497: this shout box is diarrhetic
      July 15, 2019, 04:53:27 PM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 13, 2019, 04:54:43 PM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 13, 2019, 04:34:05 PM
    • rune-list:[link] RuneScape Private Servers List & Community.
      July 12, 2019, 12:25:23 AM
    • ragnoroker: Join PoonScape today! amazing new server - growing fast! [link]
      July 11, 2019, 10:31:15 AM
    • ragnoroker: Join PoonScape today! amazing new server - growing fast! [link]
      July 11, 2019, 10:31:11 AM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 10, 2019, 05:27:05 AM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 10, 2019, 05:10:40 AM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 10, 2019, 04:43:07 AM
    • ArtexAdv: Artex-rsps,com [link] (BRAND NEW SERVER)
      July 08, 2019, 07:59:48 PM
    • ArtexAdv:[link] (BRAND NEW SERVER)
      July 08, 2019, 07:59:31 PM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 08, 2019, 07:03:33 PM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 08, 2019, 07:03:16 PM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 08, 2019, 06:59:50 PM
    • dan v jad: guys join PkOwnage today!! Great new server, raids, eco, PK! Discord [link] homepage [link]
      July 07, 2019, 10:59:48 AM
    • ragnoroker: Join PoonScape today! amazing new server - growing fast! - [link]
      July 07, 2019, 08:19:24 AM
    • ragnoroker: Join PoonScape today! amazing new server - growing fast! - [link]
      July 07, 2019, 08:19:04 AM
    • PavSwag: autoscape,0rg free ultra M box today (twisted bow??)
      July 05, 2019, 04:22:44 PM
    • PavSwag: autoscape,0rg
      July 05, 2019, 04:22:32 PM
    • PavSwag: #AS [link] is 317 Oldschool RuneScape Private Server emulating core factors of the game with a strong economy and balance of experience to bossing and prestiging - Lumbridge remade - Login with a starter set of armour and items - PK District - Preloading sets - GE PVP - Clan Wars Safe PK - PVP Tournaments - Activity points for training - All Skills Working, Skilling Island, Skilling Pets, Skilling Rewards - All Working Max Capes - 25+ Bosses with Pets - Superior Slayer Encounters - Clue Scrolls & Imbue Scrolls - Player Profiles - Player Owned Shops - Minigames, Construction, Sawmill, Warriors Guild, Pest Control, Barrows (with pets), Fight Caves, Inferno Caves, Chambers of Xeric, Custom Raids - QoL Updates: Keybinds, Skill Guides,
      July 05, 2019, 04:22:29 PM

    Author Topic: If (condition) else vs if (!condition) return.  (Read 5262 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.
    Runescape Gambling

    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
    Runescape Gambling

    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
    Runescape Gambling

    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.
    Roy132

    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,322
    • 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