Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java data transfer object naming convention?

Given this scenario where you have "transfer objects" (POJO's with just getters/setters) which are passed by a client library to your API, what is the best way to name the transfer objects?

package com.x.core;   public class Car {         private String make;         private String model;          public Car(com.x.clientapi.Car car) {              this.make = car.getMake();              this.model = car.getModel();         } } 

In this example your main class and your transfer object both have the name Car. They are in different packages but I think it's confusing to have the same name. Is there a best practice on how to name the transfer objects?

like image 699
Marcus Leon Avatar asked Nov 12 '09 19:11

Marcus Leon


People also ask

How do you name data transfer objects?

Data Transfer Object classes should follow the name convention defined in the Java Language Specification: Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized.

Is there a convention for objects in Java?

Interfaces also use same convention. 2. Objects/Variables: Java Naming convention specifies that instances and other variables must start with lowercase and if there are multiple words in the name, then you need to use Uppercase for starting letters for the words except for the starting word.

Which is the correct naming convention for Java method?

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.

What are data transfer objects in Java?

A Data Transfer Object is, essentially, like a data structure. It should not contain any business logic but should contain serialization and deserialization mechanisms. DTOs can either contain all the data from a source, or partial data. They can hold data from single or multiple sources as well.


2 Answers

Data Transfer Object classes should follow the name convention defined in the Java Language Specification:

Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized.

ClassLoader SecurityManager Thread Dictionary BufferedInputStream 

[...]


Suffixing a class name with DTO or Dto is not really meaningful and doesn't tell much what the class itself represents. So it may be better to use names that describe the purpose of your classes.

Here is a non-exhaustive list of name suggestions you could use:

  • SomeSortOfCommand
  • SomeSortOfConfiguration
  • SomeSortOfCredentials
  • SomeSortOfDetails
  • SomeSortOfElement
  • SomeSortOfEvent
  • SomeSortOfFilter
  • SomeSortOfHeader
  • SomeSortOfInput
  • SomeSortOfInstruction
  • SomeSortOfItem
  • SomeSortOfMessage
  • SomeSortOfMetadata
  • SomeSortOfOperation
  • SomeSortOfOutput
  • SomeSortOfPayload
  • SomeSortOfProjection
  • SomeSortOfProperties
  • SomeSortOfQueryParameter
  • SomeSortOfQueryResult
  • SomeSortOfRepresentation
  • SomeSortOfRequest
  • SomeSortOfResource
  • SomeSortOfResponse
  • SomeSortOfResult
  • SomeSortOfRow
  • SomeSortOfSettings
  • SomeSortOfSpecification
  • SomeSortOfStatus
  • SomeSortOfSummary

Note 1: Whether acronyms or all capitalized words should be handled as words or not, I guess it's up to you. Check the Java API and you will find some stumbles like ZipInputStream / GZIPInputStream. Both classes are in the same package and the name convention is not consistent. HttpURLConnection doesn't show any consistency with acronyms either.

Note 2: Some names listed above were borrowed from this article written by Richard Dingwall (the original article seems to be no longer available, so here's a cached copy from Web Archive).

like image 130
cassiomolin Avatar answered Sep 24 '22 04:09

cassiomolin


I generally add 'DTO' to the end of the Class name as well as place all the DTO's in their own package. In your example I would call it com.x.core.dto.CarDTO.

like image 31
IaCoder Avatar answered Sep 23 '22 04:09

IaCoder