简体   繁体   中英

Java Code Style

Does Java Code Style say anything about numerical suffix in variable naming? Which of this two options is acceptable (or maybe both)?

final Computer computer_1 = new Computer();
final Computer computer1 = new Computer(); 

Java doesn't say anything in particular about numbers in their naming conventions. Although they do mention camelCases to be the general convention for objects like in your case

final Computer computer1 = new Computer() would be the preferred option here Although its not a good naming practice. You would generally name your objects to describe their use

as you can read here , computer1 is the standard. Underscore should e used only in constants.

There is wrong or right. But if you want to use a standard, I would recommend the Google Java Style Guide

Due to this: Variables in Java should be written in lowerCamelCase. So it is:

Computer computer1 = new Computer();

If we consider that Oracle's coding style is the official one (except Constants), then underscoring is not official. So the second choice is more acceptable.

The name of a variable, function or class should answer all the big questions. It should tell you why it exists, what it does, and how it is used.

As computer1 is standard and computer_1 is not, use the always standard version to facilitate the work to future developers that maybe are going to read your code.

You can find lots of java coding style guides if you browse through the internet. some of answers mention here. From a different perspective lets see a scenario where name consist of 2 words ( computer one ) instead of computer1 .

There are 3 ways commonly declare this.

01.camelCase

Computer computerOne = new Computer();

02.underscore_separated

Computer computer_one = new Computer();

03.nospaces

Computer computerone = new Computer();

i would say most understandable way of declaration is camelCase. Secondly no spaces. Underscore separated method used mostly in pl-sql or any sql related programming languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM