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

  1. #16
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Nyda
    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 ...
    }
    Maybe
    Code:
    /*bool a,b,c maybe passed in by parameters*/
    if (a) {
      ... code for section a....
    }else if (b) {
      ...code for section b....
    }else if (c) {
      ...code for section c....
    } else 
        return;//if you're not going to do anything if these tests fail...
    
      ... common code for all sections ...

  2. #17
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.
    Oh yeah almost forgot about this: structs are your friend.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by xErath
    Maybe
    Code:
    /*bool a,b,c maybe passed in by parameters*/
    if (a) {
      ... code for section a....
    }else if (b) {
      ...code for section b....
    }else if (c) {
      ...code for section c....
    } else 
        return;//if you're not going to do anything if these tests fail...
    
      ... common code for all sections ...
    If common code was the last thing to execute that would be a neat solution, but it's certainly something to keep in mind, thanks!

    Quote Originally Posted by Thantos
    Oh yeah almost forgot about this: structs are your friend.
    Yep, I was just generalizing the problem. In any case, it's not like I got thousands of occurances of this situations. I just thought someone might know of a clever way to solve it. Thanks for everybody's suggestions!
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

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