3 ms·
So... the author would rather have // Old school String string = new String(); over // C++ style auto string = new String(); // Rust sty
by timw4mail 7y ago
So... the author would rather have
// Old school
String string = new String();
over
// C++ style
auto string = new String();
// Rust style
let string = String::new();
I don't see a single advantage to repeating the type, especially in the case where it's already apparent what the type will be.
- rwj 7y agoI agree with your examples, but that code is pretty trivial. There is a point where inference can be over used, and that can cause readability issues. The OP ended with a call for empirical research.
- JulianMorrison 7y agoThe advantage is mostly when it's String s = doSomethingOpaque();
- ijpoijpoihpiuoh 7y agoThe supposed advantage. Many perfectly productive languages don't even offer the option to specify the type at all, and not just in the inference case. As far as I'm aware, nobody has ever convincingly demonstrated (with empirical data) that this is worse along any metric that people care about (development speed, bugs, cost, etc.). A lot of people have preferences on this front. Somehow, these preferences get promoted to best practices if not moral license. Just, which approach is claimed to be better really depends on who you ask, and then mainly on whichever language that person first cut their teeth on. And nobody's going to be able to cite any well-designed study that demonstrates their preference is actually superior, because none exist.
- jcelerier 7y agonitpick: the C++ style is in most cases auto string = std::string{}; (old school being just std::string string; )
- timw4mail 7y agoTrue, but I was using String as an arbitrary example.
- b_t_s 7y agoYou can get more strings in that line with obj-c ;P NSString * string = [[NSString alloc] initWithString:@"myString"];
- UIZealot 7y agoThere's no need for type inference, nor repeating the type, especially if you're designing a new language anyway. Why not: String string = new ();