Categories or partial classes: a template for the solution "God object code"?

Too big class and becomes inconvenient for work. In Objective-C, I will be tempted to use Categories to break the class, but then: will the categories break a house full of too much garbage into rooms? The same question applies to partial classes in C #. I suppose.

Under what conditions can categories be used to solve the "too big" code smell? When this is not correct and the class really needs to "be restructured or broken down into smaller classes?"

+4
source share
4 answers

A very good principle for reference is the SOLID principle . In particular, “S” means “Single Responsibility”

Principle of shared responsibility

the notion that an object should have only one responsibility.

When classes get too big, they probably have too many responsibilities. Can you identify two or more responsibilities within what the class does? If so, divide it into two or more classes. You can then combine them using façade or composite .

In other words:

  • the code that you have in the class should be divided into different classes in accordance with the principle of shared responsibility
  • God's original class becomes composite or facade: it uses all new classes, provides the same methods to the rest of the system, but does not implement any functionality on its own, except for the "conversion" of the old style, class calls in SOLID calls of the new style.

This means that regions do nothing to solve your problem from an object-oriented point of view. In fact, they are actually counterproductive as they help to hide the problem.

See also Jeff Atwood's article:

http://www.codinghorror.com/blog/2008/07/the-problem-with-code-folding.html

CodingHorror-regions

+8
source

I must admit that I never used Objective-C, but of course I used C #.

It is said that partial classes are the same as the classes dividing a class into several files do not make the class smaller, only divided in the file. Using the class will be the same.

Therefore, I do not agree that partial classes will solve this problem, they were invented mainly for other things, such as window shapes, wpf, auto-generated code. They are also useful in other situations where classes cannot be logically separated, but should usually be avoided.

I think you should split your class into several classes, the class starts to smell after 1k LOC (lines of code), also if the class is split into several files.

Use inheritance or divide a class into several classes related by fields and properties. In the example provided by chemicalNova, I would split this into several classes, not several files.

+2
source

I remember when I was a beginner, we had this divine class "BusinessService" or something like that. Every time someone locked him in TFS, you lacked luck. Therefore, I had this brilliant idea, why not divide it into partial classes. We ended up with something like "BusinessService1.cs" .. "BusinessService6.cs". It was a complete mess, and a complete disappointment to find where things are.

I think that every time you need to use a partial class, this is a design error. If Microsoft forces you to do this (wpf, winforms, etc.) - this is their design error.

+1
source

There is nothing wrong with breaking a class into partial ones. Its something that few developers use.

Personally, I like to divide the larger classes into partial ones, where the business side of things for each particle has similar functionality, but only if during development it seems that the specified class will become quite large. Otherwise, I separate related functions in scope.

As an example, if I have a "UserService" that is inside the data layer, I could split it into several partial files as follows:

UserServiceQueries.cs UserServiceUpdates.cs UserServiceInserts.cs UserServiceLogicalFunctions.cs 

.. However, they do contain partial UserService classes. I usually do not use ORM, so for me it is ideal, because each part of the related functions can become quite large (obviously this is a basic example).

In conclusion: take advantage of what is supplied. If you get the smell of code from your class, that is huge. You have only 2 options. Overwrite it or split it (if it should definitely be so big).

My opinion anyway.

-1
source

Source: https://habr.com/ru/post/1379756/


All Articles