www.webdeveloper.com
Results 1 to 4 of 4

Thread: js, my bad grammar

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Question js, my bad grammar

    Hi all,
    when writing in js I use a lot of nested if statements ...and I mean a LOT.
    I will nest if statements in other if statements more than 20 times in a row.

    People say that this is not normal but I don't know any other way.

    Example
    Sorry about this, giving an example feels kind of patronising (we all know what an if statement looks like) but if I don't give an example I know someone will ask for one, so here we go...

    Lets pretend that I have a function that receives 'foo', a variable that could be any number from 1 to 100.
    Receiving foo I now need to do 'something' that is specific to that number, this 'something' is not identical to the same thing that would need to be done for any other number.

    The way I have been handling this so far is by nesting ifs like this...

    Code:
    if(foo<50)
    {
    
    if(foo<25) {
    if(foo<12) {
    if(foo<6) {
    if(foo<3) {
    if(foo==1) {
    //run foo = 1
    } else {
    //run foo = 2
    }
    } else {
    ...etc...etc
    }
    } else {
    ...etc...etc
    }
    } else {
    ...etc...etc
    }
    } else {
    ...etc...etc
    }
    } else {
    ...etc...etc
    }
    Now if you imagine that all of the parts in the example that say ...etc...etc are actually continuing with the pattern you will start to understand my problem.

    I know about the JavaScript Switch Statement but it's kind of the some thing, I think Switch Statements look even worse. Also using the else area that comes with the if statement is much more useful.

    Is there a better solution?
    Any feed back appreciated.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,290
    For a start, stop using the foo & often accompanied bar names, it is taken from fubar and if you don't know what that is, google it.

    Your if() conditionals can be nested, generally the less you can avoid deep nests the better.

    You need to be limiting your testing scope or adding in range testing, how you achieve this is down to a number of factors like what the code is meant to do, sometimes a loop is needed, until you can produce a live chunk of code that you need help with, no end of example scripts can provide anyone here with a way of advising you on what you need to use to achieve or overcome the issue you are experiencing.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    I know a style that not everyone will agree on, but an example of how I might have handled the code above:

    Code:
    if (foo === 1) { /* ETC ETC */ } else
    if (foo === 2) { /* ETC ETC */ } else
    if (foo === 3) { /* ETC ETC */ } else
    if (foo <   6) { /* ETC ETC */ } else
    if (foo <  12) { /* ETC ETC */ } else
    if (foo <  25) { /* ETC ETC */ } else
    if (foo <  50) { /* ETC ETC */ } else
                   { /* ETC ETC */ }
    Reverse checks without nesting.
    JavaScript: Learn | Validate | Compact | bionoid

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by Max121212 View Post
    I know about the JavaScript Switch Statement but it's kind of the some thing, I think Switch Statements look even worse. Also using the else area that comes with the if statement is much more useful.
    Switch statement is useful when you want to check a single expression against multiple values. It's cleaner and easier to read the code and why to force JS to repeatedly evaluate expression with multiple if statements.

    So this looks much cleaner and is easier to follow:

    Code:
    switch (number) {
        case 1:
            //some code; break;
        case 2:
        case 3:
            //some code; break;
        case 4:
        case 5:
            //some code; break;
        default:
            //some code; break;
    }
    then this:

    Code:
    if (number === 1) {
        //some code
    }
    if (number === 2) {
        if (number === 3) {
            //some code
        }
    }
    if (number === 4) {
        if (number === 5) {
            //some code
        }
    }
    else {
        //some code
    }
    Last edited by tech_soul8; 01-03-2014 at 05:06 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles