Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to come up with clearer interface names?

Tags:

I saw in an application where it had interfaces such as:

IHasContent IHasValue IHasMesh IHasGeometry IHasTransformation 

Should they not be?:

IHaveContent IHaveValue ... 

Or?:

IIncludeContent IIncludeValue ... 

Personally I am leaning towards just making them:

IContent IValue IMesh IGeometry ITransform 

Because isn't ISomething already implies that it has that something?

As for the last one, should I make it ITransformable instead?

I think using I + (Has/Have/Include/Exist, etc) + Name makes the interface names more confusing.

Any ideas on how to come up with better interface names that doesn't feel awkward, is to the point, and gets the meaning across?

like image 603
Joan Venge Avatar asked Mar 10 '11 22:03

Joan Venge


People also ask

How should you name interfaces?

Interface names should be capitalized like class names. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

How are network interfaces named?

Network interface names are based on whether the interface is a physical or virtual network interface. Physical interfaces are assigned names based on the slot number of the adapter. Interface group names are user specified. VLANs are named by combining the interface name and VLAN ID.

How do you name an interface class?

Naming InterfacesInterfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .

Should interface name start with I in Java?

There are two main naming conventions for interfaces in Java: no prefix or suffix for interfaces, ~Impl suffix for implementations, ~I prefix for interfaces.


1 Answers

Some of these names (Content, Value, etc) are vague, and do little to describe the content/behaviour of an item. In general, names should be as specific and distinct as possible - IScriptParameter might be more descriptive than IValue. As your project grows, having more descriptive names will make your types much easier to distinguish (if you're not careful you could end up with IValue and INumber and IAmount to handle variations of "values"!)

If your interface (e.g. IMesh) means "provides the properties of a mesh", then IMesh is a perfectly fine name - it describes the fact that you can treat the object as if it were a Mesh.

If your interface is used to apply an action (eg. to render the object as a mesh, or to apply a transform to the object), then consider using a verb/adjective rather than noun naming (e.g. IRenderable, ITransformable) - this is a common pattern in .net (IEnumerable (verb/adjective) rather than ICollection (noun), for example)

To me, "IHasMesh" sounds more like IMeshContainer - i.e. it is an object that contains a mesh, and the interface allows me to "get the mesh". So it would not allow me to act on or query data within the mesh, but simply fetch an entire Mesh object through the interface.

So I would use:

  • ITransformable if the object can be transformed via the interface
  • ITransform if the object can be used directly as if it is a Transform
  • IHasTransform/ITransformContainer/ITransformProvider if the object is a container that can be queried to extract a Transform object
like image 162
Jason Williams Avatar answered Nov 03 '22 18:11

Jason Williams