4 ms·
Like many things in life, it depends. I quite like type inference when it's clear from context what the type will be, e.g.: var xs = new List<Int>(); for
by mrgriffin 7y ago
Like many things in life, it depends.
I quite like type inference when it's clear from context what the type will be, e.g.:
var xs = new List<Int>();
for (var x in xs) { ... }
I'm pretty happy for it when the type "doesn't matter" because you don't do anything with the value except pass it elsewhere.
// Presumably refactored from processSomething(x, y, z, getSomething()) because the line got too long.
var something = getSomething();
processSomething(x, y, z, something);
And sometimes the type can be sufficiently unwieldy to name, most famously in C++ taking advantage of templates, but also can show up in other languages such as Haskell that encourage this behavior.
Finally there's this comment:
> A weaker argument I have heard is that you have less code to type with type inference!
I'd say that I agree with it as stated, but I'd hope the people making that argument are actually trying to say "it's less code to read/be distracted by". IMO omitting the "less important" type information draws the reader's attention to the ones you did write, which are presumably the more interesting parts of the code.
Having said all that, I think my brain is a bit quirky because even in languages like Python I automatically/internally infer all the types based on the names of variables and roughly how/where they're used as I read through. Maybe it's an experience thing (or maybe I don't work with bad/differently-thinking developers), but I find that my first or second guess is right 99% of the time.
- bsder 7y ago// Presumably refactored from processSomething(x, y, z, getSomething()) because the line got too long. var something = getSomething(); processSomething(x, y, z, something); Sigh, people will add extra lines, refactor and risk introducing bugs simply to avoid a text line longer than a punch card from 1965.
- kjeetgill 7y agoYou gotta have a soft limit somewhere. I'll concede 80 characters or even 100 but 120 seems fair enough.
- lugg 7y agoI've always used a soft limit of 120. I use it as an indicator that I am either nested too deeply or my functions have too many parameters. Or just in general my code is too complex / hard to read after the fact. Sometimes things call to break the soft limit that is fine, it's only a signal of potential problems, not an indicator of definite problems. And fwiw, if you can't refactor like that without concern it's a language design failure. A pretty big one actually.
- mikekchar 7y agoMy terminal width is about 120 characters because my vision is poor enough that this is how many character fit on my monitor horizontally. I appreciate coding standards that limit the width of a line of code because it makes it considerably more accessible for me. Programmers with poor vision aren't that uncommon -- it's not completely out of the question that your team will hire one some day.