1

Is it a bad practice if I put the variables types in their identifiers. Like this:

String nameString = "Sergio"
String birthdayString = "January 2, 1990";
DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date birthdayDate = dateFormat.parse(birthdayString);
String filePathString = "/etc/passwd"
final File PASSWD_FILE = new File(filePathString);

I find it useful to figure out at a glance what type a variable is, but I can be missing something and doing something stupid. Can you give me some advice? Sorry for my bad english.

5
  • 3
    Yes. Commented Oct 19, 2015 at 0:41
  • Use a competent IDE, and you can hover any variable at any point to find the declared type if you actually need it, but if the type of the variable isn't clear from context, you usually have a bigger problem. Commented Oct 19, 2015 at 0:46
  • In your updated example, some of the "variables" are really constants and should have more intelligible names, like AMERICAN_DATE_FORMAT (and that one is actually provided in the JDK, at least for Java 8 date/time). Commented Oct 19, 2015 at 0:48
  • I use IntelliJ and it does not state the variable type when I hover variables (But maybe I dont know how to use IntelliJ properly), and even if it did that, it would still be a lot faster just looking at the code. Commented Oct 19, 2015 at 0:52
  • The only constant is PASSWD_FILE and now I corrected it. Commented Oct 19, 2015 at 0:55

1 Answer 1

1

It is purely a personal choice (like long variable names, too much or too little comments).

If the compiler is going to catch data type issues then it might be alright to code without type declaration and think of the program in a more abstract/logical mode. This would encourage the programmer to think and concentrate on the problem more than be bogged down in semantics.

But if it helps your readability of the program, then by all means use it. It's important that you and others are able to read and understand the code. Do whatever helps this more than less.

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

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.