5 ms·
So much easier to read and write code with inferred types. Though my co-workers obsessed with writing ‘good’ code refuse to use them. They also refuse to write
by trixie_ 8y ago
So much easier to read and write code with inferred types.
Though my co-workers obsessed with writing ‘good’ code refuse to use them. They also refuse to write comments because their code is so good ‘it documents itself’.
- jstimpfle 8y agoI think most programmers end up in this place and I think they are right, at least concerning the reading part (which is often more important since most code is read more often than written)... How long have you been programming?
- int_19h 8y agoI disagree - there are places where explicit types help, but for most local variables they're just noise. An accurate name for a variable is much more important than a type when it comes to figuring out what the code does that involves it, and types are a mouse hover away in any decent tooling anyway. (I've been programming for 20 years, and mostly in statically typed languages.)
- trixie_ 8y ago20 years. I find reading code with inferred types much much easier. The signal is what the code is doing. The typings are noise. Edit: In a big complicated system, knowing the type like ‘user’ or ‘account’ doesn’t tell you much. There’s always more information and context. A small type definition is only the tip of the iceberg.
- josephg 8y agoThis is one of those aspects where a good editor makes a big difference. I've recently moved from writing javascript in vim, to writing typescript in vim, to writing typescript using VSCode. Being able to hover over a variable and see a variable's type makes it easier to read complex code. This is useful irrespective of whether or not you're using type inference, since you don't need to hunt down the variable's declaration to figure out its type. And if you're reading back the compiler's type information through your IDE while programming, you may as well take advantage of type inference.
- jstimpfle 8y agoI'm not a big fan of types, either. As a C programmer I use types pretty much only as much as they are required (indicate to the compiler how information is represented). It's mostly integers of various sizes, plus a few floats and structs and typedefs. Still, types in general convey more information to me than the variable name. For example, it's important to me to group fields in structures by their type to prevent superfluous padding and to get a feel of cleanliness. I tend to think of types as the relevant information that says what actually happens, and of names I tend to think as secondary information that gives an impression why it should happen.
- trixie_ 8y agoint[] UserIDs; If you had a choice of knowing the variable was an int[] or knowing it’s name was UserIDs the latter would be much more useful. This is why new languages are trending towards inferred types. Much easier to write and read (you get your padding taken care of for free as well when you use var) Also ‘what’ is happening is the actual flow and logic of the code. ‘Why’ it is happening is in your comments.
- jstimpfle 8y ago> (you get your padding taken care of for free as well when you use var) How so? Padding is inserted when the layout (order of fields) is bad. There are no language that reorder structures, are there? > Also ‘what’ is happening is the actual flow and logic of the code. ‘Why’ it is happening is in your comments. That's certainly one way to put it. But I was relating to the "show me your tables..." quote's kind of feeling. So, to be more clear, maybe more to the "what is possible" vs "what have the authors currently thought of / implemented".
- UncleEntity 8y agoWhile C++ auto is nice to save on some typing I think when you have to go look at the function definition to figure exactly what type you're dealing with makes it sometimes inconvenient. Admittedly, I don't use any sort of IDE that does fancy mouseover type checking and only occasionally poke at C++ but there's one lib I wrote[0] where I had to remove a bunch of autos so I could figure out exactly what was going on (and, ironically, make the code "self documenting"). --edit-- [0] OK, translated from java to C++ so I also had to figure out the logic behind it to do any sorts of modifications.
- mruts 8y agoI think explicit types for function arguments and return types are a good thing, even in languages that support their omission. For local variables, it's probably not necessary, unless the type is especially complicated. Though, I wouldn't call putting type annotations on all variables as a bad thing. As an aside, Intellij has this great future with Scala where it displays the inferred type of all functions and variables automatically.
- aasasd 8y agoTell them that self-documenting code doesn't exist, because code can't denote the rationale for its existence and how it's supposed to be used.
- trixie_ 8y agoBelieve me I’ve tried. There are a lot of them though and all pretty young and would rather agree w each other.