Over at this question in the comments I mentioned that I heard that partial classes are best avoided if possible.
What if any is the reason for this sentiment? Or If this is an invalid sentiment, how are the perceived detriments overcome?
Aside the designer-generated code, partial classes can still be useful without compromising the code quality.
Examples of correct usage
A few usages I found particularly useful are the following:
Partial classes are the same as with regions
Partial classes have the same issue: if you rely too much on them, you can have an impression that your class is rather small, while it should have been refactored for months.
They may make the code difficult to navigate
Another flaw with partial classes is that it's not so easy to know where the stuff is. Take
Yes, Visual Studio has the F12 - Go to Definition feature, but if you can organize your code into files so the person can tell where a method is, do it.
One of the pitfalls is to think partial classes are somehow related to project modularity. This situation is perfect example.
Partial classes are only good if you have parts on single class created by completly different means. Common use is to have one piece of class generated by some tool (UI designer, model class generator, service generator) and another part added by developer. The tool-generated part can be regenerated at any time. Not so with the part added by developer.
There is one situation I was confronted with quite a couple of times before.
Although partial classes allow the concept of partial methods they don't allow partial constructors. So if you are using machine generated code it's most probably that there will be a constructor defined already which means you are unable to add custom construction code.
One common work around for this is of course to call other 'constructing methods' directly from the one defined constructor like having
but that requires the explicit use of such a pattern which is of course not the case when working with foreign or machine generated code.
Partial classes are a must in .Net in my experience. For example a window in WPF starts out as a partial class since a lot of the implementation is boiler plate that is automatically generated for you.
Another case is when using Entity Framework or other code generators. It will create objects that represent things in the database. You could add your modifications to do for example adding calculated properties to the class in the generated code file but that wouldn't be ideal because whenever you modify your model the code generator would wipe out your custom features. So you should add a partial class outside of the generated code that extends it to add the features you want.
No absolute rule can be made (even this one :-)) about best practices because the problems being solved can be so different. Example: don't use GOTOs except that at the lower level the compiler/assembler code has GOTOs all over the place to implement for loops and ifs.
I'd replace the no partials rule with: if things are so complicated you think you need to move part of the code to another file give a good thought over whether you need to break things down into smaller classes. If a smaller class solution doesn't make sense go nuts with partials.
One danger not yet mentioned with partial classes is that there is no way--short of adding an extra level of inheritance--of indicating that all object construction must be done through a particular constructor. Given:
One might hope that one could assume as an invariant that
then the previously-mentioned invariant would not hold.
IMHO there is only one case where using partial classes makes sense. But in that case it does make a lot of sense.
And that case is when you are using a tool to automatically generate code, e.g. based on a database schema, XSD schema files, or something else, and you need to add functions to the auto-generated code classes.
In that case, partial classes is a great tool to allow you to write custom code that doesn't get overridden the next time the code generation tool is executed. I had one example where we added some interfaces to classes generated from an XSD schema in order to avoid duplication in the code processing the corresponding XML data.
But of you are writing all the code by hand, splitting the code into partial classes is a code smell, an indication that your class is growing too big/having too many responsibilities.