DEV Community

Discussion on: Should You Truly Never Use var?

Collapse
 
johnwolfe820 profile image
John Wolfe

I think most people agree with you. Kyle Simpson's point about using it in top level variables that are shared across multiple scopes I think is legitimate, but even in that context its only advantage is to make the code more readable. It seems to me that selectively trying to use it in those situations would be error prone, like you mentioned.

I'm wondering if anyone has legitimate use cases where var remains the only viable variable declaration.

Collapse
 
nesvand profile image
Andrew Nesvadba

I don’t think there’s going to be a case where var is the “only viable” - all we’re left with is considering code style conventions like the one Simpson has suggested.

Will I adopt it personally? Probably not but it’s an interesting idea all the same.

Collapse
 
donut87 profile image
Christian Baer

If you have a variable in top-level shared across multiple scopes, then you have other things to worry about. Constants are fine btw. but variables? Things that can change? Over multiple scopes? Didn't we already discover this as a code smell?