开发者

Is having a lot of data fields a bad thing?

In Java, is having ~50 data fields in a class of about 1000 lines a bad thing?

Edit: Thanks for the feedback. Has really opened my eyes and I'll try to delegate functionality across more c开发者_运维知识库lasses rather than have a single do-everything class. :)


The question is very vague. The answer can be both yes and no. Yes, it's a bad thing if you can break the functionality cleanly into more classes. No, it's not a bad thing if the class is well focused and the only reason you got 50 fields is because you NEED them all there.

I have, for example, a class of 900 lines with 70 fields, but that's because the class implements a bit of artificial intelligence and the parameters the AI algorithm uses are a lot of behaviors and thresholds which need to be defined there so that child classes tweak them. In my case, I would say that it's not a bad architecture, and my 14 years of software development seem to agree ::- ).

Sure, if the class would go over 1000 lines, I could break it down further into 2: movement AI and attack AI, but for now, it really makes sense in the context of my project to have it together because movement AI and attack AI are never applied independently: they work together, they should be in the same class. If I ever have to make an object which ONLY needs movement AI, then by all means, I'd break it apart into 2.


from Code Complete:

Be critical of classes that contain more than about seven data members:The number "7±2" has been found to be a number of discrete items a person can remember while performing other tasks (Miller 1956). If a class contains more than about seven data members, consider whether the class should be decomposed into multiple smaller classes (Riel 1996). You might err more toward the high end of 7±2 if the data members are primitive data types like integers and strings, more toward the lower end of 7±2 if the data members are complex objects.


Yes, it's a code smell and will make your code harder to read and modify. Your class will become a God object

Jeff has a nice article on them, all of us should read it.


Sounds bad, but who knows. You may actually have found an object that requires 50 fields, all of which are interrelated and required to be in the one object.

If not, and to stay sane, try to split it up into smaller parts where possible.


Short story; yes. Long story; this is very subjective. A class with 1000 lines seems very excessive. Are all those fields really related? A class should be focused (exhibit high cohesion). Try splitting your fields into smaller, more tightly focused classes.


I think the bad thing is to have a class about 1000 lines... Try to refactor it to do it smaller if it possible...


Not necessarily, but it often means that your class should be seperated into several classes as it is doing to many things or your class takes on responsibilities that should be in other classes. It depends on your application's design.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜