Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Riskdemon:[link] Varrock OSRS RSPS releasing May 22nd! Discord link provided!
      May 18, 2019, 11:43:14 PM
    • violence.:[link] Most addictive RSPS brand new just opend free m box to every new player
      May 18, 2019, 08:07:04 PM
    • LukrativeHD: Zenyte, the most anticipated RSPS of 2019! The server has been in development for over 2 years! Zenyte will be launching on June 7, 2019! Join our huge community on discord : [link]
      May 17, 2019, 11:18:39 AM
    • SnowRSPS: **Snow-PS | Custom & Improved RSPS | FREE $5 Scroll | Daily Giveaways, Updates & MORE!** **Site:** **<https://SnowRSPS.com><https://Snowscape.net>** **Discord:** [link] ```HTTP |FREE TO PLAY| -317 loading 602 graphics -GambleBots Fully Coded -Fullscreen -Prestige -AutoTrivia -AutoDonations -Custom ::NewHome -50+ Bosses -Unique Pets -Working Skills -Daily Giveaways -10+ Websites -(SnowPS.org|Snow317.com|& MANY MORE) -Staff Needed Always -Super Friendly Community -& We like to Update Weekly HOPE YOU COME N ENJOY! ``` **Facebook:**<https://facebook.com/snowrsps2012>
      May 16, 2019, 06:36:31 PM
    • PalidinoDH:[link]
      May 14, 2019, 10:53:37 PM
    • Coder Nick: ancientisle . com new OSRS server with heaps of content! raids etc!
      May 14, 2019, 04:19:55 AM
    • aXo: Are there any "vanilla" servers for the current OSRS style? Like, servers including tutorial island and all that, without much in the way of ridiculous customs and portals
      May 12, 2019, 05:28:13 PM
    • PavSwag: BRAND NEW RSPS AUTOSCAPE,ORG FREE MBOX WHEN YOU GET 99
      May 10, 2019, 03:28:22 PM
    • Blasta: Hey im available for staff positions want my discord? robzzd#1501
      May 08, 2019, 09:29:13 PM
    • Coder Nick: Brand new rsps ancientisle . com need players and staff!
      May 07, 2019, 09:42:54 PM
    • IceDynasty: LOG IN NOW FOR FREE DONOR! 4 GUYS ON LIMAS-SCAPE
      May 07, 2019, 06:53:08 PM
    • IceDynasty: [youtube]KJNP6Mpg09A[/youtube] CHECK NEW 614 RSPS
      May 07, 2019, 06:24:21 PM
    • Smokey_:[link]
      May 04, 2019, 08:37:40 PM
    • Smokey_: New toplists with unique incentive review system that requires no external libraries in an easy to use plug n play system
      May 04, 2019, 08:37:35 PM
    • dan v jad:[link]  Join us on Discord! [link]  Real OS Server - #178 Data
      May 04, 2019, 11:44:19 AM
    • PavSwag: Try out OSRS brand new server need STAFF autoscape,0RG
      May 04, 2019, 03:48:02 AM
    • Chassy13: this site is dead
      May 01, 2019, 04:50:57 PM
    • Shadowspkin: Cant believe this sites still alive. @Gerrjat email me at [link] if you want
      May 01, 2019, 10:15:22 AM
    • Gerrjat 360: currently looking for developers!
      April 30, 2019, 01:01:37 AM
    • Smokey_: New toplist with unbreakable unspammable incentive review program: Website:  [link] Discord: [link] We have toplists for any type of service and reviews are star rating based and can leave comments (optional) and stackoverflow style help sections where questions/answers can be upvoted and downvoted and unique search options
      April 28, 2019, 06:25:00 PM

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