locked
Should we use else in else if condition RRS feed

  • Question

  • Hi

    Wanted to ask from experts is that whether its a good practise to use else in the mutiple else if condition. 

    My code is something like this

    if()

    { }

    else if()

    {}

    else if()

    {}

    Now in this case should i introduce a else block where i need to throw error or is it ok to leave like that.

    FYI i have a separate private method where we do all the validations and all the errors are thrown there only. 

    So wanted to check with you guys whether its a good practice to write a else block and throw the error there explicitly or its ok with my current implementation.

    • Moved by CoolDadTxModerator Friday, June 8, 2012 2:02 PM Language related (From:Visual C# General)
    Friday, June 8, 2012 11:32 AM

Answers

  • Sure. If you need it. There is nothing wrong with it. About showing error (or some kind of exception) in the last "block" - in else, it up to you. You can if you want (need) it.

    Last else, it only used in cases that you didnt cover in if and if else blocks. So if you think there might come something unexpected, like error, then use else as last block.

    ----

    To think: to avoid if else blocks, you can think of rather using switch statement, with cases.


    Mitja


    Friday, June 8, 2012 11:56 AM
  • Hi,

    This is very specific to the situation. If you initialize an object in each if-statement, it might be a good idea to incorporate an else initializing the object to its default value or to null. An else is actually nothing else than an 'else if (all situations we haven't checked earlier)'.
    If you check two different situations, and your code doesn't have to do anything in any other situations, just leave it at that.

    If it is an error state for your application when none of your ifs get hit, show an exception. If this is 'to be expected': skip it.

    By the way: beware of the fact that a long list of if - else if statements might indicate you could do this easier with a switch.

    Hope this helps


    Mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Friday, June 8, 2012 12:01 PM

All replies

  • Sure. If you need it. There is nothing wrong with it. About showing error (or some kind of exception) in the last "block" - in else, it up to you. You can if you want (need) it.

    Last else, it only used in cases that you didnt cover in if and if else blocks. So if you think there might come something unexpected, like error, then use else as last block.

    ----

    To think: to avoid if else blocks, you can think of rather using switch statement, with cases.


    Mitja


    Friday, June 8, 2012 11:56 AM
  • Hi,

    This is very specific to the situation. If you initialize an object in each if-statement, it might be a good idea to incorporate an else initializing the object to its default value or to null. An else is actually nothing else than an 'else if (all situations we haven't checked earlier)'.
    If you check two different situations, and your code doesn't have to do anything in any other situations, just leave it at that.

    If it is an error state for your application when none of your ifs get hit, show an exception. If this is 'to be expected': skip it.

    By the way: beware of the fact that a long list of if - else if statements might indicate you could do this easier with a switch.

    Hope this helps


    Mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Friday, June 8, 2012 12:01 PM
  • Once you dial-in to this article (which will take a few reads and tests in your lab) you may be able to write code without if-then statements.

    http://www.codeproject.com/Articles/12508/Dude-are-you-still-programming-using-if-then-else


    JP Cowboy Coders Unite!

    Friday, June 8, 2012 12:08 PM
  • Love your link Mr JavamanII!

    Definately can code smarter using patterns or even if you end up finding your else statements are depending on what type an object is just use polymorphism.

    The last else statement would be the default code that is run, this is more readable/obvious when using a case statement - which is useful if you have lots of conditions tyou want to check against:

     

    switch(status)             {                 caseStatus.Active : //do something herebreak;                 caseStatus.InActive: //do something herebreak;                 caseStatus.Waiting: //do something herebreak;                 caseStatus.Pending: //do something herebreak;                 default//do default something herebreak;    }

    The above uses a made up Enum called Status.  To use case statement which is more like a series of "if's":

     string status = "This is correct";
     
                switch(status)
                {
                    case "This is wrong" : //do something here
                        break;
                    case "This is correct"//do something here
                        break;
                    case "No information"//do something here
                        break;
                    case ""//do something here
                        break;
                    default//do default something here
                        break;
                }

    Chris


    Friday, June 8, 2012 2:19 PM
  • Yes Chris, I think the take away from that article is that when using IF-THEN constucts we are making state machine decisions later than we could or shoud have.  I've known about that article now since it was first written 6 years ago and never really adopted it because IF-THENs are so logical when coding.  However, I did modify when I make state machine decisions by always setting state at the earliest possible moment.  Any checkbox that is checked sets the state in the changed eventhandler.  That way the state is ready to go when one gets into their IF-THEN or CASE statements.  This has worked very nice for me when I have complicated things to track.

    JP Cowboy Coders Unite!

    Friday, June 8, 2012 4:04 PM