What is an example in which knowing C will make me write better code in any other language?

前端 未结 20 1155
独厮守ぢ
独厮守ぢ 2021-02-05 12:40

In the Stack Overflow podcasts, Joel Spolsky constantly harps on Jeff Atwood about Jeff not knowing how to write code in C. His statement is that \"knowing C helps you write bet

20条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 13:19

    I think it is worth knowing some low-level language, and there are pragmatic reasons to choose C:

    • It's low-level, close to assembler
    • It's widespread

    Understanding the whole stack is valuable. Sometimes you need to debug something's guts. Sometimes you cannot fix a performance problem without low-level knowledge (this is often not the case, e.g., when the performance problem is purely algorithmic, but sometimes it is).

    Why is C widely considered the quintessential "bottom of the stack", and not some other language(s)? I think this because C is a low-level programming language, and C won. It has been a while now, but C was not always as dominant. To take just one famous example, the proponents of Common Lisp (which had its own ways of writing low-level code) were hoping their language would be popular, too, and eventually lost.

    The following are usually implemented in C:

    • operating systems (Unix variants, Windows, many embedded operating systems)
    • higher-level programming languages (many popular implementations of Java, Python, etc)
    • (obviously) reams of popular open source projects

    I'm not a hardware person, but I gather that C has influenced CPU design heavily, too.

    So if you believe in understanding the whole stack, learning C is, from a pragmatic perspective, the best choice.

    As a caveat, I think it's worth learning assembler, as well. Although C is close to the metal, I didn't fully understand C until I had to do some assembler. It is occasionally helpful to understand how functions calls are actually performed, how for loops are implemented, etc. Less important, but also useful, is having to (at least once) deal with a system without virtual memory. When using C on Windows, Unix, and certain other operating systems, even humble malloc does a lot of work under the covers that is easier to appreciate, debug and/or tune if you've ever had to deal with manually locking and unlocking memory regions (not that I would recommend doing so on a regular basis!)

提交回复
热议问题