How to organise large code files?
Asked Answered
R

7

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

Rafaello answered 23/9, 2010 at 13:43 Comment(2)
If you can split a class into several partial files that are logically separate, perhpas you should break the class up to several classes instead. You might have too much responsibility in one place.Penalize
Only use partial for partialy generated classes.Obliging
D
13

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.

Destinee answered 23/9, 2010 at 13:48 Comment(1)
@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.Destinee
M
9

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.

Milkwhite answered 23/9, 2010 at 13:49 Comment(0)
E
5

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.

Ectopia answered 23/9, 2010 at 13:56 Comment(0)
T
3

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.

Theorem answered 23/9, 2010 at 13:48 Comment(5)
@Kent - they are not so bad. They only burned my house down once.Penalize
Using regions in class-files is often a sign of bad OOD... And never hide complexity! Make it less complex ;-)Obliging
Seriously? You guys don't use region? Just how worn out IS your scroll wheel?Theorem
@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 ^^Notability
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.Schnozzle
A
1

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.

Altimeter answered 23/9, 2010 at 13:50 Comment(0)
D
0

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.

Drusy answered 23/9, 2010 at 14:49 Comment(0)
G
0

To improve code readability: you can use the region block: https://msdn.microsoft.com/en-us/library/9a1ybwek.aspx . As for improving the structure and design of your code - consult some specialist books.

Gauge answered 22/1, 2016 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.