Java Magazine, Jan/Feb 2017

ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 60 java proposals of interest The following writeup is taken from public notes by Brian Goetz architect for the Java language at Oracle who then added additional information and observations It shows the care exerted by language designers when adding a feature that is small in appearance in this case the possible addition of a keyword var val or let to reduce the amount of boilerplate needed for declaring variables The syntax of the leading proposals was discussed in the March April 2016 issue of Java Magazine Ed More than 2500 people participated in two rounds of surveys on JEP 286 Overall the responses were strongly in favor of adding local variable type inference 74 percent were strongly in favor with another 12 percent mildly in favor 10 percent thought it was a bad idea The written comments had more of a negative bias but this shouldnt be surprising people generally have more to say in disagreement than in agreement The positive comments were very positive the negative comments were very negative While there were some passionate arguments against the numbers speak loudly this is a feature that most developers want It is the most frequently repeated request of developers coming to Java from other languages So no matter what happens here some people are going to be very disappointed When given a choice the most popular syntax choice was val and var with var only as the second choice But when asked how they felt about these choices there was a divergence more people liked val and var but more people hated it too Although people expressed strong preferences for their favorite syntax its important to remember that syntax is just the surface Language features like this have a lot of complexity under the hood even when they look simple Readability The biggest category of negative comments regarded worries about readability although most of these came from folks who have never used the feature those who have used it in other languages were overwhelmingly positive It is a wellestablished core design principle of the Java language that reading code is more important than writing code but plenty of folks assumed that this feature would inevitably lead to less readable code Of course its easy to construct strawman examples such as var x y getFoo to support the belief that this feature would harm readability But if you dig deeper you realize that the readability problem here stems from the fact that x is just a poorly chosen variable name Having a manifest type might make up for the programmers laziness but it would be better to just choose a good variable name in the first place Like any convention the use of var can do a lot of damage if not applied properly But the Java team believes that when it is used properly readability can actually be enhanced The reason for this is that it moves the variable name into a more predictable place in the code Consider a block of locals BRIAN GOETZ PHOTOGRAPH BY BOB ADLER VERBATIM JEP 286 Local Variable Type Inference var and maybe val

You must have JavaScript enabled to view digital editions.