4 ms·
Related: Dig into your language's source code and code of major libraries! Your standard lib is probably the best examples of idiomatic code and efficient code
by _asummers 9y ago
Related: Dig into your language's source code and code of major libraries! Your standard lib is probably the best examples of idiomatic code and efficient code that you're going to have access to, written by the authors of the language itself. You'll notice bugs, inconsistencies and poor docs which gives you opportunities to contribute upstream, at the same time making you read more and learn how particular features work under the hood, which might give you new perspectives in your own code.
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/lib/kernel.ex https://github.com/elixir-lang/elixir/blob/master/lib/elixir...
- quantdev 9y agoI 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.
- _asummers 9y agoCertainly 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.
- panic 9y agoBTW, 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 https://git.musl-libc.org/cgit/musl/tree/src
- woodrowbarlow 9y agowhy do you say that?
- sigjuice 9y agoBecause 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.out
- posterboy 9y agoIt's just not very readable.
- _asummers 9y agoThere was an article last week-ish about reading OpenBSD source daily. https://news.ycombinator.com/item?id=14521386 https://news.ycombinator.com/item?id=14521386
- sjellis 9y agoI 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.