Java Coding Style – Should Interfaces Have ‘I’ Prefix?

coding-styleinterfacesjavanaming

According to Should interface names begin with an "I" prefix?, I should not add prefix "I" to interfaces. However, I think prefix "I" in interface may be useful sometimes. For example, if the interface have totally different names with the implementation naturally, eg: "Fruit", "Apple","Orange", then I agree it doesn't need to have prefix. But sometimes, it is unavoidable to have a interface having similar name to the implementation, for example, in a game : Tool:

public interface Tool{
}

public class ConsumableTool implements Tool{
}

public class NonConsumableTool implements Tool{
}

I found it would be harder to find the codes that use "Tool" only instead of "ConsumableTool" (eg: search "Tool " to find the places that use Tool as parameter and something like :Tool t=…), because when searching the keyword "Tool", both the place that uses "Tool" and "ConsumableTool" would be listed, which sometimes I may just interested in finding "Tool" only. So I think

public interface ITool{
}

may help me to find the interface among the ".java" easier when I want to find which places use the interface only("ITool") instead of the implementation.

Also when feel tried after reading many lines of codes for fixing bugs, I may yo-yo to "Tool" (move into the Tool.java to view the code) to find implementation to fix bugs wrongly because of forgetting "Tool" is an interface only. Yo-yo to wrong place may be not a big problem, but adding prefix "I" may reminds me "implementations are not at here", reducing the chance of "yo-yo" to the interface accidentally, hence saving me a lot of time.

Code is usually being read more than modified, right? While whether knowing it is interface may be redundant to "Users", it helps developers to read and understand the code faster.

So my question is, are the reasons above rationale to add prefix "I" on interfaces?

Best Answer

Adding the I or not is a trade-off between the arguments you mentioned and the things already said in the top answer of the question which you linked to (which, by the way, does not clearly say "never use this prefix in Java"). Note these are all "soft" arguments, no general striking argument why using this prefix (or not) is clearly superior, or makes code an unmaintainable mess.

Hence, it all boils down to what was written in the second highest answer to that post: consistency. This means:

  • When you work alone and have to maintain a program fully on your own, without expecting it to become another team's responsibility, you can use the convention you like most - but once chosen, stick to that convention througout the code base.

  • When you work in a team, do whatever the programming style guide of your team says (and if your team's style guide does not cover this topic, discuss it with them). For Java, it is not unlikely the style guide will recommend against using the I prefix (for C#, it is not unlikely the style guide will recommend it).

Related Topic