Suggestions for standard regions

S

Simon

Just starting on C# and would like to know what suggestions
there are for the order in which declarations, event
handlers, properties, private code, nested classes, etc,
etc appear in your files. What impact do access modifiers
have on the order? Also, does anyone use standardised
region names?
 
J

John Puopolo

Hi...

You may want to check for C# Coding Standard via Google or AllTheWeb.com.
Many of the published standards address this question.

John Puopolo
 
S

Simon

Yes, I have already found some good references from
Phillips, IDesign (Juval), Lance Hunt and MSDN. However,
none of them, unless I've missed it, tackle the issue of
what order to do things in. Of course, I can make my own
up, but I would have thought this would have been dealt
with. Perhaps it's not as interesting as brace placement or
the use of tabs which both seem to be hot topics of debate ;)
 
M

Michael S

Simon said:
Just starting on C# and would like to know what suggestions
there are for the order in which declarations, event
handlers, properties, private code, nested classes, etc,
etc appear in your files. What impact do access modifiers
have on the order? Also, does anyone use standardised
region names?

Over the years I've tried diffrent approaches and tried to have well defined
rules for all my classes.
Rules like starting withall public methods and public events, then the
protected and finally the proivate parts.

However, I've given up all such efforts.

I have found that no rules is best practice. But what I do try to do is
imagining that I am a maintainer who have never seen my code before. From
that I try to place the most important methods in the order they are
typically being called. If a public method calls three private methods, I
try to keep the private once close to the public caller.

Simple and obvious methods I put at the bottom of the source file.

Oh, and I tend to make sure that an class member variables are at the top of
the class.

- Michael S
 
J

Jeffrey Palermo, MCAD.Net

Here's the order of regions that I use:
fields
events
properties
constructors
instance methods
static methods

This is not a complete list, and I agree with Michael S mostly. There
is no way I could come up with rules for this and stick with them. The
above is what I _tend_ to do, but there are times when I create more
specialized regions to enhance maintainability. I also alphabatize
members within regions.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top