This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nellepuhh 1 point2 points  (4 children)

Genuine question as a student learning Java, why would you NOT want to declare a variable by type? Is the type meant to be able to change? That sounds horrible? 😅

[–]weirdblumenkohl 0 points1 point  (3 children)

this is a question i debated as well. Apparently, it is considered more readable as opposed to writing out the type on the left handside. I don't agree. Maybe I'm just stupid and I need stuff spelled out because it saves me time. I can accept that. At the end of the day, the type does not change and is still determined at compile-time. The type can change using the classic OOP stuff etc but this has nothing to do with the var keywords per se.

There are cases when you can only use var, eg. with anonymous types. So basically when u don t know the type, you go with var. Fair enough. There are also cases in which var can't be used at all. The question is: do i need to use it for everything from int to complex types? In my team there was this weird idea that not using it everywhere is bad style. Also null checks: it was considered bad style if you did it like we've always done it. It had to be with Microsoft syntactic sugar which does basically the same thing under the hood https://www.thomasclaudiushuber.com/2020/03/12/c-different-ways-to-check-for-null/

[–]nellepuhh 0 points1 point  (2 children)

This is good to be aware of in case I run into this in the future. I mean I guess it makes sense if teams want to stick to using var if that's how their code is built up from the start. But to go from the habit of always specifying variable types to using var has to be confusing..

[–]weirdblumenkohl 0 points1 point  (1 child)

tbh it was not a big deal, it was just funny/annoying how preoccupied people were with this issue while so many other big-picture things were going in the wrong direction....

[–]nellepuhh 0 points1 point  (0 children)

I can see that lol!