5 ms·
> To learn a new language, read its standard libraryTo learn a new language, read its standard library Yes, but also no. The standard library is often more co
by dathinab 5y ago
> To learn a new language, read its standard libraryTo learn a new language, read its standard library
Yes, but also no.
The standard library is often more complex and uses more advanced features then you often need for most projects. I.e. collections in std are supper general purpose, but if you need to write a collection it's normally specific to the purpose you need it for.
I.e. std is the "most" general purpose library you normally find so even if it's written with "KISS" in mind it's still often not so simple.
Then it sometimes uses unstable language features you normally can't use (as it's often made "in sync" with the language) and/or optimizations which in most other cases would count as "pre-mature" optimizations.
Through without a question you can learn a lot there, you just should be aware of the points above.
- vletal 5y agoOr it even uses patterns which would be considered anti patterns outside of it for the sake of optimizations.
- masklinn 5y agoFor older languages, it’s also pretty common for some of the stdlib’s corners to be very dusty with API design, naming conventions, and coding patterns considered at best way outdated.
- pharmakom 5y agoSystem.Collections.IEnumerable says hello
- GuB-42 5y agoOptimized code is not an anti pattern! What is an anti pattern is premature optimization, code you write a certain way because you think it is optimized but because you don't understand the compiler and hardware, it is not. Optimizations in standard libraries are generally good ones, and reading it can give a good idea of what is or isn't worth it.
- hvidgaard 5y agoI think the comment simply stated that when learning a language you should focus on idiomatic code and not optimizations. The latter which is probably present and justified in a standard library.
- bborud 5y agoVery well put. (And if the standard library doesn't represent idiomatic use of the language, that's a red flag).
- vletal 5y agoTake Scala collections. These are written using builders and if-else checks of emptiness which are definitely not idiomatic. Authors of the std-lib did them for you so you do not have to. That is not a red flag.
- bborud 5y agoThe other way around: non-idiomatic use of a language in its standard library points to weaknesses in the language. If emptiness is something that is so important you add extra branchpoints inside collections to check for it, there ought to be some way of disallowing emptiness or {nil,null}ness in the language itself. Sure, it is nice for people who use the standard library, but it points to things in the language that perhaps should have been given some more thought.
- hvidgaard 5y agoI'm not sure I would call it a weakness. Performance vs expressiveness and maintainability will always be a trade off. It could be a weakness, but it's just as likely that it was a deliberate trade off.
- colund 5y agoA library like Java Collections API sometimes uses optimizations involving bitshift etc which is too confusing for most application level code. So your comment that it's not an anti-pattern to do such things in application code is something I have to disagree with.
- bborud 5y agoActually, this isn't so much an argument against reading the standard library as it is another reason to do so. If the standard library is ugly and complex, it tells you something important about its design, and possibly about the language itself and its maintainers. It is a good indication of where things might well end up for you. You should look at the ugly bits, and you should ask yourself "am I going to invest in this?"
- vbezhenar 5y agoThat's why I decided not to invest in Scala. Its standard collections library was so hard to grok that I just decided to stay on Java. I must understand everything in the entire stack I'm using. If I have blind spots, it makes me nervous. Java is pretty good in that regard, for example. I regularly browse its standard library and it's quite comprehensible. Concurrent stuff is not easy, but I guess that's the nature of underlying algorithms. Also that's the reason that I don't like Java streams. They're as hairy as Scala collections. I'm avoiding Java streams. IMO implementation details of any library matters as much as its public API. If library implementation is not nice to read, it's a very bad smell.
- Jeff_Brown 5y agoThe trick is to read unfaithfully. You don't need to understand everything in the standard library. You just need to swim around in good code for a while. If you're in over your head, chances are you can skip that part.