Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

C# Coding standards


  • Please log in to reply
15 replies to this topic

#1 JonnyBoats

JonnyBoats

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts
  • LocationPhillips, ME

Posted 19 April 2011 - 11:49 AM

Please, I do not want to open a can of worms here :-)

I came across this blog post with a nice set of C# coding standards at http://weblogs.asp.n...s-document.aspx

Now I am not advocating that this "standard" is better than any other standard, what I am suggesting is that the use of some standard is generally better than not having any standard ;-)

As a parenthetical note, remember that the great thing about standards is that you have so many to choose from!

Anyway for someone new to C# programming and Netduinos I would encourage you to read this standard as at a minimum it will cause you to think about the best way to do things, and thinking is always good. Once you have some experience then you can develop your own set of standards.

#2 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 19 April 2011 - 12:43 PM

I came across this blog post with a nice set of C# coding standards at http://weblogs.asp.n...s-document.aspx

Will be looking at it to add some Coding Standards to my Netduino Tutorial :) If I can get at it ... been trying ... ok got it
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 19 April 2011 - 12:52 PM

I'll suggest this one, that is more complete and often refreshed.
I have read that rules in the past, then not long ago, and I think they're very well done.
http://csharpguidelines.codeplex.com/

However, totally agree that the important is the readiness, instead of a bunch of useless rules.
Cheers
Biggest fault of Netduino? It runs by electricity.

#4 Terry Massey

Terry Massey

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationPekin, IL

Posted 19 April 2011 - 12:57 PM

Please, I do not want to open a can of worms here :-)




I'm not arguing. or trying to open the can anymore, however I will put in my two cents. One statement in the standards, I do Disagree with but only partially. It says DO NOT use Hungarian Notation! I will say if you give me a code snippet to help you out with something. and there are variables declared out of the current context. Hungarian Notation is very helpful. I don't know everything about your code so I have to make a guess as to the type. and My guess could be wrong making it take 3-4 Times as long to help you out. I come from a background where Hungarian Notation was used well strictly. However I have since grown lazy since I moved to c# from VB and VBScript. Intelisense takes care of most of that question for you. but a snippet well is just that. it is a small snapshot of a great meaning. So it just helps.
Thanks,
Terry Massey

#5 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 19 April 2011 - 12:59 PM

http://csharpguidelines.codeplex.com/

I'll check that one too !

I want to build a C# guidelines for .net MF code :) not a full blown .net, like a cheat sheet so thanks for the links !
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#6 JonnyBoats

JonnyBoats

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts
  • LocationPhillips, ME

Posted 19 April 2011 - 01:52 PM

I'm not arguing. or trying to open the can anymore, however I will put in my two cents. One statement in the standards, I do Disagree with but only partially. It says DO NOT use Hungarian Notation! I will say if you give me a code snippet to help you out with something. and there are variables declared out of the current context. Hungarian Notation is very helpful. I don't know everything about your code so I have to make a guess as to the type. and My guess could be wrong making it take 3-4 Times as long to help you out. I come from a background where Hungarian Notation was used well strictly. However I have since grown lazy since I moved to c# from VB and VBScript. Intelisense takes care of most of that question for you. but a snippet well is just that. it is a small snapshot of a great meaning. So it just helps.


As an old 16 bit Windows C programmer, I used to be a big user of Hungarian Notation. You may wish to read http://www.joelonsof...cles/Wrong.html where Joel Spolsky talks about the difference between Apps Hungarian and Systems Hungarian. (Scroll to to the bottom of the post to find it).

I believe Apps Hungarian is extremely useful, but quite frankly today with managed code Systems Hungarian has lost much of its usefulness.
  • Michel Trahan likes this

#7 Terry Massey

Terry Massey

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationPekin, IL

Posted 19 April 2011 - 02:09 PM

As an old 16 bit Windows C programmer, I used to be a big user of Hungarian Notation. You may wish to read http://www.joelonsof...cles/Wrong.html where Joel Spolsky talks about the difference between Apps Hungarian and Systems Hungarian. (Scroll to to the bottom of the post to find it).

I believe Apps Hungarian is extremely useful, but quite frankly today with managed code Systems Hungarian has lost much of its usefulness.


very nice article...... I like where he says I use kind on purpose. type/kind... Well there is a difference. because if I used oSomeCustomObject... that is just useless... but then again the variable name should be descriptive. however if for instance you have a integer/double/long/decimal to me that is very important to differentiate. because precision changes things. Is this a whole number or a decimal value and how precise it is. however if I could easily see that the value is an integer because you put i in front well I know the amount of precision. and the number of bytes it is taking up. so if your having a out of memory issue well knowing the type is important. but yes I agree I really like the suggestion on the us an s.. I do tons of web programming and that is a nice way to handle that..... I might have to start doing that.
Thanks,
Terry Massey

#8 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 19 April 2011 - 04:15 PM

You may wish to read http://www.joelonsof...cles/Wrong.html where Joel Spolsky talks about the difference between Apps Hungarian and Systems Hungarian. (Scroll to to the bottom of the post to find it).

I believe Apps Hungarian is extremely useful, but quite frankly today with managed code Systems Hungarian has lost much of its usefulness.

Wow ! I was doing it all wrong LOL ! I was mixing up both System and Apps Hungarian, and prefering the Apps one but tried to continue on the System one too and hating it ... been off programming for 7 years ... thanks, now I know what to do, keep the good and throw the bad and ugly system hungarian from my habits :)

A big thanks for the article you point at ! Boy do I love it here, I learn so much !
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#9 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 19 April 2011 - 05:14 PM

Guys, I would add a little consideration about guidelines and the MF. The wonderful rules proposed on several documents, plus the *REALLY USEFUL* patterns, changed dramatically my life in better. My apps are much more readable, reliable and flexible. But... ...I also have found that often, using Netduino, we have hard time to apply that rules and patterns, so that we must accept compromises... I personally don't like that because it seems much more like programming a sweetened-C++ instead of C#, so... probably the best thing to do is to create a half-way guideline for system weaky of resources. One vote for Michel! Bye all!
Biggest fault of Netduino? It runs by electricity.

#10 theTroll

theTroll

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 19 April 2011 - 05:20 PM

Going to have to disagree with most here. Hungarian is evil. Why? Because if you are writing code in which you need Hungarian Notation then your code is not well written, especially in C#. The biggest question is about types. How do you know the type? Well any method should be a page or two at the most, as a general rule I define my types at the start of the method or in the method declaration. So that way people don't have to go looking all over for them. Variables and Method names should be self-documenting by the names you use. Don't use b if you mean socketBuffer, or p1 if you mean outputPin1. It should be that any person can sit down and read your code and now only know what the variables are doing but what you are trying to do. If you want to change the width for your PWM don't just write Width but write PWMChangeWidthOfPulse(int pulseWidth). As long as you write code like that anyone will be able to understand it.

#11 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 19 April 2011 - 08:15 PM

Buy "Framework design guidelines". /thread. It's pretty useless to try and invent additional standards and ignore the perfectly good one already laid out in a professional publication...

#12 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 19 April 2011 - 10:57 PM

Going to have to disagree with most here. Hungarian is evil. Why? Because if you are writing code in which you need Hungarian Notation then your code is not well written, especially in C#.

The biggest question is about types. How do you know the type? Well any method should be a page or two at the most, as a general rule I define my types at the start of the method or in the method declaration. So that way people don't have to go looking all over for them.

Variables and Method names should be self-documenting by the names you use. Don't use b if you mean socketBuffer, or p1 if you mean outputPin1.

It should be that any person can sit down and read your code and now only know what the variables are doing but what you are trying to do. If you want to change the width for your PWM don't just write Width but write PWMChangeWidthOfPulse(int pulseWidth).

As long as you write code like that anyone will be able to understand it.


I've resisted posting here because one persons perfect "standard" is another persons nightmare. But what "theTroll" has said IMHO, hits the nail on the head.

A book for me that really changed how I write code is Refactoring - Improving the Design of Existing Code by Fowler. I now write much cleaner code, with well thought out variable and method names. And code that no longer needs comments.

I know comments are a polarizing issue, but I eschew code comments. Because in the end, code comments lie. Maybe not today, maybe not next week; but when the code is changed, the comments are almost never updated to reflect the new reality.

#13 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 20 April 2011 - 12:39 AM

code comments lie. Maybe not today, maybe not next week; but when the code is changed, the comments are almost never updated to reflect the new reality.

Got that right lol
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#14 Pete Brown

Pete Brown

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationAnne Arundel County, MD

Posted 31 May 2011 - 03:41 AM

Self-plug. I've had this page up since before the framework coding guidelines books were printed.

http://10rem.net/art...-best-practices

Hope it helps.

Pete
Pete Brown - http://10rem.net (NETMF, C++, Windows, C64, and general geekery) Twitter: @pete_brown
I work for Microsoft. Opinions expressed here are my own and do not necessarily reflect those of my employer,our partners or customers.

#15 Eric Burdo

Eric Burdo

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts

Posted 31 May 2011 - 11:05 AM

I'm with Johny here... Read Joel's post on Hungarian notation. However, getting into Standards discussion, is like discussion politics. Everyone has their own opinion, and they never truly line-up with anyone else. Coding guidelines are one thing (don't declare things as generic objects unless you truly need them, which is rare), use the tightest scope as possible, etc. But trying to standardize on some other things... is just a nasty hornets nest.
~ Eric D. Burdo ~ http://brick-labs.com/

Today LED's, tomorrow, the world!!! Well, OK, maybe servos.

#16 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 31 May 2011 - 06:21 PM

Frame work design guidelines. Buy it. /end of thread
  • Dan Morphis likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.