3

Is it a normal practice, or a sign of a bad design?

For example I have a function that loads prefabs in a main logic class nearing 200 lines, and I can either move it out into a separate file LoadPrefabs.cs by making it partial or making it a separate class and calling extra code just by new LoadPrefabs(ref prefabs);

Am I missing something obvious, is there a better way to organize large classes?

2

2 Answers 2

6

If your class is too big and unwieldy when it's all in one place and you can easily see it all, what makes you think scattering it across multiple files and making it harder to correlate will be an improvement?

Partial classes are essentially a hack to make it easier for generated code, such as that produced by the form designer, to work together with manually written code. Using them to make manually written code work with other manually written code is generally considered a bad idea.

6
  • 3
    I agree in principle, though I disagree with the "hack" characterization. It's a bona-fide feature, and an important one in all code generation contexts. I could see a potential use case in avoiding source control merge conflicts, though I imagine that most developers would have the same view of that usage as they would have of #regions. May 4, 2016 at 1:33
  • So creating separate classes to execute extra code and referencing or passing objects to them is better for organizing code, even though it is clunkier passing potentially multiple objects between them? I just want to be clear on that point.
    – Serge
    May 4, 2016 at 1:51
  • @Serge: Using composition, you don't pay for what you don't use, unlike partial classes, which all go along for the ride even if you only use one partial (they are all merged together at compile time to create a single class). May 4, 2016 at 1:57
  • @RobertHarvey Yep, I understand that, only in this case I will use every one of those classes because they are essential to the logic, and I used partial as an example because it is the only other alternative I know how to split the code. I considered making them singletons, but I wanted to avoid static methods. I'll accept this answer just for the partial part of it.
    – Serge
    May 4, 2016 at 2:15
  • @Serge I think "even though it is clunkier" makes your question very loaded. Maybe the obvious way to rip apart this gigantic class seems clunky, but it seems unlikely to me that there isn't any good factoring.
    – 31eee384
    May 4, 2016 at 22:57
2

Refactor the big function. It probably does several things repeatedly or nearly so, if it loads several "prefabs". Then, the logic needed for repetition can be put into other functions, which could open more possibilities for refactoring (one or more new classes for loading "prefab" types, maybe?).

Moving the code to a partial class will only obscure the fact that your class is bigger than it ought to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.