Home > Blockchain >  Java class name convention for class holding entity associations
Java class name convention for class holding entity associations

Time:10-27

I have a pattern of classes that will be used to hold associations between different entities. I'm having a hard time thinking of a naming convention for such a situation.

For example:

public class ClassName {
  private Entity1 entity1;
  private List<Entity2> entity2List;
  private Set<Entity3> entity3Set;
}

In the above example "entity1" is the primary entity and "entity2List" and "entity3Set" are data relations.

CodePudding user response:

There is no hard time in it. because the names you used are according to the naming conventions in java. But try to use meaningful names for variables and classes to improve the readability of the code. unless you may fall into a difficult situation when you need to check this code again after a while.

CodePudding user response:

Generally, my naming of classes depends on whether they will be exposed in an external API and who the clients will be.

There is nothing wrong with:

private Set<Entity3> entity3Set;

But the code will now require:

getClassName().getEntity3Set()...

Where it could have been:

getClassName().getEntity3s()...

Both namings are perfectly valid; I have just found that omitting the Collection type in the property name slightly reduces code-clutter and therefore allows for code easilier read by the business domain experts, who probably don't care about the differences between Sets and Lists.

On the other hand, including the Collection type might be beneficial when the API users are Java coders, as displaying the type does convey information about data structures.

  •  Tags:  
  • java
  • Related