CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Coding standards

    Personally I like Hungarian notation for C#, because I like it for C++.

    I have read in Microsoft coding standard articles that Hungarian notation for C# is bad.

    I've also read that for member variables you should use '_'.

    e.g.

    Code:
    // bad according to Microsoft coding standards
    class MyClassBad
    {
        private string m_sString;
        private int m_nInteger;
    }
    
    // good according to Microsoft coding standards
    class MyClassGood
    {
        private string _theString;
        private int _theInteger;
    }
    Any thoughts ? I still like Hungarian notation - it's easy to understand and you always know what type the variables are.

    And C# is so close in syntax to C++ then why not use Hungarian notation ?

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  2. #2
    Join Date
    Oct 2004
    Location
    South Africa
    Posts
    86

    Re: Coding standards

    hi
    Microsoft also suggest that using _ infront of a variable is bad as well.
    __property they may just come up with _property the next time and then we all have to change our code.
    I also think Hungarian notation is good, but it looks very messsy
    like
    MyCar* m_pMyCar // that is fine
    PineApple* m_pPineApple //that looks ugly
    instead
    MyCar* m_myCar // camel casing
    PineApple* m_pineApple
    then there the thought of how do we know its a pointer or a member variable
    well intellisence can help, also reading the code will the you
    m_myCar.GetName();
    my_myCar->GetName();
    what you think

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Coding standards

    Just my opinions:

    Compiler doesn't take care about variables names. If you obey syntax of C# for compiler, all other aspects of your code are for human readability. You should write the code like you were writing a story. You write "John eat an apple", not "Man-John do-eat fruit-apple." In other words, I agree that Hungarion notation is obsolete.

    To underscores (_) - only one argument I have heard for using is that if you use it, you can see all fileds together in intellisence. I strongly disagree with writing code for IDE instead for developer. If you want to distinguish between e.g. property and filed, you can use casing and this keyword.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Coding standards

    I prefer to use hungarian... It makes me feel so clear when i am reading the code after 3-4 years )

    I use _ for globals and m_ for locals... works perfect...
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  5. #5
    Join Date
    Jan 2005
    Location
    Brighton, England
    Posts
    241

    Re: Coding standards

    Hungarian notion is obsolete and even though I used it for many years, I am moving away from it.

    I use the following;

    Code:
    // Local variable
    int thisIsAVariable;
    
    //Member Variable
    int thisIsAVariable_;
    
    and for methods
    
    void MyMethod();
    but everyone tends to use a slightly different style, so you should just use what you feel happy with.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Coding standards

    I also moved away from Hungarian notation since it does not make much sense in type-safe language any longer. Furthermore, it was never a really nice way in my eyes anyway just for the drawbacks it has (for example, it is not a type-safe system).

    I am basically using the same scheme as Darka for variables, however I use some more '_' in it... Nevertheless, one will never find any 'My' in my code...

    Furthermore, one should usually avoid leading underscores since these are reserved for library implementers...at least they used to be in C++...

  7. #7
    Join Date
    Jan 2005
    Location
    Brighton, England
    Posts
    241

    Talking Re: Coding standards

    obviously MyMethod() was just an example :-)

  8. #8
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Coding standards

    Just my very private opinions :

    1) Any coding convetion (CC) U use consistently is a good one!
    2) If you plan to share code within a team you must make an agreement about CC with the rest of team (or with organization for witch U write that piece software).
    3) If your code is open source somehow and U want that code more understandable for others - than U have to keep on standards (and Hungarian notation is pretty good for that)

    Now about c# CC:

    I really disagree that "Hungarian notion is obsolete" - look for 3)

    I agree with Andy Tacker (except that I prefer "_ " for privates and "m_" for public).

    It is true that "Compiler doesn't take care about variables names" but peoples really do care ...

    I've heard opinions that any Hungarian notation is obsolete since we have InteliSence and other goodies.... but how InteliSence will work on Notepad, Mail reader, or on printed materials (Documentations or books for example)?

    Personally I like Hungarian notation for C# too. But I've made some slight modifications in it. So it is not strictly HN but HN like .

    Best regards,
    Krzemo.

    PS: I came form C,C++ so it can affect my judgement .

  9. #9
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Coding standards

    I've never been convinced by Hungarian notation but then I've never had to do C++ in a large project. How does it help the developer? Or is it left over from the days when IDEs were nonexistent and you need all the help you can get when it comes to editing C++ code?

    Coming from a Smalltalk background, using meaningful variable names and editing code in a good IDE has always been sufficient. The transition to C# has been relatively painless because of Intellisense. Without it I would probably spend more time in MSDN help then actually coding.
    Useful? Then click on (Rate This Post) at the top of this post.

  10. #10
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Coding standards

    I have to agree that I like hungarian notation too, and I don't think it's obsolete either.

    I like knowing what type the variables are.

    I like using m_ for members too.

    I suppose I've been using Hungarian for such a long time in C++ that I've just grown used to it. Writing C# without it really does feel a little strange.

    However, hungarian notation breaks down in C++.NET (at least prior to the newest version on C++/CLR).

    Since all classes are in fact pointers it doesn't really make a great deal of sense to have a function like

    Code:
    void SetString(System::String *psString);
    Especially when they're viewed in the object browser in C#.

    What I tend to do is use Hunarian notation in source files, but non-hungarian (or rather de-referenced hungarian) in the header files i.e.

    Code:
    // in .h
    void SetString(System::String *sString);
    
    // in .cpp
    void MyClass::SetString(System::String *psString)
    {
    }
    I just wondered what everyone's thoughts were. Thanks a lot !

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Coding standards

    Quote Originally Posted by darwen
    I like knowing what type the variables are.
    Which is exactly one thing where hungarian notation can fail badly...since that is totally up to the developer...if you have declared an 'short' two years ago, it would be named like
    Code:
    short sVar;
    This variable has been used a several times throughout your application, passed to functions which takes a short etc.

    Now....after two years, you realize that 2 bytes isn't big enough any longer and you need it to be an 'int' instead...so, you have to change every occurence of this variable as well as any affected function argument etc. in order to change from 'sVar' to 'iVar'. And many developers did not and simply left it with 'sVar'...and thus immediately had wrongly commented code...

  12. #12
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Coding standards

    ...so, you have to change every occurence of this variable ...
    Search and replace?

    .. as well as any affected function argument etc.
    U have to do that anyway (not Hungarian Conv. named vars too).

    And many developers did not and simply left it with 'sVar'...and thus immediately had wrongly commented code...
    And how do we call them? Bad proggrammers ?



    Best regards,
    Krzemo

  13. #13
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Coding standards

    Andreas Masur:

    An example:
    Code:
     
    void DoSometing(int userid) //eg. 1234
    { ...}
    void DoSomething(string userid); //eg. Smith
    { ... }
    ....
    
    MyClass.DoSomething(uid);
    "MyClass.DoSomething(uid);" Try to guess which one "DoSometing" is called... .Of course U can moving around a mouse and waits for InteliSence to explain ... But (as I said before) it could be printed on paper or that fragment could be sended to U by email...

    But if U have HN variable than "MyClass.DoSomething(szUid);" helps U a litle bit, isn't it?

    Of course - As I wrote before Hungarian notation is good as long as U use it consistently.

    Best regards,
    Krzemo.

  14. #14
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Coding standards

    I know that there is search and replace, however, bottom line is...it takes time. Time that I usually spent somewhere else. Furthermore, many developers simply don't do it, thus, if I have to look through their code I cannot rely on the correctness of the notation and thus, it is worthless to me.

    Having said this (and all the above), I have never said that there is a wrong or right. Everybody is free to choose their own style...and thus, I will not discuss whether the hungarian notation is useful or not. For me it simply isn't...

    As to your example....I usually don't care which function is being called aside from my intention. Whether or not is hungarian notation is being used or not does not help myself, since I both know the variables and the functions...the more important thing in my eyes is simply naming variables meaningful...if that is being done, I usually do not need any additional type info.

    As I said, this is simply personal preference....you are free to use hungarian notation or any other as you like...

  15. #15
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Coding standards

    Quote Originally Posted by Krzemo
    U have to do that anyway (not Hungarian Conv. named vars too).
    Yes...but I would need to do this only at two place (declaration + defintion) whereas using hungarian notation, I would need to do it inside the function for every occurence as well...

    Quote Originally Posted by Krzemo
    And how do we call them? Bad proggrammers ?
    I never implied that neither mentioned it...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured