#"var vs explicit types debate"

1 messages ยท Page 1 of 1 (latest)

tulip onyxBOT
#

@west obsidian has a question:

galiron

Guys I can't decide. Should I use var or the explicit type? Right now I still have the explicit ones. Any opinions?

#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Guys I can't decide. Should I use var or the explicit type? Right now I still have the explicit ones. Any opinions?

It ultimately depends on personal preference and the specific situation.

Using var can make code more concise and easier to read, especially in cases where the type is obvious from the initialization. It can also make refactoring easier, as you only need to change the type in one place.

However, using explicit types can make code more readable and understandable for others, especially if they are unfamiliar with the codebase. It can also provide more clarity on variable types at a quick glance.

Consider the readability and maintainability of your codebase when making this decision. It may be helpful to discuss with your team or refer to any coding guidelines in place.

In general, favor readability and consistency within your project. Use var when it enhances readability without sacrificing clarity, but opt for explicit types when it provides added context or clarity. Ultimately, consistency is key - choose one style and stick with it throughout your codebase.

tidal chasm
#

usually the consensus is that u can use var if its obvious from the right side

#

such as var person = new Person("John");

#

but shouldnt if its hidden behind a method

#

var person = findPerson("John");

#

its also generally okay if ur prototyping sth quickly

west obsidian
#

Yea, I know, but I just can't decide what to use. I like both approaches

tidal chasm
#

or if ur writing a book/readme or similar guide and want to not write too much and distract

tidal chasm
#

theres no clear opinion on that in the community

west obsidian
#

Thing is, with the var keyword I can visually easily spot declarations cuz var is highlighted differently

tidal chasm
#

u can pick the color for declarations in ur IDE also for the types

#

that shouldnt be a reason to use var over explicit types

rigid light
#

Java is a language like english. Don't think about how the computer will understand your code, think about how the reader will read it. Typicaly you want code to contain as less noise as possible but maximize the info the user has. E.g. ArrayList<String> array = new ArrayList<String>(); contains the exact same info as: var array = new ArrayList<String>(); However if like @tidal chasm said the type is inferred so only implicit, you force the reader to know that value, which CAN be fine. It just depends on who reads your code. But as this is normaly hard to know, it's a convention to write inclusive (not requiring to much knowledge to be able to understand the) code. However this is obv. opinionated too, as there is no consensus on how much knowlege the average programmer should have.

#

Also depends on what your writing. If your writing a compiler the average reader will prob. know more than the average programmer

rigid light
#

Good presentation indeed! Thanks for sharing that, didn't saw that either, will steal that link to share ๐Ÿ™‚

ionic flicker
#

@west obsidian my contribution to the discussion is that var should never be used with primitive types or Strings. int and long are basically the same number of characters to type, and the size is explicit. I'll pick int x = 5; over var x = 5; any day of the week. I also prefer String be explicit. String name = "Alice"; Sure you should be able to know from the RHS what it is, and the compiler does that, but I dont want to. It adds cognitive overhead for future reading and it takes almost an equal amount of time to type. It also adds consistency since you cant var a field or parameter.

contrary to Zabs opinion I actually prefer var for non-obvious or longer classes, since the type isnt really important and at that point the variable name, method name, or variable+method name should be obvious. like var person = personService.findById(1L); or var optionalPerson = personService.findById(1L); Adding a Optional<Person> is just cluttery and irrelevant to common dataflows. Also helps with massive return type names like AbstractCustomPokemonBuilderFactoryServiceFactory (exaggerated). But with some database entities they are huge, and adding an optional to that is even larger. If I'm in a RepoRequestController and I say var request = repoRequestService.findById(id); I dont even need the variable name to matter much, and I definitely dont want GheRepositoryRequestDto or worse

I also almost always var in iterables like for (var person : people) since the type should generally be obvious from the explicit signature of the Iterable List<Person> people = .... Especially in the case of Map.Entry<K, V> i'd rather just for (var entry : personMap.entrySet()). Also if you're locally creating a collection and you have List<Person> people = new ArrayList<>() you can't just turn that to var people = new ArrayList<>() it has to be var people = new ArrayList<Person>() which is the same length -1.

west obsidian
#

Yea I guess especially with the long return type values you are right.

fading torrent
#

I always prefer explicit types just easier to reason about but if it's nested data structures like a linked list of hashmaps or smtng, then Var

tidal chasm
#

as mentioned earlier, it can be great for prototyping or making tutorials/readmes

#

full types would only be distracting for this type of content

#

cause every reader immediately knows that this part is currently not relevant and moves eye focus on the actual interesting parts of the code instead

humble remnant
humble remnant