Use Java Naming Conventions

 class​ Rover {
»static​ ​final​ ​double​ WalkingSpeed = 3;
 
»final​ String SerialNumber;
»double​ MilesPerHour;
 
» Rover(String NewSerialNumber) {
  SerialNumber = NewSerialNumber;
  }
 
»void​ Drive() {
  MilesPerHour = WalkingSpeed;
  }
»void​ Stop() {
  MilesPerHour = 0;
  }
 }

We have to name a lot of stuff in Java: packages, classes, interfaces, enums, methods, variables, fields, parameters, and constants. Take a look at the code above—it contains a lot of names. Do you notice anything strange? Can you see the pattern?

We consistently named everything in CamelCase: no spaces, and all words start with a capital letter. As long as we’re consistent, we can choose any way of naming, can’t we?

Well, in theory yes, but not in practice! The Java code out there that counts as professional follows a different style. To us, the code above isn’t just hard to read—it even feels horribly wrong. Just to be clear: There’s no functional error. But to a Java programmer, this doesn’t read like Java code.

How can we change the code so that it also looks like Java?

 class​ Rover {
»static​ ​final​ ​double​ WALKING_SPEED = 3;
 
»final​ String serialNumber;
»double​ milesPerHour;
 
» Rover(String serialNumber) {
 this​.serialNumber = serialNumber;
  }
 
»void​ drive() {
  milesPerHour = WALKING_SPEED;
  }
 
»void​ stop() {
  milesPerHour = 0;
  }
 }

Since 1997, we have the Java code conventions.[29] These conventions are the de facto standard for formatting Java code, including names.

Above, we changed the code to conform to the Java code conventions. We kept the name of the class because it already follows the conventions: it’s written in CamelCase, beginning with a capital letter and starting every term in the name with a capital letter. Interfaces and enums work likewise.

To let the constants (variables that are final and static) stand out, you should write their names in CAPITAL_SNAKE_CASE. This means that all letters in the name are uppercase, and you separate terms with an underscore. That way, the name of the constant already scream out “I won’t change.”

Methods, fields, parameters, and variables use a variant of camelCase where the first letter starts in lowercase. This has the downside of a possible confusion of method (behavior) and variable (state) names since they’re named in the same scheme. That’s why their name itself should tell the reader if it’s a method or a variable. You should name your methods as verbs, as you see in drive() in the code above, or let them start with verbs like is, has, save, get, or set, etc. For variables, use nouns, such as serialNumber or milesPerHour.

The Java code conventions are only the very first step in good naming. The point is that you’ll never have a good name if it doesn’t follow the conventions (the reverse, a bad name that follows the conventions, is entirely possible and all too common). Conventions are a basic requirement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.140.68