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. var abuse

var abuse

Scheduled Pinned Locked Moved The Lounge
question
50 Posts 17 Posters 0 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.
  • R RugbyLeague

    Ah, I wondered what happened to the generics bit on my OP How is it sloppy and lazy?

    P Offline
    P Offline
    Peter Mulholland
    wrote on last edited by
    #22

    Coming from a C++ background, I feel it doesn't improve the readability of the code, particularly when used to accept a return value from a method. In my experience, those that like it overuse and abuse it. It was a mistake to add it to the C# language.

    Pete

    R N 2 Replies Last reply
    0
    • P Peter Mulholland

      Coming from a C++ background, I feel it doesn't improve the readability of the code, particularly when used to accept a return value from a method. In my experience, those that like it overuse and abuse it. It was a mistake to add it to the C# language.

      Pete

      R Offline
      R Offline
      RugbyLeague
      wrote on last edited by
      #23

      I come from a C++ background too and I like var - I agree using it to accept a return value from a method is a poor use of it.

      1 Reply Last reply
      0
      • M Mario Majcica

        And why? I can understand some cases in which you want to make code more compact, as

        List myName = new List();

        to use a

        var myName = new List();

        or as mentioned before in cases that you do not know the return type. But using always var only because of laziness, just pisses me of!

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #24

        The 'why' is because the type is already there in the line, so using var doesn't remove any information. Personally I only use it very rarely but that's a matter of style, I think it is fine when the type is in the line anyway (either as a new expression or an explicit cast: var something = (TypeName)expression).

        1 Reply Last reply
        0
        • M Mario Majcica

          I'm so pissed of by var abusers. They do not understand that are making code unreadable, for what, writing var instead of string? Having the same problems in your company? ex.

          var metadataValues = new List<object>();
          foreach (var metadataDefName in metadataDefNames)
          {
          var name = metadataDefName;
          // Search definition with the same name.
          var metadata = metadataCollection.FirstOrDefault(
          metadataDef => String.Equals(metadataDef.MetaData.Name, name, StringComparison.OrdinalIgnoreCase));
          ...

          Cheers!

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #25

          Reason for my 1 vote: There is nothing wrong with that. If you need to get the type, just hover over the 'var' in the IDE.

          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

          M B P 3 Replies Last reply
          0
          • P Pete OHanlon

            Or you are using ReSharper and tell it to autofix the "problems" in your code.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #26

            Well put. Exactly why I do NOT use handholding tools like Reshaper. Clearly their definition of 'problems' is very subjective, and personally does not suite me. I write the code exactly how I want it!

            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

            H 1 Reply Last reply
            0
            • L leppie

              Reason for my 1 vote: There is nothing wrong with that. If you need to get the type, just hover over the 'var' in the IDE.

              ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

              M Offline
              M Offline
              Mario Majcica
              wrote on last edited by
              #27

              Meanwhile I'm reading the code I can't constantly hover the vars! Com'on, how can you say that??? :) And this is the lounge, I'm not discussing about what should be a good practice, I lament about things that makes me piss off.

              L 1 Reply Last reply
              0
              • M Mario Majcica

                Meanwhile I'm reading the code I can't constantly hover the vars! Com'on, how can you say that??? :) And this is the lounge, I'm not discussing about what should be a good practice, I lament about things that makes me piss off.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #28

                Mario Majcica wrote:

                Meanwhile I'm reading the code I can't constantly hover the vars!

                Use Reflector then :) If the code is well written, you should be able to infer the type while reading. What does get me is the person using 'var' instead of 'object', now that is silly. :)

                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                1 Reply Last reply
                0
                • P Peter Mulholland

                  Coming from a C++ background, I feel it doesn't improve the readability of the code, particularly when used to accept a return value from a method. In my experience, those that like it overuse and abuse it. It was a mistake to add it to the C# language.

                  Pete

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #29

                  Peter Mulholland wrote:

                  It was a mistake to add it to the C# language.

                  C++ has it too now, it's called auto.

                  auto x = SomeFunc();

                  Regards, Nish


                  Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com

                  P 1 Reply Last reply
                  0
                  • R RugbyLeague

                    Why? What is the point of repeating information?

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #30

                    Many people don't know what C#'s var means. They incorrectly think it's not type-safe! :-)

                    Regards, Nish


                    Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com

                    P 1 Reply Last reply
                    0
                    • L leppie

                      Well put. Exactly why I do NOT use handholding tools like Reshaper. Clearly their definition of 'problems' is very subjective, and personally does not suite me. I write the code exactly how I want it!

                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                      H Offline
                      H Offline
                      hairy_hats
                      wrote on last edited by
                      #31

                      leppie wrote:

                      I write the code exactly how I want it!

                      So the bugs are deliberate?

                      L 1 Reply Last reply
                      0
                      • N Nish Nishant

                        Many people don't know what C#'s var means. They incorrectly think it's not type-safe! :-)

                        Regards, Nish


                        Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com

                        P Offline
                        P Offline
                        Peter Mulholland
                        wrote on last edited by
                        #32

                        I haven't looked at it enough to concern myself with whether or not it's type safe. I don't think it aids code readability, and I've never found a situation where I needed to use it.

                        Pete

                        N 1 Reply Last reply
                        0
                        • L leppie

                          Reason for my 1 vote: There is nothing wrong with that. If you need to get the type, just hover over the 'var' in the IDE.

                          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #33

                          It's nice to be able to read the code without hovering the mouse over things. And one might not be in an IDE at the time.

                          L 1 Reply Last reply
                          0
                          • L leppie

                            Reason for my 1 vote: There is nothing wrong with that. If you need to get the type, just hover over the 'var' in the IDE.

                            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

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

                            I can't hover over it when it's printed on a page, posted in a message here, displayed by someone else on a big screen in a meeting, etc.

                            1 Reply Last reply
                            0
                            • M Mario Majcica

                              I'm so pissed of by var abusers. They do not understand that are making code unreadable, for what, writing var instead of string? Having the same problems in your company? ex.

                              var metadataValues = new List<object>();
                              foreach (var metadataDefName in metadataDefNames)
                              {
                              var name = metadataDefName;
                              // Search definition with the same name.
                              var metadata = metadataCollection.FirstOrDefault(
                              metadataDef => String.Equals(metadataDef.MetaData.Name, name, StringComparison.OrdinalIgnoreCase));
                              ...

                              Cheers!

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

                              GAH! I don't need to see that when I first wake up in the morning! What were you thinking, man? Post it later in the day. :~ But I certainly agree with you.

                              M 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                GAH! I don't need to see that when I first wake up in the morning! What were you thinking, man? Post it later in the day. :~ But I certainly agree with you.

                                M Offline
                                M Offline
                                Mario Majcica
                                wrote on last edited by
                                #36

                                Healing my frustrations here!

                                1 Reply Last reply
                                0
                                • H hairy_hats

                                  leppie wrote:

                                  I write the code exactly how I want it!

                                  So the bugs are deliberate?

                                  L Offline
                                  L Offline
                                  leppie
                                  wrote on last edited by
                                  #37

                                  viaducting wrote:

                                  So the bugs are deliberate?

                                  What bugs? ;P But in reality, I loathe typing, so I try code correctly the first time. I dont mind spending 90% of my time figuring out the entire solution mentally. You type less, and debug almost never :) Or maybe that is just how I work :rolleyes:

                                  ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                  B 1 Reply Last reply
                                  0
                                  • P Peter Mulholland

                                    I haven't looked at it enough to concern myself with whether or not it's type safe. I don't think it aids code readability, and I've never found a situation where I needed to use it.

                                    Pete

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #38

                                    Peter Mulholland wrote:

                                    I don't think it aids code readability, and I've never found a situation where I needed to use it.

                                    You absolutely need it with anonymous types, and it's more than handy with LINQ where you know it's returning some collection but don't know or care what type it is.

                                    Regards, Nish


                                    Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com

                                    1 Reply Last reply
                                    0
                                    • B BobJanova

                                      It's nice to be able to read the code without hovering the mouse over things. And one might not be in an IDE at the time.

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #39

                                      BobJanova wrote:

                                      It's nice to be able to read the code without hovering the mouse over things.

                                      Knowing the type is not really necessary if the code is unambiguous. Just replace the type with some type 'Foo' with a property of 'Bar'. Now if we have 10 types, all with a property 'Bar', yeah then it gets shitty. :)

                                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                      B 1 Reply Last reply
                                      0
                                      • OriginalGriffO OriginalGriff

                                        It's VB creep: The bits of VB that mean that you don't have to think about what you are doing are slowly being introduced to C#. Coming soon: "On Error Resume Next" Then it will be time to go back to C++ because C# will be mostly used by lazy idiots who don't care a fig for maintenance.

                                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                        V Offline
                                        V Offline
                                        Vark111
                                        wrote on last edited by
                                        #40

                                        OriginalGriff wrote:

                                        Coming soon: "On Error Resume Next"

                                        What do you mean "coming soon"? It's already there:

                                        try
                                        {
                                            //some op
                                        }
                                        catch {}
                                        
                                        B OriginalGriffO 2 Replies Last reply
                                        0
                                        • V Vark111

                                          OriginalGriff wrote:

                                          Coming soon: "On Error Resume Next"

                                          What do you mean "coming soon"? It's already there:

                                          try
                                          {
                                              //some op
                                          }
                                          catch {}
                                          
                                          B Offline
                                          B Offline
                                          Bergholt Stuttley Johnson
                                          wrote on last edited by
                                          #41

                                          I did know a programmer that did just that, first thing he wrote in any method class etc was a try catch block in which he put all his code, and yes it had a empty catch oh and he was a ex-VB programmer too was soooo proud his code never threw exceptions

                                          You cant outrun the world, but there is no harm in getting a head start Real stupidity beats artificial intelligence every time.

                                          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