Click here to Skip to main content
15,902,275 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: goto statement Pin
altomaltes10-Nov-13 20:23
professionalaltomaltes10-Nov-13 20:23 
GeneralRe: goto statement Pin
PIEBALDconsult10-Nov-13 5:04
mvePIEBALDconsult10-Nov-13 5:04 
GeneralRe: goto statement Pin
CPallini10-Nov-13 6:13
mveCPallini10-Nov-13 6:13 
GeneralRe: goto statement Pin
  Forogar  10-Nov-13 6:23
professional  Forogar  10-Nov-13 6:23 
GeneralRe: goto statement Pin
CPallini10-Nov-13 6:30
mveCPallini10-Nov-13 6:30 
GeneralRe: goto statement Pin
Bill_Hallahan10-Nov-13 6:31
Bill_Hallahan10-Nov-13 6:31 
GeneralRe: goto statement Pin
  Forogar  10-Nov-13 6:51
professional  Forogar  10-Nov-13 6:51 
GeneralRe: goto statement Pin
Bill_Hallahan10-Nov-13 7:05
Bill_Hallahan10-Nov-13 7:05 
That code is you showed is fine, and I also prefer the the nested form of error checking when it is manageable. That isn't always the case. (Note the comment in my last message, there is no "silver bullet!")

As I wrote in my code example, "That construct avoids the extreme indenting that can occur with a lot of nested error checks." The key words are "can" and "nesting". Deep nesting cannot always be easily avoided by breaking into functions, and when that is the case, the loop construct is useful.

For the example below I used the nested form to compare 8 keys, where the values of the 8 keys form a single key to a dictionary (or a map). Each item in the map is sorted in lexicographic order. (This code snippet is taken from the article Generic Sparse Array and Sparse Matrices in C#[^]

C#
    public int CompareTo(ComparableTuple8<TItem0, TItem1, TItem2, TItem3, TItem4, TItem5, TItem6, TItem7> group)
    {
        int result = this.Item0.CompareTo(group.Item0);

        if (result == 0)
        {
            result = this.Item1.CompareTo(group.Item1);

            if (result == 0)
            {
                result = this.Item2.CompareTo(group.Item2);

                if (result == 0)
                {
                    result = this.Item3.CompareTo(group.Item3);

                    if (result == 0)
                    {
                        result = this.Item4.CompareTo(group.Item4);

                        if (result == 0)
                        {
                            result = this.Item5.CompareTo(group.Item5);

                            if (result == 0)
                            {
                                result = this.Item6.CompareTo(group.Item6);

                                if (result == 0)
                                {
                                    result = this.Item7.CompareTo(group.Item7);
                                }
                            }
                        }
                    }
                }
            }
        }

        return result;
    }

    #endregion
}


That is 8 keys. Now, imagine there were 16 keys. The nested form would require extreme indentation and it would be more difficult to read.

To break the code up into more comparison functions would be both inefficient and confusing. There could be a function that compared 16 keys that called a function that compared 15 keys, that called a function that compared 14 keys, etc., or some other breakdown such the the function that took 16 keys did four comparisons and called a function that did 12 key comparisons, that called a function that did 8 key comparisons, etc.. Those solutions are not good solutions.

An array could be used to to store the keys, and the comparison function could loop over they keys comparing them, however, that is less efficient, and in this case, look-up time is critical. Furthermore, not all keys necessarily derive from a common type, so the array could not be strongly typed. This would be particularly bad for a generic container.

For 16 keys, the loop form would be desirable, although of course the break statements would execute as the result of a comparison, not an error.

modified 10-Nov-13 13:52pm.

GeneralRe: goto statement Pin
Rob Grainger10-Nov-13 22:50
Rob Grainger10-Nov-13 22:50 
GeneralRe: goto statement Pin
Bill_Hallahan11-Nov-13 5:05
Bill_Hallahan11-Nov-13 5:05 
GeneralRe: goto statement Pin
Rob Grainger11-Nov-13 8:28
Rob Grainger11-Nov-13 8:28 
GeneralRe: goto statement Pin
Bill_Hallahan11-Nov-13 13:51
Bill_Hallahan11-Nov-13 13:51 
GeneralRe: goto statement Pin
Rob Grainger12-Nov-13 2:00
Rob Grainger12-Nov-13 2:00 
GeneralRe: goto statement Pin
Tarek Elqusi10-Nov-13 6:53
professionalTarek Elqusi10-Nov-13 6:53 
GeneralRe: goto statement Pin
Marc Clifton10-Nov-13 9:58
mvaMarc Clifton10-Nov-13 9:58 
GeneralRe: goto statement Pin
Joe Woodbury10-Nov-13 10:14
professionalJoe Woodbury10-Nov-13 10:14 
GeneralRe: goto statement Pin
Christian Graus10-Nov-13 12:01
protectorChristian Graus10-Nov-13 12:01 
GeneralRe: goto statement Pin
Member 1008817110-Nov-13 12:55
Member 1008817110-Nov-13 12:55 
GeneralRe: goto statement Pin
Stefan_Lang11-Nov-13 1:42
Stefan_Lang11-Nov-13 1:42 
GeneralRe: goto statement Pin
vl211-Nov-13 8:01
vl211-Nov-13 8:01 
GeneralRe: goto statement Pin
Stefan_Lang12-Nov-13 1:15
Stefan_Lang12-Nov-13 1:15 
GeneralRe: goto statement Pin
vl212-Nov-13 1:30
vl212-Nov-13 1:30 
GeneralRe: goto statement Pin
werinus12-Nov-13 1:34
werinus12-Nov-13 1:34 
GeneralRe: goto statement Pin
vl212-Nov-13 1:45
vl212-Nov-13 1:45 
GeneralRe: goto statement Pin
werinus12-Nov-13 2:36
werinus12-Nov-13 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.