公共静态方法 - 一个坏兆头? [英] Public static methods - a bad sign?

查看:99
本文介绍了公共静态方法 - 一个坏兆头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚读过这篇文章: http://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.html ,最后一点让我想到了我的代码,特别是建议:


世界上哪一个公共方法对你的对象做了什么,它不依赖于对象中的任何字段?这肯定是代码味道。问题是检查的自动修复是应用static关键字。真是没有。那不是你想要做的。不依赖于对象状态的公共方法不可能是具有明确声明的章程的对象的一部分。它只是没有凝聚力,应放在其他地方。所以:如果方法是私有的,接受自动修复,但如果方法是公共的,则不要。


代码问题本质上是一个对象变换器。它需要一个A类型的对象并将其转换为另一种类型。



我的层次结构是这样的:



接口ObjectTransformer - > GenericObjectTransformer



然后在这下面,GenericObjectTransformer由ObjectTransformerA和ObjectTransformerB扩展



现在, ObjectTransformerA和ObjectTransformerB都需要一些功能,但实际上并不依赖于GenericObjectTransformer的任何实例变量,因此它是GenericObjectTransformer中受保护的静态方法。



这是否违反上面的规则?显然这是受保护的而不是公开的,但它仍然是一个可以从类外部访问的方法,与类本身无关?



有什么想法吗?

解决方案

我不同意你提取的摘录。


不具有对象状态依赖性的公共方法不可能是具有明确声明的章程的对象的一部分。它只是没有凝聚力,应放在其他地方。所以:如果方法是私有的,接受自动修复,但如果方法是公共的,则不要。


仅仅因为方法是静态的并且与状态无关,并不意味着它属于低内聚类别。凝聚力/功能性不是基于状态。



当您尝试确定Cohesiveness时,请考虑整个类的角色,而不仅仅是实例变量。如果您正在查看的逻辑与通用概念(GenericObjectTransformer)相关,则将其保留在那里。



如果是计算月球轨道的常规,或者海洋的深度将其移动到公用事业类(我们场地的另一个臭区)。 / p>

I've just read this article here: http://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.html, and the last bit in particular got me thinking about my code, specifically the advice:

What in the world is a public method doing on your object that has no dependency on any fields within the object? This is certainly a code smell. The problem is that the "auto-fix" for the inspection is to apply the static keyword. Nooooo. That's not what you want to do. A public method without any dependency on object state can't possibly be part of an object that has one clearly stated charter. It's just not cohesive and should be placed somewhere else. So: if the method is private, accept the auto-fix, but if the method is public then don't.

The code in question is essentially an object transformer. It takes an object of type A and converts it to a different type.

My hierarchy is like this:

Interface ObjectTransformer -> GenericObjectTransformer

and then below this, GenericObjectTransformer is extended by ObjectTransformerA and ObjectTransformerB

Now, some functionality is required by both ObjectTransformerA and ObjectTransformerB, but doesnt actually depend on any instance variables of GenericObjectTransformer, so its a protected static method in GenericObjectTransformer.

Is this a violation of the rule above? Obviously this is protected rather than public, but its still a method accessible from outside of the class that has nothing to do with the class itself?

Any thoughts?

解决方案

I disagree with the excerpt you pulled.

A public method without any dependency on object state can't possibly be part of an object that has one clearly stated charter. It's just not cohesive and should be placed somewhere else. So: if the method is private, accept the auto-fix, but if the method is public then don't.

Just because a method is static and has no relation to state, doesn't mean it falls under the "low cohesion" category. Cohesion/Functionality isn't based on state.

When you are trying to determine Cohesiveness think about the role of the class as a whole, not just the instance variables. If the logic you are looking at is related to the generic concept (GenericObjectTransformer) then leave it there.

If it is a routine to calculate the orbit of the moon, or the depth of the ocean move it to a utility class (another smelly area of our field).

这篇关于公共静态方法 - 一个坏兆头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆