DEV Community

Discussion on: How to use the var keyword in Java? Example Tutorial

Collapse
 
skuzzle profile image
Simon Taddiken

It's worth mentioning that var is not intended to replace all your explicitly typed local variable declarations. It need to be applied with caution in order to not screw up the readability of your code.
For instance, the example above with the OutputStream declaration is not really chosen carefully. With var you are forced to put more emphasis on the naming of your variables. The statement from the example above could have looked like this:

var bos = processFile();
Enter fullscreen mode Exit fullscreen mode

Here you have no clue about what bos might be. Using acronyms like bos is a bad practice anyway but may become painful when using var. Use proper naming like this:

var outputStream = processFile();
Enter fullscreen mode Exit fullscreen mode

Also it might not be the best to compare Java's var with JavaScript's var as the latter is typed entirely dynamical.

Collapse
 
javinpaul profile image
javinpaul

I completely agree with you that naming becomes even more important with var now. A variable name like "bos" does make sense when you have ByteArrayOutputStream as type but with var, yes, we got to be more careful. Thanks for highlighting that part.

Collapse
 
abstratt profile image
Rafael Chaves

I'd question the practice of including the type name in the variable name. Good names denote role or purpose, not type.

In your example, it would matter what the data in the file is going to be used for (say, contentsToCompress, imageContents, dataToSort etc).