6 ms·
Forcing usage of constructors is hardly more readable IMO. Which bug is easier to spot? cat = new Cat(1, 2, 4); cat = Cat.builder().eyes(1).legs(2).he
by cle 7y ago
Forcing usage of constructors is hardly more readable IMO.
Which bug is easier to spot?
cat = new Cat(1, 2, 4);
cat = Cat.builder().eyes(1).legs(2).heads(4).build();
Additionally, from the examples in that post, records aren't valid JavaBeans either, so they can't be used with the myriad of existing tools/libraries that expect beans.
From my first impression, it looks like records don't really address many of the reasons we use Lombok and its ilk, so many of us will just ignore this feature. Or do like I've done in the past couple of years, and just ignore Java altogether.
- skocznymroczny 7y agoJava could really used keyword arguments as a replacement of the Builder pattern: cat = new Cat(eyes=1, heads=2, legs=4);
- rooam-dev 7y agoToo much for small classes/records imho e.g. `new Name("First", "Last")` vs. `new Name(first="First", last="Last")` Also, IDEs can help with parameter hints when is needed.
- deleted 7y ago[deleted]
- deepsun 7y agoThey can be optional, like in Kotlin -- you can give name to only some (after unnamed ones).
- rooam-dev 7y agoI think it's perfect for parameter objects
- kelnos 7y agoI expect the reason that they didn't want them to be valid beans is because then they'd lose immutability.
- Rebelgecko 7y agoIf you're using a decent IDE, that bug is just as easy to spot when using a constructor. My IDE adds the var names in front of the values, so when I create a new Cat(1,2,4) it actually looks like this: new Cat(eyes:1, heads:2, legs:4);
- electrum 7y agoThis is how they look in IntelliJ IDEA: https://www.jetbrains.com/help/idea/viewing-reference-information.html#inlay-hints https://www.jetbrains.com/help/idea/viewing-reference-inform...
- cle 7y agoIf using an IDE is necessary to make the code readable, then Java should ship with an IDE. Or just add it to the language. I find myself often reading code outside of an IDE, such as in GitHub or other hosted repos. Why should it be less readable in those cases? Because it's marginally easier to write? I also write a lot of Go code, and appreciate how "go vet" disallows unkeyed struct literals, forcing you to write the field names when creating structs. It is not an IDE feature, and I can freely browse GitHub or grep around in the command line and the code is just as readable as in an IDE. It also means Go works in a lot of editors and IDEs more easily, because those editors/IDEs don't need to implement a bunch of extra features to make the code actually readable.
- seanmcdirmid 7y agoVerbosity is a huge problem in a language. Verbosity that makes unfamiliar code easier for you to read can actually hurt the readability to a more familiar reader trying to figure out where the bug is. I don’t need that thisIsTheBloodyFooArgument: all over the place if I already know it is the bloody foo argument. Eventually, the code becomes so repetitively noisy that I have to rewrite it stripped down into a text file just to get a gist of the real things I need to understand (like when comments get in the way and don’t help. But really, this is a problem we don’t need to have...tooling could make it so we have the verbose repetitive version that make unfamiliar readers happy and the more concise to the point version that makes familiar readers happy. But for some reason, our profession is infatuated with text files that make this best of both worlds impossible.
- mister_hn 7y agoAssuming you are using an IDE, the new Cat(1,2,4) is far better and concise, plus the IDE tells you already what they are.