Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Emulous: Come check out Emulous: [link]
      Today at 03:37:19 PM
    • Emulous:[link]
      Today at 03:37:00 PM
    • Travas: ur mom
      July 21, 2018, 06:04:50 PM
    • YNRDAB: Watcha big ballers up too?
      July 20, 2018, 11:01:06 AM
    • dark250: i need help for coding client
      July 19, 2018, 08:27:37 AM
    • Travas: yes
      July 14, 2018, 02:19:44 PM
    • drubrkletern: lmao racist and sexist much?
      July 14, 2018, 01:11:52 PM
    • Cole1497: yeah i got you lil boat give me a second
      July 13, 2018, 03:19:35 AM
    • B0ATY.: yo can anyone unban my ip, i got banned for saying i hope someone gets aids :p
      July 12, 2018, 09:16:00 PM
    • jme: ziplock this domain belongs to the kremlin now
      July 12, 2018, 06:54:31 PM
    • Pure_: lol
      July 12, 2018, 01:37:47 PM
    • ziplock: wtf is this forum? Haven't checked in in 4 years lmao
      July 11, 2018, 10:26:03 AM
    • ziplock: Idk
      July 11, 2018, 10:25:41 AM
    • _^_: guys is anyone still even here
      July 11, 2018, 01:54:43 AM
    • Travas: gay
      July 08, 2018, 02:00:14 PM
    • CheapOnlineHost: Hosting starting at $0.75 hostcheap.io
      July 08, 2018, 09:35:20 AM
    • q1w1e1r1t1y: is the server actually updating or is it down?? trying to log on for the first time in a few months and it says "server updating try again in 1 minute" for the last 10 minutes now
      July 07, 2018, 11:11:51 PM
    • Cole1497: gamers unite
      July 07, 2018, 04:26:07 PM
    • IamLation: LMFAO @cole
      July 06, 2018, 10:36:52 AM
    • drubrkletern: olfihsouhdiu
      July 04, 2018, 04:25:32 PM

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