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. Poor var (C#)

Poor var (C#)

Scheduled Pinned Locked Moved The Lounge
csharpc++comdiscussion
62 Posts 36 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.
  • H hairy_hats

    Maybe we could stick to the status quo but enforce column alignments à la Fortran to make all this scanning easier? ;)

    I hope you realise that hamsters are very creative when it comes to revenge. - Elaine

    B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #22

    I once built a column aligning editor for a COBOL project. Impressed my cute team mate quite a bit, where others were all using notepad and counting columns, ours had tab settings at all the right columns. :)

    1 Reply Last reply
    0
    • T tec goblin

      I'll want you to explain me what you would use for a linq query that returns an anonymous type, or what is not enough strongly typed for you in the code var records = new List(); For me, var is a blessing. It saves me of repeating redundant information (why the hell would I want to say in the left AND the right hand side that this is a damned List of Records?).

      A Offline
      A Offline
      Adriaan Davel
      wrote on last edited by
      #23

      I think it has big strengths and big weaknesses, if used badly its going to cause confusion, if used correclty it will assist greatly. One rule that I have (and that I love var for) is the variable naming MUST be good else code becomes less readable...

      ____________________________________________________________ Be brave little warrior, be VERY brave

      1 Reply Last reply
      0
      • P PIEBALDconsult

        Hmmm... what if object had worked like var to begin with? :suss:

        A Offline
        A Offline
        Adriaan Davel
        wrote on last edited by
        #24

        Hmmm, I think I like the way you think... :cool:

        ____________________________________________________________ Be brave little warrior, be VERY brave

        1 Reply Last reply
        0
        • B BillWoodruff

          (Namaste, Namaskaram, or Vanakkum : your choice :) Sri Nishant, I understand the "Committee for the Defense of Var," with a war-chest of millions of dollars provided by the VB party, in collusion with strident libertarians protesting strongly-typed languages, fed by a network of radio-show hosts with legions of fanatic followers ... is even now launching a campaign to use this sneaky trojan-horse of "var" as just the launching pad for the total dismantling of pure, reformed, C#. Sinister forces in the industrial-military complex, the webs of former government officials who left their jobs with regulatory bodies in government to turn around and become lobbyists for the companies they once regulated ... at enormous salaries ... they are all in on the plan to dilute the vital fluids of young programmers, making them weakly-typed. Even now the "Var" poster-child's poster : a picture of a ten-year old female hacker, with the face of an angel, whose hands are permanently crippled from typing long object-inheritance declarations twice on the same line, are going up in shopping malls and latterias across the country bearing the slogan "With Var She would still be able to type !" It is time for us to forget our minor quibbles about "polymorphism," and unite to combat this insidious threat to our ritual purity. When our enemies are not "bound" by our ethics, surely we are righteous to fight them back without respect for what they claim to be their ethics ! best, Bill

          "Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

          K Offline
          K Offline
          KramII
          wrote on last edited by
          #25

          This is pure anti-varist propaganda. If there is an enemy in the camp, it is not var. The much-maligned VB party strives not for anarchy, but for freedom with responsibility. The VB party does not condone any acts of violence perpetrated with var or any other tool at our disposal. We do seek to free, but we also strive to educate. Now, we do openly acknowledge that there are those within out ranks who misappropriate the libertarian message of var, who misuse its expressiveness to undermine productivity and pollute the code-base. However, var is not the cause of poor practice, but merely a tool of convenience for the unlearned hack. Take away var and these uneducated code-manglers will find a myriad other techniques to defile your code. They will tangle your classes, force your file handles permanently open, race your threads until they lock, or leach your resources with their crude algorithms and ugle data structures. So, we recognise that there are those who misuse the tools we provide, but argue that this is true of every party, regardless of colour and flag. And may I say this to those who besmirch the good name of the VB party: you speak of the impurity promoted by var, but ignore a greater danger: providing unregulated access to the world of unmanaged code, a realm where the uninitiated are free to dangle their pointers without reprise nor censure! He who is without sin, my friends, he who is without sin.

          KramII

          B 1 Reply Last reply
          0
          • K KramII

            This is pure anti-varist propaganda. If there is an enemy in the camp, it is not var. The much-maligned VB party strives not for anarchy, but for freedom with responsibility. The VB party does not condone any acts of violence perpetrated with var or any other tool at our disposal. We do seek to free, but we also strive to educate. Now, we do openly acknowledge that there are those within out ranks who misappropriate the libertarian message of var, who misuse its expressiveness to undermine productivity and pollute the code-base. However, var is not the cause of poor practice, but merely a tool of convenience for the unlearned hack. Take away var and these uneducated code-manglers will find a myriad other techniques to defile your code. They will tangle your classes, force your file handles permanently open, race your threads until they lock, or leach your resources with their crude algorithms and ugle data structures. So, we recognise that there are those who misuse the tools we provide, but argue that this is true of every party, regardless of colour and flag. And may I say this to those who besmirch the good name of the VB party: you speak of the impurity promoted by var, but ignore a greater danger: providing unregulated access to the world of unmanaged code, a realm where the uninitiated are free to dangle their pointers without reprise nor censure! He who is without sin, my friends, he who is without sin.

            KramII

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #26

            5 ! At last I meet a "worthy opponent." best, Bill

            "Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

            1 Reply Last reply
            0
            • R Rei Miyasaka

              I remember Sun insisting at conferences several years ago that the unsafe keyword in C# would be its downfall. People are idiots.

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #27

              unsafe was named so 'scary' and buried under an extra compiler option that nearly no one is using it. I've seen code doing manual marshaling (Marshal.AllocHGlobal, Marshal.Copy, etc.) for P/Invoke calls which could have been replaced with a simple fixed block around the P/Invoke call.

              R 1 Reply Last reply
              0
              • B Brady Kelly

                How about looking for the variable name that should indicate it being a list, or do you declare List v1 = new List();? I would quickly scan down my list and find var studentList = new List();

                A Offline
                A Offline
                Anders 0
                wrote on last edited by
                #28

                Or even better:

                var students = new List();

                That way, you're not repeating yourself... ;)

                T B A 3 Replies Last reply
                0
                • B Brady Kelly

                  How about looking for the variable name that should indicate it being a list, or do you declare List v1 = new List();? I would quickly scan down my list and find var studentList = new List();

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #29

                  That is no good. var studentList = new List(); is bad in two ways: 1. now it will say "List" every time you need to reference your students collection, that is even more typing with var than without it; 2. if and when you decide to change your collection type (say to SortedList, Dictionary, whatever) you have to rename all those "studentList" references. And obviously List v1 = new List(); is very bad, it should have been List students = new List(); :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • H hairy_hats

                    Maybe we could stick to the status quo but enforce column alignments à la Fortran to make all this scanning easier? ;)

                    I hope you realise that hamsters are very creative when it comes to revenge. - Elaine

                    G Offline
                    G Offline
                    Gary Wheeler
                    wrote on last edited by
                    #30

                    Steve_Harris wrote:

                    enforce column alignments à la Fortran

                    Oh varf, er, barf.

                    Software Zen: delete this;

                    1 Reply Last reply
                    0
                    • T tec goblin

                      I'll want you to explain me what you would use for a linq query that returns an anonymous type, or what is not enough strongly typed for you in the code var records = new List(); For me, var is a blessing. It saves me of repeating redundant information (why the hell would I want to say in the left AND the right hand side that this is a damned List of Records?).

                      C Offline
                      C Offline
                      csharphacker
                      wrote on last edited by
                      #31

                      I guess I don't get all the hoopla over var? What happens if you were to initialize your variables and use them elsewhere? List records = null; records = new List(); Also, what kind of overhead does var add to your applications when you use it everywhere? It's like a lot of new features in C# (don't get me started on extension methods). They were added to allow LINQ to be implemented, but should NOT be used in general, everyday programming tasks (unless you are using LINQ) Some mornings it just doesn't seem worth it to gnaw through the leather straps. - Emo Phillips

                      1 Reply Last reply
                      0
                      • B Brady Kelly

                        How about looking for the variable name that should indicate it being a list, or do you declare List v1 = new List();? I would quickly scan down my list and find var studentList = new List();

                        N Offline
                        N Offline
                        Netblue
                        wrote on last edited by
                        #32

                        Your kidding right? I have seen that code a million times written by other developers.

                        Proudly drinking the finest Maryland craft beer. Visiting Maryland for business? First round is on me!

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          If you look at this week's poll forum, you can see some pretty nasty comments about the C# var keyword. People seem to have mistaken it to be equivalent to the VB6/COM VARIANT data type. A closer equivalent to the VB6/COM VARIANT would be C# 4.0's dynamic keyword. But to attack var based on some wrong assumptions is sad. Remember, all's fair in love and var ;P

                          Regards, Nish


                          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                          My latest book : C++/CLI in Action / Amazon.com link

                          M Offline
                          M Offline
                          Mark J Miller
                          wrote on last edited by
                          #33

                          I say, use it or don't but it doesn't really save you typing - ever heard of intellisense? Try it, count how many total keystrokes it takes to use var verses typing the whole thing out using intellisense as soon as you get the option. Most of the time you'll type more than 3 characters on the left side to declare the type, but intellisense will then give you the right side for free (1 keystroke to accept the selected type from the intellisense list). You break even - so that little girl w/ the cripped fingers == propaganda :P

                          Code responsibly: OWASP.org Mark's blog: developMENTALmadness.blogspot.com Click here for Free Industry White Papers/Magazines! Bill Cosby - "A word to the wise ain't necessary - it's the stupid ones that need the advice."

                          D 1 Reply Last reply
                          0
                          • L Lost User

                            And shopping. Especially if there is dicounted shoes and chocolate.

                            Visit http://www.notreadytogiveup.com/[^] and do something special today.

                            G Offline
                            G Offline
                            grgran
                            wrote on last edited by
                            #34

                            "shoe shopping" ??????? What kind of programmers are you hanging out with ... there is exactly one type of flip-flop ... cheap ... shopping is not really an issue. Chocolate is is only slightly more complicated, it comes in three forms; large, rich and "large and rich". The third is always perferred but not always available. :-)

                            1 Reply Last reply
                            0
                            • A AspDotNetDev

                              Yeah, this should have been thought of way long ago. Makes one wonder if any other languages have this feature.

                              P Offline
                              P Offline
                              Pascal Ganaye
                              wrote on last edited by
                              #35

                              Vb has it but they managed to re-use the dim keyword rather that creating a new var keyword. Normal Declarations (without using var):

                              C# : int x;
                              VB : dim x as integer

                              Using var (and Option Infer in VB)

                              C# : var x = 5;
                              VB : dim x = 5

                              Note that VB as also a nice compact dim ... as new ... syntax:

                              C# : var x = new Button();
                              VB : dim x as new Button()

                              Both language look very similar to me now.

                              A 1 Reply Last reply
                              0
                              • M Mycroft Holmes

                                I think calling anything "var" is a bad naming convention, that name has way too much baggage.

                                P Offline
                                P Offline
                                Pascal Ganaye
                                wrote on last edited by
                                #36

                                yes dim was better :-)

                                1 Reply Last reply
                                0
                                • N Nish Nishant

                                  If you look at this week's poll forum, you can see some pretty nasty comments about the C# var keyword. People seem to have mistaken it to be equivalent to the VB6/COM VARIANT data type. A closer equivalent to the VB6/COM VARIANT would be C# 4.0's dynamic keyword. But to attack var based on some wrong assumptions is sad. Remember, all's fair in love and var ;P

                                  Regards, Nish


                                  Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                  My latest book : C++/CLI in Action / Amazon.com link

                                  M Offline
                                  M Offline
                                  MiddleTommy
                                  wrote on last edited by
                                  #37

                                  I dont understand var bashing. It is a shortcut use it or dont use it. You can turn intellisense off too. As for readability? I find var makes me digg to understand code more than using traditional static type declarations as a crutch. If var is confusing your understanding of code then your methods are too long and too complex. Also you can always hover over a variable to have visual studio tell you the type of a var declared object.

                                  M 1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    Yeah, this should have been thought of way long ago. Makes one wonder if any other languages have this feature.

                                    D Offline
                                    D Offline
                                    Dave Sexton
                                    wrote on last edited by
                                    #38

                                    JavaScript. Or did you not mean to include dynamic languages?

                                    But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson
                                    Because programming is an art, not a science. Marc Clifton
                                    I gave up when I couldn't spell "egg". Justine Allen

                                    A 1 Reply Last reply
                                    0
                                    • M Mark J Miller

                                      I say, use it or don't but it doesn't really save you typing - ever heard of intellisense? Try it, count how many total keystrokes it takes to use var verses typing the whole thing out using intellisense as soon as you get the option. Most of the time you'll type more than 3 characters on the left side to declare the type, but intellisense will then give you the right side for free (1 keystroke to accept the selected type from the intellisense list). You break even - so that little girl w/ the cripped fingers == propaganda :P

                                      Code responsibly: OWASP.org Mark's blog: developMENTALmadness.blogspot.com Click here for Free Industry White Papers/Magazines! Bill Cosby - "A word to the wise ain't necessary - it's the stupid ones that need the advice."

                                      D Offline
                                      D Offline
                                      Dan Neely
                                      wrote on last edited by
                                      #39

                                      While not defending var itself, intellisense effectiveness depends on the type. For an extreme example start a winform app, begin typing DataGridViewRow, without scrolling you'll have to type all but the last two characters before the type is selected. Immediately before you type the second R it's on 'page 10' of the list. DataGridView itself is almost as bad because of the old DataGrid class.

                                      The European Way of War: Blow your own continent up. The American Way of War: Go over and help them.

                                      M 1 Reply Last reply
                                      0
                                      • N Nish Nishant

                                        If you look at this week's poll forum, you can see some pretty nasty comments about the C# var keyword. People seem to have mistaken it to be equivalent to the VB6/COM VARIANT data type. A closer equivalent to the VB6/COM VARIANT would be C# 4.0's dynamic keyword. But to attack var based on some wrong assumptions is sad. Remember, all's fair in love and var ;P

                                        Regards, Nish


                                        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                        My latest book : C++/CLI in Action / Amazon.com link

                                        J Offline
                                        J Offline
                                        Jorgen Sigvardsson
                                        wrote on last edited by
                                        #40

                                        I bet the same people would "hate" compilers if given the question.

                                        -- Kein Mitleid Für Die Mehrheit

                                        1 Reply Last reply
                                        0
                                        • D Dan Neely

                                          While not defending var itself, intellisense effectiveness depends on the type. For an extreme example start a winform app, begin typing DataGridViewRow, without scrolling you'll have to type all but the last two characters before the type is selected. Immediately before you type the second R it's on 'page 10' of the list. DataGridView itself is almost as bad because of the old DataGrid class.

                                          The European Way of War: Blow your own continent up. The American Way of War: Go over and help them.

                                          M Offline
                                          M Offline
                                          Mark J Miller
                                          wrote on last edited by
                                          #41

                                          Point well taken. Intellisense doesn't always save a whole lot of typing, but I'm referring to the right side of the declaration. Based on your example:

                                          var row = new DataGridViewRow();
                                          ^--

                                          Here intellisense helps me after 27 characters, then I still need type type "();" taking the total to 30. Intellisense only saved me 2 characters. verses:

                                          DataGridViewRow row = new DataGridViewRow();
                                          ^-- ^--------------

                                          Intellisense kicks in at 13 characters and completes the type and a space for me. Then I type 10 more taking the total to 23. Again, add 3 for "();" and I've typed a total of 26. Using var cost me 4 keystrokes. This is pretty common when comparing intellisense to var because (except when the left side is an interface) the type on the left matches the type on the right and so when you type "new " intellisense will give you the type which is on the left. The only keystroke I need at this point is [TAB]. I can even save an additional keystroke on the left side if I make my intellisense keystroke with [SPACEBAR] instead of [TAB]+[SPACEBAR].

                                          Code responsibly: OWASP.org Mark's blog: www.developMENTALmadness.com Bill Cosby - "A word to the wise ain't necessary - it's the stupid ones that need the advice."

                                          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