Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided i...
For further actions, you may consider blocking this person and/or reporting abuse
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:Here you have no clue about what
bos
might be. Using acronyms likebos
is a bad practice anyway but may become painful when usingvar
. Use proper naming like this:Also it might not be the best to compare Java's
var
with JavaScript'svar
as the latter is typed entirely dynamical.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).
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.
Many things which take just 5 minutes in languages like Python, Groovy, or JavaScript can take more than 30 minutes in Java due to its verbosity. This is such a bullshit. Variable declaration is a very small part of a class/method. Let's say a 5% of the total. If not declaring the type removes 50% of the declaration, the total gain is about 2%. Nothing to fuss about!
That's true and that's why Java is not for writing scripts or quick utilities but at the same time, I have found managing a large and super large project in Java much easier than let's say Python or JavaScript. Though I am not a Python or JavaScript expert, I have just happened to work on that, but that's what I have felt during my slightly limited exposure.
lol
Java var is nothing like Python's def. def is used to declare a function or method.
Yeah, that's true, Java var is less powerful than Python def or even JavaScript var but it still useful on making your code elegant and concise.
Haven't really understood it in Java or C#, I don't understand the gains except for not having to type a few characters, and you're loosing that nice readability in statically typed languages.
Also, var should be illegal now in javascript, having let and const.
It's not just typing but readability improves with more concise code but yes, you got to be more careful with variable names now because you just have one identifier to use to convey your intention and what the variable is and what it does.
Indeed :-)
Java does NOT have dynamic typing features, before or after java 10. var still is static typing.
Not really.