Skip to content

Policy on var Keyword #3

@AB-xdev

Description

@AB-xdev

Problem

Some people like to use the var keyword. Usually these people don't care about reviews.

Because it's always cool to read the following as a reviewer:

var deliveryState = deliveryDao.find(...); 

// dozens of lines of code

deliveryState.id(); // It's a DeliverState entity



var sendState = sendDao.find(...);

// dozens of lines of code

sendState.isEmpty() // Surprise it's an Optional. 

// How can you tell without an IDE? 
// Answer: YOU CAN NOT!

Widespread usage of var effectively makes Code Reviews via Web UIs very hard.

var can be tolerated if it's used on very long Types descriptions (e.g. Map.Entry<MySuperCoolCacheKey, MySuperCoolValue> entry) with good variableNames however otherwise I see no reason why it should be used in the first place, because IDEs have automatic variable extraction capabilities that automatically extract the correct type:

ImageImage

See also: OpenJDK's Local Variable Type Inference CSG

Proposed solution

Prevent the use of var in general or if the total type description length is below a certain limit (somewhere around 30 chars)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions