Using “Base” in a Class Name

前端 未结 13 2027
旧巷少年郎
旧巷少年郎 2020-12-30 21:07

Is it acceptable to use the word \'Base\' in a class name which is a the bottom of the inheritance tree?

I have always found this a bit of a cop-out, just wondering

13条回答
  •  迷失自我
    2020-12-30 21:52

    Classes that have the same name as their parent classes bug me to no end. In Java java.sql.Date extends java.util.Date. This is very annoying because you have to specify the exact class you want to import or else specify the classname fully (including package/namespace).

    Personally I prefer to name things as they are; if a Base or Abstract class exists only to provide a partial implementation of something, and doesn't represent the interface for that thing, it is often acceptable to put the word Abstract or Base in its name. However, if that class represents the interface as well, then you should just name it after what it does.

    For example, in Java, we have the Connection interface (for DB connections). It's just called Connection, not IConnection. You use it like this:

    Connection con = getConnectionFromSomewhere();
    

    If you are making a JDBC driver and need to implement connection, you could have a ConnectionBase or AbstractConnection which is the lower layer of the implementation detail of your particular Connection. You might have

    abstract class AbstractConnection implements Connection
    
    class OracleConnection extends AbstractConnection
    

    or something like that. The clients of your code, however, never see AbstractConnection nor do they see OracleConnection, they only see Connection.

    So, in general, classes that are meant to be generally useful should be named after what they represent/do, whereas classes that are helpers for code maintenance/organization can be named after what they are.

    *ps I hate naming Interfaces with I. Do people name all their classes with C? It's 2009! your IDE can tell you what type of object that is, in the odd case when it even matters if it's an interface or a class.

提交回复
热议问题