Java 10 var: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:


Type inference reduces the amount of time it takes to write Java code, but in some cases does not necessarily improve its readability. Some say that developers spend much more time reading source coding than writing it, so probably the amount of time it takes to write the code should be optimized in favor of readability. This is an example where <tt>var</tt> does not improve readability:
Type inference reduces the amount of time it takes to write Java code, but in some cases does not necessarily improve its readability. Some say that developers spend much more time reading source coding than writing it, so probably the amount of time it takes to write the code should be optimized in favor of readability. This is an example where <tt>var</tt> does not improve readability:
<syntaxhighlight lang='java'>
</syntaxhighlight>


However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where <tt>var</tt> improves readability and it comes with an additional, not immediately obvious, advantage.
However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where <tt>var</tt> improves readability and it comes with an additional, not immediately obvious, advantage.

Revision as of 16:07, 25 May 2018

Internal

Overview

The feature behind the Java 10 new keyword var is called local variable type inference.

var and Code Readability

Type inference reduces the amount of time it takes to write Java code, but in some cases does not necessarily improve its readability. Some say that developers spend much more time reading source coding than writing it, so probably the amount of time it takes to write the code should be optimized in favor of readability. This is an example where var does not improve readability:

However, there may be cases when var actually improves readability by eliminating obviously redundant type declaration in a very local context. Logically, this is similar to naming a variable "i" instead of "internalProcessId" in a short loop. This is an example where var improves readability and it comes with an additional, not immediately obvious, advantage.