0

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(); 
3
  • It's not common practice to use underscore in Java variables. The second one would be more java-like style Commented Oct 31, 2017 at 11:19
  • If you had a collection of Computer objects, i.e. more than one, I'd generally expect to see some data structure being used, e.g. an array or List. This would remove the need for numbering the variables and reduce repetitiveness of code (depending on their actual usage). Commented Oct 31, 2017 at 11:22
  • When using constant it is prefer to be the whole field in capital letters and with underscore separation between each two words, and try to avoid numbers, ex: COMPUTER_ONE. Commented Oct 31, 2017 at 11:52

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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();

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.