How do you personally organize your methods, event handlers (kinda the same), properties etc... in your classes?

I've been grouping things by type using regions and then alphabetizing them within the regions, but I was just wondering what other people do if there is a standard?

Recommended Answers

All 11 Replies

Stop using region things, unless your code is really looong. Regions are only for your or someone elses notification to find some code faster, and its better organized as well.
Usually we put fields, properties, delegates on top of the class (together in some order), below are methods and events.

is there anything else you would like to know, something more specifically=

Why stop using regions? Is there something bad about them? I like it because I can find things faster (or it seems like it anyway)

I didnt say you must not use Regions, sure you can, but they will only your code "less readable" is your code is "short".
Reagions comes right, I repeat, when you have a lot of code, maybe 1000 of lines on one *.cs file.

Otherwise, they are sure a good option to divide the code into peaces.
But remebember on thing too: its better to create more *.cs files then using Regions. The whole code still looks mor clear.

Depends on the class you are building.
I tend to put all my buttonclick handlers in a region and my MenuItemclicks in another region. There are no rules I guess. Just be consistent.

I'm the opposite of Mitja, I use regions all over the place. Usually have them for Properties, Constructors, Public Methods, Private Methods, Internal Classes (usually named Helper Classes), Events, and Interfaces, to name a few.


How do you order methods in your regions?

This is very kinda a personal question. Every one has his own opinion on this, this question is like "Which car you like?". Im sure you will get almost as many different answeres, as many people you will ask.

But try to hold one princial: Keep same kind of data (methods in this case) together.
I would recommend you ones again to split methods into classes, so one class would have similar method and stuff around.
And try to hold on this 3 or n - Tier Arcitecture. This means splitting data into sectors:
- data access - if you work with databases),
- buisiness layer - additional code, like some calculations, ...
- graphical user interface (code for controls)

How do you order methods in your regions?

Alphabetical, of course :)


Do you ever keep the default event handler names if you do things alphabetically? Like: "buttonApplyFilter_Click" or do you change the names?

I know it's a personal preference, but I'm curious, that's why I asked.

For 'real' applications (not ones where I'm just trying to show how something woks) I rename the controls (For example "ExitButton") but I leave the default handler names ('ExitButton_Click', etc.)

You can always tell the system generated items as I will never, never, never, ever put a '_' in a name. Cannot stand them.

Hmmmm interesting.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.