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.
  • 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
                      • L leppie

                        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 Offline
                        B Offline
                        Bergholt Stuttley Johnson
                        wrote on last edited by
                        #42

                        Sorry "Undocumented features"

                        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
                        • V Vark111

                          OriginalGriff wrote:

                          Coming soon: "On Error Resume Next"

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

                          try
                          {
                              //some op
                          }
                          catch {}
                          
                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #43

                          Takes too much typing for most idiots:

                              Bitmap GetScreen()
                                  {
                                  try {Rectangle screenRegion = Screen.AllScreens\[0\].Bounds;} catch{}
                                  try {Bitmap grab = new Bitmap(SystemInformation.PrimaryMonitorSize.Width,
                                                                SystemInformation.PrimaryMonitorSize.Height,
                                                                PixelFormat.Format32bppArgb);} catch{}
                                  using (Graphics screenGraphics = Graphics.FromImage(grab))
                                      {
                                      try {screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);} catch{}
                                      }
                                  return grab;
                                  }
                          

                          With resume next, you just add it once and walk away - until the problem is so big it bites you on the ass and you have no idea where it started... X|

                          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."

                          "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
                          • 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
                            Luca Leonardo Scorcia
                            wrote on last edited by
                            #44

                            While your example is clearly awful, I find myself using it quite consistently recently. It helps a lot with lambdas, nested generics and similar verbose type definitions. Guess it's just a language feature like the others. My personal rule is to use it only for complex (more than 1 type parameter) generic types. I'm a heavy Resharper user, but that var rule is the first one I turn off. The second one is the 'Warning on unnecessary this.' rule. Guess it's time to introduce your colleagues to FxCop :) Cheers!

                            Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

                            M 1 Reply Last reply
                            0
                            • L Luca Leonardo Scorcia

                              While your example is clearly awful, I find myself using it quite consistently recently. It helps a lot with lambdas, nested generics and similar verbose type definitions. Guess it's just a language feature like the others. My personal rule is to use it only for complex (more than 1 type parameter) generic types. I'm a heavy Resharper user, but that var rule is the first one I turn off. The second one is the 'Warning on unnecessary this.' rule. Guess it's time to introduce your colleagues to FxCop :) Cheers!

                              Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

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

                              I need the machine that on StyleCop warning hits the responsible person with a bat! :)

                              L 1 Reply Last reply
                              0
                              • M Mario Majcica

                                I need the machine that on StyleCop warning hits the responsible person with a bat! :)

                                L Offline
                                L Offline
                                Luca Leonardo Scorcia
                                wrote on last edited by
                                #46

                                That's just fine, I'm completely irresponsible :-\ ;)

                                Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

                                1 Reply Last reply
                                0
                                • N Nish Nishant

                                  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 Offline
                                  P Offline
                                  Peter Mulholland
                                  wrote on last edited by
                                  #47

                                  Wonderful, and I thought the C++ code I've been looking at for 3 years couldn't get any worse. Now it can.

                                  Pete

                                  N 1 Reply Last reply
                                  0
                                  • P Peter Mulholland

                                    Wonderful, and I thought the C++ code I've been looking at for 3 years couldn't get any worse. Now it can.

                                    Pete

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

                                    :-D

                                    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
                                    • H hairy_hats

                                      List list=new List(); is only one extra character to type, and is clearer IMO.

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #49

                                      FYI, List takes a generic parameter.

                                      Driven to the ARMs by x86.

                                      1 Reply Last reply
                                      0
                                      • L leppie

                                        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 Offline
                                        B Offline
                                        BobJanova
                                        wrote on last edited by
                                        #50

                                        There are a lot of 'Bars' in the real world though ... Close, Draw, Update, Refresh etc.

                                        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