Single Entry Single Exit

This is a discussion on Single Entry Single Exit within the C Programming forums, part of the General Programming Boards category; So I've been hearing about the Single Entry/Single Exit (SESE) rule, and all I can find out about it is ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

    Single Entry Single Exit

    So I've been hearing about the Single Entry/Single Exit (SESE) rule, and all I can find out about it is that it's good to have a single entry and a single exit (return) in your functions. Why is this?

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Makes the code easier to read. You won't be jumping out of the code mid-function. It's not always easier to code, but it's easier to follow.

    Code:
    int Foo(int option)
    {
       switch (option)
       {
          case 1:
             /* Do stuff */
             return 0;
          case 2:
            /* Do stuff */
            break;
          case 3:
             DoSomething();
             return 0;
       }
       return 0;
    }
    As you can see the switch statement has several cases where it exits the function. One particular case though waits to exit till the end of the function. Of course this is a stupid example but in a large function it's nice to know that there is a single exit at the end of it.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ah, I see, thanks for the response. I agree that it makes it more readable.

    So is it safe to assume that there is no technical reason behind it?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No technical reason.

    But a worse case is when you have a complex function that takes locks:
    Code:
    int func() {
       ...
       lock(something);
       ...
       if (something) {
          if ((p = malloc(somesize))== NULL)
                return -1;
       }
       ....
       ....
       unlock(something);
       return 0;
    }
    I found some code similar to this where I was working several years back - of course, the malloc returning NULL is a rare error condition, so the return was not executed particularly often - or at all - but if it happened, it would have locked the rest of the application completely.

    This is one of the cases where a goto is actually the RIGHT solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ok, I see, nice to see what you can use a goto for.

    Thanks guys!!

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    ahhh now i see the real use of goto. If we dint had goto in that case defiantly we should have exited from the main. But this give more flexible by skipping some code of they though error occurred.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shell problem
    By ArXi in forum Linux Programming
    Replies: 2
    Last Post: 06-04-2009, 03:30 AM
  2. Single destination shortest path
    By dpp in forum Game Programming
    Replies: 12
    Last Post: 05-12-2009, 12:01 AM
  3. Single task queue
    By jordanguyoflove in forum C Programming
    Replies: 2
    Last Post: 03-29-2009, 02:05 AM
  4. Grid Entry
    By porecha_jp in forum Windows Programming
    Replies: 0
    Last Post: 12-01-2002, 09:50 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21