Scrivio Interview library
LeadershipIntermediate6 min read · updated 12 November 2026

Your team argues about var in code reviews. Some want it everywhere, others want it banned. What guideline would you write and why?

I would write a short guideline rather than a blanket rule: use var when the type is obvious from the right-hand side, such as a constructor call or a clearly named factory method, and avoid it when the initializer hides the type, when the exact numeric type matters, or when we want a variable to be an interface type rather than the concrete class. Because var moves meaning from the type to the name, the guideline also asks for good variable names. I would publish it in the style guide with examples, referencing the OpenJDK style notes on var, and let reviewers point to it instead of arguing each time.

Background to this question

Style arguments in code review are expensive: they slow down every pull request, they create friction between people, and they rarely change anyone's mind. A senior engineer's job is to turn the argument into a short, agreed rule that people can apply without debate. var is a good test case because both camps have a point: var can remove noise, and it can also hide information a reader needs. In this answer, we will look at what each side gets right, write the rules for when var helps and when it hurts, cover the subtle cases of interface types and numeric literals, and then talk about how to introduce the guideline so the arguments actually stop.

1A Quick Recap of What var Changes

var lets the compiler infer the type of a local variable from its initializer. The type is still static and checked at compile time; what changes is only what the reader sees. With an explicit type, the declaration tells the reader the type. With var, the reader must get it from the right-hand side, the variable name or the IDE. So the whole question is about readability: when does the written type help the reader, and when is it just repetition?

2What Both Sides Get Right

The people who want var everywhere are right that some declarations are pure noise. In Map<String, List<Order>> ordersByCustomer = new HashMap<String, List<Order>>(), the type is written twice and the line is hard to read. Removing it lets the variable name and the logic stand out.

The people who want it banned are right that some declarations become puzzles. In var result = service.process(request), the reader cannot tell whether result is a Response, an Optional, a CompletableFuture or a list, and code review happens in a browser where there is no IDE to hover over the variable. A blanket rule either way loses one of these benefits, so the guideline should describe the cases.

3When var Helps and When It Hurts

The core rule is simple: use var when the type is obvious from the right-hand side, and write the type when it is not. A constructor call, a static factory with a clear name, a cast, or a literal for a well-known type all make the type obvious. A call to a method whose name says nothing about the result does not.

Java
// Good: the type is visible on the right
var orders = new ArrayList<Order>();
var customer = Customer.of(id, name);
var reader = Files.newBufferedReader(path);
for (var entry : ordersByCustomer.entrySet()) { } // long Map.Entry type, obvious from context
// Avoid: the type is hidden
var result = service.process(request); // what is result?
var x = repository.find(id); // Optional<Order>? Order? null?
Step throughShould this declaration use var?
  1. Is the type obvious from the right-hand side?
    YesContinueNoWrite the type
  2. Does the exact numeric type matter?
    YesWrite the typeNoContinue
  3. Should the variable be an interface type?
    YesWrite the interface typeNoContinue
  4. Does the name explain what the value is?
    YesUse varNoRename first, then use var

4 steps in order. Play them, or step through them one at a time.

The guideline also asks for better names, because with var the name carries more of the meaning. var pendingOrders = orderRepository.findPending() is readable even though the type is not written, while var data = load() is not, with or without var.

4The Subtle Cases: Interfaces and Numbers

Two cases deserve their own lines in the guideline because they change behaviour, not just readability. The first is interface versus implementation. var takes the type of the expression, so var list = new ArrayList<String>() declares an ArrayList, not a List. Usually that does not matter for a local variable, but it lets ArrayList-specific methods creep into the code, and it means we cannot later assign a different List implementation to the same variable. When the variable is meant to be used through an interface, writing List<String> states the intent.

The second is numeric types. var infers the literal's type, so var total = 0 is an int, and compound assignment silently narrows anything we add to it.

Java
var total = 0; // int, not double
total += 2.5; // compiles: means total = (int) (total + 2.5)
System.out.println(total); // 2, the fraction is gone
double sum = 0; // write the type when the numeric type matters
sum += 2.5;
System.out.println(sum); // 2.5
var big = 3_000_000_000L; // long, because of the L suffix

That is why the guideline says to write the type for numbers whose exact type matters, such as accumulators, money and anything mixed with other numeric types.

5Introducing the Guideline So the Arguments Stop

A guideline only ends arguments if people accept it. I would draft it in half a page, with the rules above and four or five good and bad examples taken from our own codebase, and point to the OpenJDK project's style guidelines for local variable type inference as the reference the rules are based on. Then I would ask both camps to review the draft, adjust it once, and publish it in the team's style guide.

After that, reviewers stop debating taste and simply link to the rule. Mechanical parts can be automated: IDE inspections and formatter settings can be shared in the repository, and some static analysis tools can flag var with literals or where the initializer is a method call. We also agree that existing code is not rewritten just to add or remove var; it changes only when someone touches the code for another reason. After a few months, we look back and adjust the rule if it is causing friction.

6Follow-up questions

Should var be allowed in tests?

Usually yes, under the same rule. Tests often create objects with constructors and builders, where the type is obvious, so var helps. Assertions about returned values sometimes benefit from an explicit type, because the type is part of what the test documents.

What about var with generics and the diamond?

The guideline should say to put the type arguments on the right when using var, because var list = new ArrayList<>() infers ArrayList<Object>. Writing new ArrayList<Order>() keeps the type precise.

How do you handle a senior engineer who refuses to follow it?

I would talk to them directly about the concern behind the objection, because it is often a fair one, such as reviews without an IDE. If the concern is valid, the guideline should change; if it is a matter of taste, the point of having a team rule is that individual taste gives way to consistency, and I would say that plainly.

7Conclusion

In this answer, we saw that both sides of the var debate are partly right: var removes noise when the type is obvious and hides information when it is not. The guideline we wrote uses var when the right-hand side shows the type, avoids it for opaque method calls, numeric types that matter and variables meant to be interfaces, and asks for meaningful names. We introduced it as a short style guide entry with examples, based on the OpenJDK style notes, reviewed by both camps and enforced by links and tooling rather than debate.

What to say in the interviewAbout 100 seconds aloud

Open with

I would not pick either extreme, because both sides are partly right. var does reduce noise when the type is obvious from the right-hand side, like new ArrayList<Order>() or Customer.of(...), and it hurts when the initializer hides the type, like var result = service.process(request), especially since reviews often happen in a browser without an IDE.

Then, if they want more

So I would write a short guideline. Use var when the type is obvious from the initializer: constructors, clearly named factories, casts, long generic types in loops.

Write the type when the initializer is an opaque method call, when the exact numeric type matters, because var total = 0 is an int and total += 2.5 silently truncates, and when the variable should be an interface type, since var picks the concrete class, so var list = new ArrayList<String>() is an ArrayList, not a List.

Because var moves meaning into the name, the guideline also asks for descriptive variable names, and for type arguments on the right with the diamond.

I would base it on the OpenJDK style guidelines for local variable type inference, add four or five examples from our own code, have people from both camps review it, and publish it in the style guide. Then reviewers link to it instead of arguing, IDE settings are shared, and we do not rewrite old code just for var.

After a few months, I would revisit it and adjust if it causes friction.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your team argues about var in code reviews. Some want it everywhere, others want it...": https://scrivio-interview-library.netlify.app/java/types-and-values/8af29bd7e5/