views:

207

answers:

6

I am increasingly aware that my code in any single file can often span hundreds of lines quite easily and although I know the implementation might be sound, it still feels messy and unorganised.

I understand that there are situations where a lot of code is neccessary, but whats the best way to organise it all?

I've thought about separating variables from methods, privates from publics and internals but I don't want to because I can't help thinking that the components of ONE class belong in ONE file.

This whole thing is compounded when I'm working with the codebehind of a WPF window, which always seem to grow at an exponential rate into one huge mess very quickly.

Also: C# has a keyword called partial, which allows you to split a class over any number of files without affecting the functionality. However, I have noticed that Microsoft only seem to use partial to hide generated code from you (Winforms / WPF.) Which leads me to question whether splitting a class simply because it has many lines is a legitimate use of partial - is it?

Thanks

+12  A: 

Separate your code into responsibilities. For each responsibility, define a single type. That is, follow the Single Responsibility Principal. Doing so will result in smaller units of code, each of which performs a very specific function. Not only does this result in smaller files, but also in better design and maintainability.

HTH,
Kent

Kent Boogaart
How does this apply to a WPF code-behind class?
Gabe
@Gabe: Most (often, all) of the functionality for a view resides in one or more view models. Following SRP implies each VM has a single responsibility, rather than lumping everything related to the view into one VM. That said, it is sometimes OK to have code in the code-behind, if it's strictly view related (not business logic) and isn't shared by other views (not a shared component or behavior). In that case, SRP is still being upheld because the responsibility is to "manifest the view", so to speak.
Kent Boogaart
+2  A: 

I tend to group properties, constructors, methods, and helper methods (private methods) together with regions. If I have a lot of methods, I create more regions based on what they do (especially good for overloads). And speaking of overloads, try minimizing your code with optional parameters.

As far as I understand partial means that the class exists in two separate files. Webforms and controls are partial because the other "part" of the file is the as[p|c]x file that goes with it.

Brad
Regions: just say no ;)
Kent Boogaart
@Kent - they are not so bad. They only burned my house down once.
Oded
Using regions in class-files is often a sign of bad OOD... And never hide complexity! Make it less complex ;-)
Yves M.
Seriously? You guys don't use region? Just how worn out *IS* your scroll wheel?
Brad
@Brad: Some people use the "Go to definition" feature and/or MetalScroll. And I've never seen a programmer's scroll wheel not working anymore ^^
AndiDog
Regions do have the potential to be abused. But used in the right way they are very useful. For larger classes e.g. UI I normally group methods that have similar responsbilities together, for example render, layout, initalization. You could use partial classes too, but I prefer not to have to flick between files.
Ian
+8  A: 

If your files are big because they contain a single class/struct that is big, then this is usually (but not always) a hint that your class is dealing with multiple concerns and can be refactored into a number of smaller, more specialised classes.

Paul Ruane
+1  A: 

I go on the theory that if you cant see an entire method on one screen (i.e. you have to scroll), you should break the method up into further methods - either in the same class or when the code will get used more than once into a helper class.

Vixen
+5  A: 

If I understand you, your main problem is that your forms end up being too big, which leads to the classes for those forms containing too much code, which is quite normal if your forms aren't very simple. The way to try minimize this is by using User Controls since if you move the controls to other classes, you also move the code behind to other classes.

It can sometimes make it a little more difficult to communicate between the controls, but that's usually more than made up for by the fact that the code in each class will be much easier to understand.

ho1
A: 

We use stylecop. It helps a bit because it enforces a structure on your code and an order for what should appear where. Hence you can then find your way around larger files a bit more intuitively.

AJM