views:

84

answers:

2

Hi All,

What is the best way to format your c# code.

I'm looking to improve the way my code is formated. I curentlly put all my code into regions (i.e methods functions ect)

Is this the best way or are there other ways that work better?

Sp

A: 

I tend to use regions to group logically related code together - almost all code in a given class will be contained within a region in my code base.

Other than that, it's personal preference, though I will say that if you are working on a production code base for another company, do what they do - changing the formatting to the way you do it will screw up a lot of file differencing tools when people do historical comparisons in source control.

Adam
I'm glad to see someone supporting regions. It seems a lot of people on SO think regions are the devil.
Neil N
@Neil N http://weblogs.asp.net/bsimser/archive/2007/10/30/regions-evil.aspx
Yuriy Faktorovich
@yuriy - use specific then, I otherwise disagree - there is nothing slower than having to review the variables touched inside a method to see what's going on - grouping related methods and class state into a region helps.
Adam
@Adam Are the classes still following SRP if you're grouping related functionality a lot inside it?
Yuriy Faktorovich
They could quite easily do so yes. Also, I'd argue that code in the wild doesn't religiously stick to the ideal situation - using that as a rebuttal is also a bit academic - something you don't often find in production. Not everyone uses ReSharper either. Again on top of this, you could argue that regions aren't evil - the lack of support for them in refactoring tools is evil. Either way - its all personal preference unless there is an established standard otherwise.
Adam
While I agree that OVERUSE of regions is evil (the type of coder who would overuse regions would also overuse/underuse a lot of other things) but when used sparingly they are quite handy. I have maybe a dozen in a 30k line app.
Neil N
A: 

the "best" way is to follow your companies coding standards. Other then that, it is personal taste.

Muad'Dib