Thread: if...else...finally? question about style

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    210

    if...else...finally? question about style

    I'm looking for a way to do this in a more elegant way - if possible.

    Code:
    /*bool a,b,c maybe passed in by parameters*/
    bool entered= false;
    
    if (a) {
      ... code for section a....
      entered= true;
    }
    else if (b) {
      ...code for section b....
      entered= true;
    }
    else if (c) {
      ...code for section c....
      entered= true;
    }
    
    if (entered) {
      ... common code for all sections ...
    }
    I use the above solution quite frequently in my code, but it's doesn't really look like good style to me. I know I could check for (a || b || c) in a surrounding if check, but that would double the number of checks for the compiler. Is there some better way to do this? Maybe a completely different approach? I feel like I'm missing something...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    u could try the switch statement as in using case a:....code for case a....case b:...code for case b...and so on.i think that if u have different codes for a,b,c,then u are not going wrong anywhere,not as far as i think.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    if ( a || b || c ) {
      ... common code for all sections ...
    }
    Doesn't seem unreasonable to me. Since the compiler has already tested if ( a ), I can see a good optimiser doing the right thing and eliminating the extra checks.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by Salem
    Code:
    if ( a || b || c ) {
      ... common code for all sections ...
    }
    Doesn't seem unreasonable to me. Since the compiler has already tested if ( a ), I can see a good optimiser doing the right thing and eliminating the extra checks.
    but there is also the variable specific code.Can it be bundled up into one without the different checks??

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by PING
    u could try the switch statement as in using case a:....code for case a....case b:...code for case b...and so on.i think that if u have different codes for a,b,c,then u are not going wrong anywhere,not as far as i think.
    But it's either a, b or c and always the common section. I can't see how to do that with a switch. :/

    Quote Originally Posted by Salem
    Code:
    if ( a || b || c ) {
      ... common code for all sections ...
    }
    Doesn't seem unreasonable to me. Since the compiler has already tested if ( a ), I can see a good optimiser doing the right thing and eliminating the extra checks.
    Well, if it's just an a, b and c that is fine. But if each is another function or complex statement I'd have to introduce a new boolean for each of them.

    Also even though my main intention is to tidy up that code, I did some tests with gcc and it pretty much messes the if (a || b || c) up. It ends up with a lot of jumps, performs the if (b) test twice (*), once tests if c is true and later if it is false.
    Strangely though it manages to even remove the "entered" variable from the code I posted above and replace it with unconditional jumps.... which made me think. Is this maybe a situation where goto is a suitable solution?


    [*] I thought about posting the (now commented and cleaned) asm code, but I guess a c board isn't the right place to do so?
    Last edited by Nyda; 11-23-2004 at 04:23 AM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Write whatever you consider to be the simplest and most obvious code, and let the optimiser take over.
    Micro-optimisations like this are a sure sign of focussing on the wrong thing, especially if your project is not yet complete.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    i dont think that the code can be made shorter as such.what i was getting to was that using the switch,u can make the code neater for the first 3 cases as that seems to be what you are concentrating on doing..

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Salem
    Write whatever you consider to be the simplest and most obvious code, and let the optimiser take over.
    Micro-optimisations like this are a sure sign of focussing on the wrong thing, especially if your project is not yet complete.
    Sure, I just thought there might be some clever way to write it as short, clean code without double checks or additional variables. Especially since I use it a few times throughout my code (which is mostly complete, I'm just trying to get some good style into it before adding optional stuff).

    Thanks Salem and Ping!
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  9. #9
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Quote Originally Posted by PING
    i dont think that the code can be made shorter as such.what i was getting to was that using the switch,u can make the code neater for the first 3 cases as that seems to be what you are concentrating on doing..
    The problem with this is that switch operates on a single variable, in the example these are separate variables. You might be able to combine them using bit mangling to form a integer, then use the switch on this. I tried this, but it looked pretty ugly to me.

    However I notice that the branches in your code are mutually exclusive (due the "else" statements) - so why not just do:

    Code:
    /*bool a,b,c maybe passed in by parameters*/
    
    if (a) {
      ... code for section a....
    ... common code for all sections ...
    
    }
    else if (b) {
      ...code for section b....
      ... common code for all sections ...
    }
    else if (c) {
      ...code for section c....
      ... common code for all sections ...
    }

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Jez
    However I notice that the branches in your code are mutually exclusive (due the "else" statements) - so why not just do:

    ...snip...
    Imagine "Common code" is several lines of code which depends on too many variables from the current function to export it to its own function. Also, expect there to be more options than just a, b and c.

    /me thinks there should be some kind of if..else if...[else|finally] statement :-)
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    From your descriptions I would say you should re-evalutate your algorithm.

    Though without changing any of the algorithm I would suggest something but I'd probably get shot *cough*goto*cough*

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> me thinks there should be some kind of if..else if...[else|finally] statement :-) <<

    And how do you think that would be implemented under the covers? *cough*goto*cough*

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Thantos
    Though without changing any of the algorithm I would suggest something but I'd probably get shot *cough*goto*cough*
    I also thought about that but decided that code without any jump-marks doesn't look any better after introducing the first one. And since it's going to be opensource I'd probably get shot, too... like multiple times
    I guess I'll just do what Salem said, use whatever looks best in a given situation.

    Quote Originally Posted by anonytmouse
    And how do you think that would be implemented under the covers? *cough*goto*cough*
    It would be just another unconditional jump. Or maybe just append a copy of the common code to each mutually exclusive section. No big difference to all the other statements that use jmp... i.e. while, for, else...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  14. #14
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    I thought of using goto too. Didn't want to say anything though... (call me a coward). But if it's o.k. by Thantos, then who am I to argue?

    Perhaps a gosub would be useful here, or nested function definitions with closures.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I would avoid using gotos like the plague but in may be a prettier solution. It doesn't mean that it would be any better however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. automatic type conversation style question
    By sept in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 04:28 AM
  2. [MFC] Picture box question (CStatic with SS_BITMAP style)
    By Lionel in forum Windows Programming
    Replies: 2
    Last Post: 09-29-2007, 12:13 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM