是单词“帮手”在类名中有代码气味吗? [英] Is the word "Helper" in a class name a code smell?

查看:112
本文介绍了是单词“帮手”在类名中有代码气味吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们似乎从网页中抽象了很多逻辑方法,并创建了帮助类。可悲的是,这些类都听起来一样,例如

We seems to be abstracting a lot of logic way from web pages and creating "helper" classes. Sadly, these classes are all sounding the same, e.g

ADHelper,(Active Directory)
AuthenicationHelper,
SharePointHelper

ADHelper, (Active Directory) AuthenicationHelper, SharePointHelper

其他人有这么多命名约定的类吗?

Do other people have a large number of classes with this naming convention?

推荐答案

它有资格作为代码气味,但记住代码气味不一定会引起麻烦。这是你应该看看,然后决定是否可以。

I would say that it qualifies as a code smell, but remember that a code smell doesn't necessarily spell trouble. It is something you should look into and then decide if it is okay.

我曾经说过,我个人觉得这样的名字增加了很少的价值,因为它是如此通用的类型可能很容易成为一堆非相关的实用方法。也就是说助手类可能会变成大类,这是常见的代码气味之一。

Having said that I personally find that a name like that adds very little value and because it is so generic the type may easily become a bucket of non-related utility methods. I.e. a helper class may turn into a Large Class, which is one of the common code smells.

如果可能,我建议找到一个类型名称,更详细地描述方法的作用。当然这可能会提示额外的助手类,但只要他们的名字是有用的,我不介意数字。

If possible I suggest finding a type name that more closely describes what the methods do. Of course this may prompt additional helper classes, but as long as their names are helpful I don't mind the numbers.

前一段时间我在代码审查期间遇到了一个名为XmlHelper的类。它有很多方法,显然所有与Xml。然而,从类型名称不清楚方法有什么共同之处(除了与Xml相关)。原来,一些方法是格式化Xml和其他人解析Xml。所以IMO类应该被分成两个或更多的部分与​​更具体的名称。

Some time ago I came across a class called XmlHelper during a code review. It had a number of methods that obviously all had to do with Xml. However, it wasn't clear from the type name what the methods had in common (aside from being Xml-related). It turned out that some of the methods were formatting Xml and others were parsing Xml. So IMO the class should have been split in two or more parts with more specific names.

这篇关于是单词“帮手”在类名中有代码气味吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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