Live data from Hacker News

To learn a new language, read its standard library

patshaughnessy.net

181–190 of 243 posts

Re: To learn a new language, read its standard library

#181

The advice really only applies for very specific definitions of "learn". Let me cite a single line from the MSVC implementation of the C++ standard library ( https://github.com/microsoft/STL/blob/main/stl/inc/xtree ): using _Scary_val = _Tree_val , _Tree_simple_types , _Tree_iter_types >>; Also, good luck understanding this class member definition. Hope you figured out _Scary_val! _Compressed_pair > _Mypair; (Hint: E…

I don't think the advice applies for C++ either. The last time I worked with a C++ code base I tried to understand some std:: API semantics by reading the code but I failed every single time. I'm not an expert C++ programmer by any means but I think I've passed the novice stage where you try to learn the language. Maybe for C++ constant learning of the base language is needed :-)

For Go and C on the other hand I think it works very well since the core languages are so simple it's viable to read others code successfully without being a language expert. I read the Go stdlib code all the time if something is unclear in the documentation (the docs are good but sometimes there are edge cases that are not clearly documented).

Re: To learn a new language, read its standard library

#182
post #116

I find languages are pretty easy to pick up after the first few. What seems to be the barrier nowadays is frameworks. How do people go about learning a new framework? Obviously most of them have an intro project to follow on with, but they tend to be pretty simplistic, and once that's done, getting to the complicated bits that you actually want to implement seem to be the barrier.

/me trying to learn OCaml after learning a bunch of other languages. Disagrees.

Same experience, I thought I had things figured out but after a few weeks struggling with OCaml I felt like a complete retard :-) But it was an eye opener in some way on how to write programs in a different way. I think OCaml also suffers a bit the same problem as C++ does. You need to learn quite a lot to be able to read others code. There are many advanced concepts with cryptic syntax that is hard to search for on the net.

Re: To learn a new language, read its standard library

#183
This doesn’t seem like good advice to me, as others have already said.

C++ standard library is written in a heavily templates style most user code isn’t written in. That would be a terrible way to learn.

Some of Python’s standard library delegates to C code.

Much of Clojures standard library builds the language up from a small set of primitive, sometimes out of macros, and isn’ta good example of how to write good code yourself.

The standard libraries tend to be written in a different mindset than idiomatic user code is, as the requirements differ. In my opinion, the best way to learn a language is to implement a non trivial but small project in it.

Re: To learn a new language, read its standard library

#185

> 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 l…

Actually, 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?"

Re: To learn a new language, read its standard library

#186
This depends on the language.

In C#, standard library is awesome, and indeed a good starting point for people learning the language.

On the other end of the spectrum there’re languages like C++ with these horrible templates, or Rust with tons of unsafe code. Adopting patterns or coding style from these standard libraries is not the best idea.

Re: To learn a new language, read its standard library

#187
post #161
post #139

Earlier quoted context omitted.

Or it even uses patterns which would be considered anti patterns outside of it for the sake of optimizations.

Optimized 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.

Though this argument also supports the parent comment actually: notably, standard library has much higher than usual chance and assumption of being possibly used in tight loops - with no way to relax that assumption based on actual (often closed-source) usage. So the same optimizations done in stdlib could perfectly well be considered premature if done in non-stdlib code (even if we imagined some func was not in stdlib and one needed to write exactly same func in one's code).

Re: To learn a new language, read its standard library

#188
post #172
post #161

Earlier quoted context omitted.

Optimized 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.

A 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.

You seem to be implying that it is desirable to employ programmers who would get confused over bitshifting and lack the ability to figure it out.

Re: To learn a new language, read its standard library

#189
post #161

Earlier quoted context omitted.

Optimized 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.

I 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.

Very well put.

(And if the standard library doesn't represent idiomatic use of the language, that's a red flag).

Post reply on HN