Hacker News new | ask | show | jobs
by chrislynch42 3013 days ago
var seems like a bad idea to me. One of the things I like about Java is (strong?) typing. Recently learning C# and was put off by 'var'. Why is it a good idea?
4 comments

'var' in C# and Java doesn't change strong or static typing at all. It's just type inference. Your code is still statically typed, the compiler and IDE will catch errors. It's just that it's smart enough to infer the types of redundant things instead of making you type it.

So instead of typing:

  AbstractConcreteFactoryFactory factory = new AbstractConcreteFactoryFactory();
You can just type it only once:

  var factory = new AbstractConcreteFactoryFactory();
It drastically decreases mental/physical overhead of typing the dang code out without in any way affecting typing.

C# is a huge breath of fresh air compared to java largely because of useful type inference. It's easy to make compilers do more work now.

Rust is doing type inference with drastically stronger types, for example.

Variables declared with var are still strongly typed. Just because the keyword exists in JavaScript, it doesn't mean it has the same semantic meaning.

    var p = new Person();
    
    p = 1234; // This will trigger a compile time error
depends, writing something like:

`Simple<List<Map<String, Demo>, OtherList<Map<Int, String>>` is a little bit painful.

I use scala and basically most often at least public Methods should (in scala you can omit even that, however some functional libraries need a return type) have a explicit type, which most often is enough.