Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. The new GOTO Statement?

The new GOTO Statement?

Scheduled Pinned Locked Moved The Lounge
questionlinqhardwarealgorithmscollaboration
82 Posts 29 Posters 80 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E ekolis

    Hmm, come to think of it, I recently discovered that in C#, you can declare a variable in one case block and use it in a subsequent case block!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #51

    Yes that has bitten me in the past, where I got compile error about multiple variables with the same name that way. It seems strange for C#, where the different cases behave much less like "just labels".

    1 Reply Last reply
    0
    • S Steve 2

      In what way is "laziness" bad, concerning typing? Who needs typing excercise? I cringe at the sight of redundancy, and mentioning the type twice in an assignment is redundant. "In my opinion var should only be used to infer anonymous types and nothing else" And how do you rationalize that? Because you think it was made for something else, and should be used only there? It's your personal taste, other than "should.. blah blah", "beginners blah blah" I've heard no sensible explanation of you. Now I don't claim to be a genious programmer, but I'm "in the field" as long as you are plus some small extra years, although not knowing every aspect of every language I need to use. Still I'm quite confident that if there was a really dangerous issue with "using var outside of LINQ", I would have noticed it, I guess I'm experienced enough for that. I just don't see any, from none here who commanted about that, just loads of personal taste statements...

      F Offline
      F Offline
      Fabio Franco
      wrote on last edited by
      #52

      Steve#2 wrote:

      Who needs typing excercise?

      Do you really think that Ctrl+C, Ctrl+V is a painful typing exercise. Better yet, do you really think that, Ctrl+C, Ctrl+V is too much for the sake of readability? Most C# people look at the beginning of the line to verify a type (or a base type/interface). Having a var there will not help at all when one is quickly sweeping the code.

      Steve#2 wrote:

      And how do you rationalize that? Because you think it was made for something else, and should be used only there?

      You don't use dynamic keyword for types known at compile type, do you? Somethings are there for a purpose, so that's what we should use them for. Do you use a hammer as a screwdriver? You could bust something open, but I'd rather not brake it while doing so.

      Steve#2 wrote:

      "beginners blah blah"

      I never said you were a beginner, but beginners do tend to abuse the keyword, specially when they are starting now, when the keyword already exists.

      Steve#2 wrote:

      I've heard no sensible explanation of you.

      I think I already made my point about why you should use tools for the jobs they were designed for, unless you don't have the right tool.

      Steve#2 wrote:

      just loads of personal taste statements...

      Even if they look personal, there are very solid reasons for that. As you should know, OriginalGriff has more experience than both of us combined, it's often wise to trust experienced people.

      To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

      S 2 Replies Last reply
      0
      • F Fabio Franco

        PIEBALDconsult wrote:

        Bullpuckey; it was made for Linq.

        I'd just like to add that it was made for Linq because Linq generates a lot of anonymous types. It would be a hell to create classes for all possible result sets.

        PIEBALDconsult wrote:

        That's exactly the scenario the using directive was made for:

        using MyDic = Dictionary<int, List<Vec3<float>>> ;
        ...
        MyDic stuff = new MyDic() ;

        (Except that generics cme later. :sigh: )

        Yeah, I'd say nothing was made for that so we do workarounds :) Or we could model our own classes that handle trickier business rules, I like to believe everyone is capable of that.

        To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

        D Offline
        D Offline
        debo01
        wrote on last edited by
        #53

        The var keyword is best suited for scenarios where the type is not known at design time as in the case of LINQ that uses anonymous types. To use it otherwise can constitute an abuse of the var keyword.

        1 Reply Last reply
        0
        • S sisnaz

          After coming down off of the "That's pretty cool" factor and as members on my team have increasingly been using anonymous methods, lambda expressions and new Func<> routines embedded in methods. The complexity (IMO) as increased significantly. I've begun to question this seemingly popular approach as; What's the difference between using embedded functions and a goto statement? It seems to me it's no different and just as difficult to follow and maintain. I'd be curious on other opinions of this practice.

          M Offline
          M Offline
          Marbry Hardin
          wrote on last edited by
          #54

          There's definitely something to be said for clarity. Not just from the aspect of someone else having to support or extend your code, but sometimes just coming back to your own code after a period. Something about a hammer and nail comes to mind.

          1 Reply Last reply
          0
          • F Fabio Franco

            Steve#2 wrote:

            Who needs typing excercise?

            Do you really think that Ctrl+C, Ctrl+V is a painful typing exercise. Better yet, do you really think that, Ctrl+C, Ctrl+V is too much for the sake of readability? Most C# people look at the beginning of the line to verify a type (or a base type/interface). Having a var there will not help at all when one is quickly sweeping the code.

            Steve#2 wrote:

            And how do you rationalize that? Because you think it was made for something else, and should be used only there?

            You don't use dynamic keyword for types known at compile type, do you? Somethings are there for a purpose, so that's what we should use them for. Do you use a hammer as a screwdriver? You could bust something open, but I'd rather not brake it while doing so.

            Steve#2 wrote:

            "beginners blah blah"

            I never said you were a beginner, but beginners do tend to abuse the keyword, specially when they are starting now, when the keyword already exists.

            Steve#2 wrote:

            I've heard no sensible explanation of you.

            I think I already made my point about why you should use tools for the jobs they were designed for, unless you don't have the right tool.

            Steve#2 wrote:

            just loads of personal taste statements...

            Even if they look personal, there are very solid reasons for that. As you should know, OriginalGriff has more experience than both of us combined, it's often wise to trust experienced people.

            To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

            S Offline
            S Offline
            Steve 2
            wrote on last edited by
            #55

            Fabio Franco wrote:

            Do you really think that Ctrl+C, Ctrl+V is a painful typing exercise. Better yet, do you really think that, Ctrl+C, Ctrl+V is too much for the sake of readability? Most C# people look at the beginning of the line to verify a type (or a base type/interface). Having a var there will not help at all when one is quickly sweeping the code.

            Ctrl+C, Ctrl+V and jumping around... come on, it still disrupts the flow and is quite a PITA. Sweeping, well, depends on how quickly, I guess, since my IDE shows the type of a variable when hovering the cursor over it, which happens to be where I read when scrolling. There are languages which don't have something like LINQ but do have automatic type inference, so a lot of people seem to disagree here what such a feature is good for or isn't. But yeah, I can see how that can be annoying - I have the problem with code where people put the opening { brace at the same line instead another one :-D I hate it when I can't identify scope by looking at the exact same column, but have to search for it in variying line widths... With my usual assignment style var use, though, the type is still quite to the left - if there is a very long variable name, I might put the type in the next line, a bit intented.

            Fabio Franco wrote:

            You don't use dynamic keyword for types known at compile type, do you?

            Huh? Var is not dynamic. Bad example? Exactly *because* the type is known at compile time, I do use var in assignments!

            Fabio Franco wrote:

            Somethings are there for a purpose, so that's what we should use them for

            That sounds quite like a priest, talking dogma, not to be questioned. If something turns out to work quite nicely for something else than original intention, why not use it? That something was not originally envisioned to be use for something, alone, is not an argument at all, for refraining from using it that way.

            Fabio Franco wrote:

            Do you use a hammer as a screwdriver? You could bust something open, but I'd rather not brake it while doing so.

            I have not seen an example of this var use busting something open, other than you personally can't read the code as quickly.

            Fabio Franco wrote:

            OriginalGriff has more experience than both of us combined, i

            F 1 Reply Last reply
            0
            • F Fabio Franco

              Steve#2 wrote:

              Who needs typing excercise?

              Do you really think that Ctrl+C, Ctrl+V is a painful typing exercise. Better yet, do you really think that, Ctrl+C, Ctrl+V is too much for the sake of readability? Most C# people look at the beginning of the line to verify a type (or a base type/interface). Having a var there will not help at all when one is quickly sweeping the code.

              Steve#2 wrote:

              And how do you rationalize that? Because you think it was made for something else, and should be used only there?

              You don't use dynamic keyword for types known at compile type, do you? Somethings are there for a purpose, so that's what we should use them for. Do you use a hammer as a screwdriver? You could bust something open, but I'd rather not brake it while doing so.

              Steve#2 wrote:

              "beginners blah blah"

              I never said you were a beginner, but beginners do tend to abuse the keyword, specially when they are starting now, when the keyword already exists.

              Steve#2 wrote:

              I've heard no sensible explanation of you.

              I think I already made my point about why you should use tools for the jobs they were designed for, unless you don't have the right tool.

              Steve#2 wrote:

              just loads of personal taste statements...

              Even if they look personal, there are very solid reasons for that. As you should know, OriginalGriff has more experience than both of us combined, it's often wise to trust experienced people.

              To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

              S Offline
              S Offline
              Steve 2
              wrote on last edited by
              #56

              Btw., I just skimmed through the C# 4.0 language specification, and it is littered with examples like:

              var contacts = new List

              Do you think the language designers would have used examples like that if they thought using the "var" keyword that way was bad, or against their very own intentions?

              1 Reply Last reply
              0
              • J jschell

                sisnaz wrote:

                What's the difference between using embedded functions and a goto statement?

                Because one is cool and the other isn't. It is of course related to the difference between understanding the syntax of a language and being able to write syntax that is maintainable.

                S Offline
                S Offline
                sisnaz
                wrote on last edited by
                #57

                Good point.

                1 Reply Last reply
                0
                • S Steve 2

                  Fabio Franco wrote:

                  Do you really think that Ctrl+C, Ctrl+V is a painful typing exercise. Better yet, do you really think that, Ctrl+C, Ctrl+V is too much for the sake of readability? Most C# people look at the beginning of the line to verify a type (or a base type/interface). Having a var there will not help at all when one is quickly sweeping the code.

                  Ctrl+C, Ctrl+V and jumping around... come on, it still disrupts the flow and is quite a PITA. Sweeping, well, depends on how quickly, I guess, since my IDE shows the type of a variable when hovering the cursor over it, which happens to be where I read when scrolling. There are languages which don't have something like LINQ but do have automatic type inference, so a lot of people seem to disagree here what such a feature is good for or isn't. But yeah, I can see how that can be annoying - I have the problem with code where people put the opening { brace at the same line instead another one :-D I hate it when I can't identify scope by looking at the exact same column, but have to search for it in variying line widths... With my usual assignment style var use, though, the type is still quite to the left - if there is a very long variable name, I might put the type in the next line, a bit intented.

                  Fabio Franco wrote:

                  You don't use dynamic keyword for types known at compile type, do you?

                  Huh? Var is not dynamic. Bad example? Exactly *because* the type is known at compile time, I do use var in assignments!

                  Fabio Franco wrote:

                  Somethings are there for a purpose, so that's what we should use them for

                  That sounds quite like a priest, talking dogma, not to be questioned. If something turns out to work quite nicely for something else than original intention, why not use it? That something was not originally envisioned to be use for something, alone, is not an argument at all, for refraining from using it that way.

                  Fabio Franco wrote:

                  Do you use a hammer as a screwdriver? You could bust something open, but I'd rather not brake it while doing so.

                  I have not seen an example of this var use busting something open, other than you personally can't read the code as quickly.

                  Fabio Franco wrote:

                  OriginalGriff has more experience than both of us combined, i

                  F Offline
                  F Offline
                  Fabio Franco
                  wrote on last edited by
                  #58

                  Steve#2 wrote:

                  ince my IDE shows the type of a variable when hovering the cursor over it, which happens to be where I read when scrolling.

                  So you need to hover over it, don't you?

                  Steve#2 wrote:

                  There are languages which don't have something like LINQ but do have automatic type inference, so a lot of people seem to disagree here what such a feature is good for or isn't.

                  Yep, like javascript, I don't think you want a start a discussion that this is a good feature of javascript....

                  Steve#2 wrote:

                  But yeah, I can see how that can be annoying

                  Yes, very much ;)

                  Steve#2 wrote:

                  I hate it when I can't identify scope by looking at the exact same column

                  I hate that as well X| X|

                  Steve#2 wrote:

                  Huh? Var is not dynamic. Bad example?
                  Exactly *because* the type is known at compile time, I do use var in assignments!

                  You're missing the point. The point is, you should use a tool for what it's designed for, not to make your life easy at some point, you gotta think of the consequences.

                  Steve#2 wrote:

                  If something turns out to work quite nicely for something else than original intention, why not use it?

                  That's my point. Using var just because is easier shouldn't be justification at the cost of readability and comprehension. I'm sick of so many times having to look what the type is because I can't deduce it by it's declaration. So, it may work quite nicely for you who does not have to maintain other people's code, but no, it does not work when you do have to.

                  Steve#2 wrote:

                  I have not seen an example of this var use busting something open, other than you personally can't read the code as quickly.

                  You missing the point again, it was just an analogy. And if you haven't seen it, it does not mean it doesn't happen. And it's not just me who can't read as quickly. If you tell me that there is a quicker way to know a variable type other than it's declaration, please enlighten me and the rest of the world.

                  Steve#2 wrote:

                  I don't like just trust, I like compelling explanations.

                  As do I. And that's what I'm doing here in the la

                  S 1 Reply Last reply
                  0
                  • C CPallini

                    I disagree. :)

                    Veni, vidi, vici.

                    F Offline
                    F Offline
                    Fabio Franco
                    wrote on last edited by
                    #59

                    But bad software engineers do tend to prefer bad languages don't you think? "But it's not working!!!" :confused:

                    To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Yes, I agree. Every element of a language has it's use - even goto and var in C#- it's just that if you use them inappropriately you get less readable code instead of more. Personally, I find lambdas are useful in their place, but I avoid using them most of the time. var should be banned outside Linq!

                      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                      K Offline
                      K Offline
                      Kevin Marois
                      wrote on last edited by
                      #60

                      Outside of Linq, the only purpose var serves is to save a lazy programm a few keystrokes.

                      If it's not broken, fix it until it is

                      1 Reply Last reply
                      0
                      • S Steve 2

                        haha sounds like me. var is certainly not only nice for foreach loops or LINQ. I find it rather stupid to type something like:

                        Dictionary>> stuff = /*sigh*/ new Dictionary>>();

                        Yes, stupid indeed. And it's exactly that type of scenario the "var" keyword was made for.

                        K Offline
                        K Offline
                        Kevin Marois
                        wrote on last edited by
                        #61

                        Steve#2 wrote:

                        And it's exactly that type of scenario the "var" keyword was made for

                        What "scenerio" is that? You don't want to type more than you have to, so you leave it to the next developer to figure out what the type is?

                        If it's not broken, fix it until it is

                        S 1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Yes, I agree. Every element of a language has it's use - even goto and var in C#- it's just that if you use them inappropriately you get less readable code instead of more. Personally, I find lambdas are useful in their place, but I avoid using them most of the time. var should be banned outside Linq!

                          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                          K Offline
                          K Offline
                          Kevin Marois
                          wrote on last edited by
                          #62

                          Had a "senior" guy I worked with who asked in a meeting why you couldn't do

                          var myvariable = null;

                          If it's not broken, fix it until it is

                          OriginalGriffO 1 Reply Last reply
                          0
                          • M Mycroft Holmes

                            Actually I liked GOSUB but now I know why!

                            Never underestimate the power of human stupidity RAH

                            K Offline
                            K Offline
                            Kevin Marois
                            wrote on last edited by
                            #63

                            This [^] was always fun

                            If it's not broken, fix it until it is

                            1 Reply Last reply
                            0
                            • S Steve 2

                              I don't think it was made just for LINQ, that's not what I've read in countless introdution texts about new features back then. What do you mean by "Except that generics cme later" ? Generics are there since .NET 2.0, var since 3.5 IIRC.

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #64

                              Steve#2 wrote:

                              not what I've read in countless introdution texts

                              Which were likely written by they who abuse it.

                              Steve#2 wrote:

                              What do you mean by

                              Generics came after the using directive.

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Yes, I agree. Every element of a language has it's use - even goto and var in C#- it's just that if you use them inappropriately you get less readable code instead of more. Personally, I find lambdas are useful in their place, but I avoid using them most of the time. var should be banned outside Linq!

                                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                                I Offline
                                I Offline
                                Ian Shlasko
                                wrote on last edited by
                                #65

                                Nah, var has its uses... I basically use it in two situations: 1) Triggering simple events, just because it's glue code that I don't care about:

                                private void OnDownloaded()
                                {
                                var evt = DataDownloaded;
                                if (evt != null)
                                evt(this, EventArgs.Empty);
                                }

                                1. Eliminate long, redundant declarations, when there's nothing fancy going on... I know, some people would rather see the type on the left side, but I happen to find this a LOT more readable:

                                var modelTemp = new ViewModels.GridTree.ViewTableModel(view);

                                I actually don't use it for LINQ unless the generated types are really nasty... LINQ types aren't "obvious", so I'd rather see exactly what they are... And if the returned type is a Lookup of Dictionaries of HashSets of Arrays of Strings, then that's a signal to refactor, not a reason to use var. Just eearched my code and found 79 uses of the var keyword... Except it looks like about 75% of them are in my snippet-generated OnPropertyChanged functions (INPC in models)... Another 20% or so are in third-party code, and the rest are more of the two items I listed above. ... Speaking of third-party code... Just glanced at a few of the var hits from one of the controls I scavenged, and found: var stepSize = (int)value;... That's just unnecessary...

                                Proud to have finally moved to the A-Ark. Which one are you in?
                                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                                F 1 Reply Last reply
                                0
                                • S sisnaz

                                  After coming down off of the "That's pretty cool" factor and as members on my team have increasingly been using anonymous methods, lambda expressions and new Func<> routines embedded in methods. The complexity (IMO) as increased significantly. I've begun to question this seemingly popular approach as; What's the difference between using embedded functions and a goto statement? It seems to me it's no different and just as difficult to follow and maintain. I'd be curious on other opinions of this practice.

                                  K Offline
                                  K Offline
                                  Kirk Wood
                                  wrote on last edited by
                                  #66

                                  It all depends on the context. I mean it would be horrible (but possible) to put the entire program crammed down into the main function (method) of your program. It would not be the GOTO, it would be something much more hideous. But if you need to iterate through an IEnumerable and only work with some of them it is quite nice to throw in a lambda expression to do this: foreach(var item in collection.Where(i=>i.Care==true) Another place I have found a great like (though it does have some ramp time to get used to it) is to use a lambda expression for an async method callback. In this case I find it makes it easier to read because: 1) It keeps the whole process together. 2) No need to cram context and then cast it. (Yes I know there is overhead and under the covers everything gets shoved into a context object and pulled back out, but I choose to only worry about noticible performance.) Now I have seen some horrible uses as well that seemed "cool." In the end if it doesn't make it easier to maintain it makes it more fragile.

                                  1 Reply Last reply
                                  0
                                  • K Kevin Marois

                                    Had a "senior" guy I worked with who asked in a meeting why you couldn't do

                                    var myvariable = null;

                                    If it's not broken, fix it until it is

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #67

                                    :doh: Post it in Coding Horrors! Oh, and check his hair - is it starting to rise in two peaks?

                                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      Steve#2 wrote:

                                      type of scenario the "var" keyword was made for.

                                      Bullpuckey; it was made for Linq. That's exactly the scenario the using directive was made for:

                                      using MyDic = Dictionary<int, List<Vec3<float>>> ;
                                      ...
                                      MyDic stuff = new MyDic() ;

                                      (Except that generics cme later. :sigh: )

                                      J Offline
                                      J Offline
                                      Jecc
                                      wrote on last edited by
                                      #68

                                      MyDic is a lousy name. It should be

                                      using DictionaryFromIntToListOfVectorsOfThreeFloats = Dictionary<int, List<Vec3<float>>> ;
                                      ...
                                      DictionaryFromIntToListOfVectorsOfThreeFloats intsTo3DFloatCoords = new DictionaryFromIntToListOfVectorsOfThreeFloats() ;

                                      Edit: 'stuff' is a lousy name too.

                                      P 1 Reply Last reply
                                      0
                                      • S Steve 2

                                        I think I do: automatic type inference. Which is what my example used them for.

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #69

                                        No, it's "automatic type inference for those times when the developer can't know the type when writing the code".

                                        S 1 Reply Last reply
                                        0
                                        • J Jecc

                                          MyDic is a lousy name. It should be

                                          using DictionaryFromIntToListOfVectorsOfThreeFloats = Dictionary<int, List<Vec3<float>>> ;
                                          ...
                                          DictionaryFromIntToListOfVectorsOfThreeFloats intsTo3DFloatCoords = new DictionaryFromIntToListOfVectorsOfThreeFloats() ;

                                          Edit: 'stuff' is a lousy name too.

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #70

                                          Jecc wrote:

                                          MyDic is a lousy name

                                          What's in a name? It only has scope within the file so keep the file small and easily read.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups