My Authors
Read all threads
Thanks for all votes and insightful answers to the poll on usage of @java's var! Not unexpectly, replies range from "using var all the time" to "don't see the point of it". Yet one third never using var at all was a surprise for me. Some repeating themes from replies in this 🧵.
1⃣ Readability vs. writability: some argued var optimizes for writing code (less characters to type) at the cost of reading code (less explicit type info). I don't think that's the intention behind var. In fact, more (redundant, repetitive) code may read worse.
2⃣ var only or primarily used in test code: described by several folks as a good starting point for getting their feet wet with local variable type inference, before using it more widely.
3⃣ Structural typing: a sweetspot for var. E.g. when dealing with query DSLs returning complexily structured generic types, omitting them can greatly increase readability. Similarly, as @tagir_valeev put it, "'var' instead of 'Iterator<Map.Entry<String, Integer>>' is relieving".
4⃣ Missing support for val: some would like to have this for less verbose declaration of final local variables. This was considered, here's @BrianGoetz's explanation why it wasn't added (immutability more important for fields than local vars): mail.openjdk.java.net/pipermail/plat…
5⃣ var rarely used for anonymous types: being able to declare and reference members of local-scoped anonymous types was discussed with excitement by some back when Java 10 came out. No one mentioned it during this poll; seems like use cases for this are rare after all?
6⃣ My take-away: readability is key. If explicit variable type declarations provide clarity -- go and add them. If they're more noise than anything -- var is your friend. @stuartmarks' style guide is of great help when making this decision. ~~ Fin ~~

openjdk.java.net/projects/amber…
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Gunnar "Not Home Alone" Morling 🌍

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!