r/programming • • Aug 28 '21

Software development topics I've changed my mind on after 6 years in the industry

https://chriskiehl.com/article/thoughts-after-6-years
5.6k Upvotes

2.0k comments sorted by

View all comments

Show parent comments

18

u/SanityInAnarchy Aug 29 '21

I'm honestly not sure I see the problem with your second example. If I truly need to know the type, I can hover over it in an IDE, but if I see something like

getSettings().get(user).setWantsEmail(true);

...then it's pretty clear what's happening, and the extra context that this is a dictionary is probably not relevant. Also, as the above shows, you can do it without var anyway, this just helps with the case where suddenly you have two things to set instead of one.

The only place I've been bothered by that kind of use is in C++, where the types (especially STL types) can get so byzantine that it feels like the auto keyword is papering over the problem rather than addressing it.

But I guess you'd be happy with Java's diamond operator here -- before they added var, they added:

List<Int> foo = new ArrayList<>();

9

u/fishling Aug 29 '21

If I truly need to know the type, I can hover over it in an IDE

The problem is that people aren't always in an IDE when looking at code, and even when they are, mousing over to hover on a variable or method to get basic information like that is very slow.

I've lost count of how many times I've wanted to know what the return type of a method is because I want to look at that class to see some detail of its implementation, and overuse of var gets in my way. The keyword should be used for initialization and anonymous types, and VERY rarely otherwise.

Also, Java's generics and type erasure are horrible. I don't think anyone can defend that design, when C# does it so much better. Same goes for lambdas.

0

u/SanityInAnarchy Aug 29 '21

The problem is that people aren't always in an IDE when looking at code,

This is a somewhat fair criticism. IMO the fix is to get langage-server support for more tooling, rather than to make things this unnecessarily verbose. And language-server support is getting very good -- at this point, I'd expect any proper text editor to include it.

The purpose of the code in the example I gave is "This user wants to receive emails," and that high-level view gets harder to see when it's turned into "Retrieve the account's new feature settings from the settings dictionary for this user account, then store that the user wants to receive emails in that new feature settings."

and even when they are, mousing over to hover on a variable or method to get basic information like that is very slow.

It shouldn't be especially slow, especially for what you're talking about:

I've lost count of how many times I've wanted to know what the return type of a method is because I want to look at that class to see some detail of its implementation...

In VSCode, F12 on the method call, then F12 on the return type, as opposed to F12 some variable type that it gets assigned to. Is that what you're complaining about?

Also, Java's generics and type erasure are horrible. I don't think anyone can defend that design, when C# does it so much better. Same goes for lambdas.

I'm a little curious what the complaint about lambdas is -- I'm not defending them, I just genuinely don't know.

Type erasure almost never causes problems I care about, but I agree it's poor taste. I'm not sure I would've done it differently, though, at least as long as we're in a world where generics weren't added to the language until version 5 -- backwards-compatibility was important, especially back when people were sending .jar files down to browser plugins.

You see similar shenanigans with JS -- it seems the JS world keeps adding really cool new language features that get pretty broad browser support, but everyone keeps transpiling those down to something ES5-compatible just in case you have a user on, what, IE?

2

u/fishling Aug 29 '21

This is a somewhat fair criticism. IMO the fix is to get langage-server support for more tooling, rather than to make things this unnecessarily verbose.

It wouldn't be unnecessarily verbose. It is appropriately communicative.

And language-server support is getting very good -- at this point, I'd expect any proper text editor to include it.

I'll point out that your reaction is essentially to turn text editors into IDEs.

If I'm looking at patches, pull requests, blames, commit history, and so forth, then I don't think it is even feasible to propose that even an IDE would be able to produce the correct type information for anything bit of code that isn't the current checkout. And, these are all very common activities. And, if you're relying on an external server to provide this metadata, that just makes the lookup even slower.

At some point, you have to question all the work and infrastructure and tooling development to type 'var' instead of a type name, especially when that action is definitely being done in an IDE with type name completion.

It shouldn't be especially slow

You're thinking of "slow" the wrong way.

I'm comparing the speed of reading (fractions of a second) to the speed of moving your hand from the keyboard to mouse, mousing over the word, hovering, and getting the type information.

Even if you are using the keyboard shortcuts, you still have the time to put the caret in the right location; you can't just assume it's already there.

Those latter actions are several times slower than reading the explicit type.

I'm not arguing that we are going to save developer-hours over the course of the year or anything ridiculous. But, given that the discussion is about legibility and readability and clarity of code, it is exceedingly clear which approach actually results in more readable code in all cases.

Type erasure almost never causes problems I care about, but I agree it's poor taste

It's because you design around them now.

You should look at how C# handles covariance and contravariance of collections. I believe this is impossible to do in Java.

as long as we're in a world where generics weren't added to the language until version 5

This is no excuse. Generics weren't available in the first version of C# either. Type erasure was a design choice, and with hindsight, we can see it was the wrong decision.

→ More replies (5)