I agree with the advice to read your standard library, because I think there's huge value to your general understanding of lower-level concepts and to knowing the available functionality of your language, but I disagree that it is the best example of idiomatic code, necessarily. For example, C++'s STL was written to be portable, efficient, and general beyond all other goals. Modern C++ style and abstractions are nowhere to be found, and if you follow the STL style, you may over-abstract (templatize), use manual memory management, and architect for non-relevant circumstances.
Certainly depends on the language and where in the standard library you are, though you're completely right to always be cognizant that it's all been taken through battles in production at scale and has been optimized accordingly. One could also argue that that kind of code might be idiomatic for that kind of problem (an example of general, portable, and efficient), and a good guideline on how that might be done, though it might not be a super satisfying argument depending on how conservative your definition of "idiomatic" is.
BTW, if you're going to read a C standard library, skip glibc and go for musl: https://git.musl-libc.org/cgit/musl/tree/src
There was an article last week-ish about reading OpenBSD source daily.
Re:Learn to Read Code
by woodrowbarlow on Thursday June 29, 2017 @11:32AM
(#14664091)
why do you say that?
Because libcs other than glibc are less bloated and probably easier to read.
~ $ cat empty.c
int main(void) {return 42;}
~ $ gcc -static empty.c -O3
~ $ strip a.out
~ $ size
text data bss dec hex filename
677598 5792 10464 693854 a965e a.out
~ $ diet gcc -static empty.c -O3
~ $ strip a.out
~ $ size
text data bss dec hex filename
901 32 76 1009 3f1 a.outIt's just not very readable.
I would modify this slightly, and suggest finding out who some of the most highly regarded programmers are for your language, and reading their libraries.
Standard libraries are often a very mixed bag of stuff because things are added but rarely taken away. Some it may be very new, and some of it may have been written before the idioms of a language were even finalized, and then frozen by compatibility needs. If you don't already have a strong sense of good style (or what is currently considered good practice for a language) you may find it difficult to identify what is good and what is a relic of history.
In Java, this might be java.util.Collections package, or java.util.Concurrent. or in C++ that might be Boost. In Elixir, I start with Kernel.ex[0]
https://github.com/elixir-lang/elixir/blob/master/lib/elixir...