In C# how many lines before a class should be cons

2019-03-08 16:06发布

A good rule of thumb by is that I intelligently refactor any method over 50 lines.

The count does not include comments and white space but actual code. The reason I also say intelligently is there are plenty of times where a class over 50 lines is acceptable and cannot or should not be changed.

I do not have a rule of thumb for classes. Generally I don't check classes to see if they should be refactored.

On my currently project I have just about complete a class that is almost 4000 lines long. However no methods over 50, and most of the lines and methods are private and not acting on any data outside of the class.

What is the rule of thumb for refactoring classes?

10条回答
smile是对你的礼貌
2楼-- · 2019-03-08 16:28

I wouldn't say that there's any "rule of thumb" for refactoring large classes. Sometimes a class really encapsulates a lot of business logic and should be as large as it is. However, you might consider asking yourself a few questions:

  1. (Assuming you're writing object oriented code) Is my code truly object-oriented? That is, does it follow the Single Responsibility Principle (thanks, Nebakanezer)? Is this class a helper class? If so, how could I refactor its methods into more appropriate object classes?

  2. Do I have a solid architecture? That is, am I making use of abstraction and inheritance rather than re-inventing the wheel in every class? Are overloaded methods calling base methods as appropriate?

  3. Do I really need all this code? Could some of the logic be externalized using xpath, lambda expressions, or some form of database driven expressions?

  4. Is the code extensible? Is it easy to maintain? Was it well-architected from the beginning, or are we always making small patches to try to fix problems?

I hope that this helps a little; it can be difficult to refactor large classes, but I think if you start looking through my questions, you may find pretty quickly that there is room for improvement in your code...I know I typically do.

Especially look at #1--it's really common for people to create a ton of helper classes all over the place, which is very anti-object oriented (in my opinion). That's a different topic, but you may want to see what really -belongs- in the class you've made and what could/should be somewhere else.

As a rule of thumb, if the class is maintainable and flexible, it may not need to be changed. :)

查看更多
做个烂人
3楼-- · 2019-03-08 16:28

In C#, my rule of thumb for classes is anything over 500 lines is getting too long. I really like methods under 10 lines, and I guess under 20 is acceptable. I think it really depends on the context.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-08 16:32

This class could be a candidate for the refactoring exercise of "extract an object that you don't think is there"?

Perhaps, for example, you could extract a method object that would allow you to refactor several of the 50 line methods into members of an extracted class?

Alternatively, encapsulating some of those private methods that contain business logic in a new object that collaborates with this one might provide a new way to reuse either this object, or the extracted one.

One line is enough to consider refactoring. 40 each 50 line methods is starting to scream "I'm too complicated, there's another object hiding in here" to my ear.

查看更多
在下西门庆
5楼-- · 2019-03-08 16:38

When the class violates the SRP, it's time to refactor.

The single responsibility principle is a computer programming principle that states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility

查看更多
登录 后发表回答