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

前端 未结 20 1098
独厮守ぢ
独厮守ぢ 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:26

    It's hard to quantify exactly, but having an understanding of C will give your more insight into how higher-level language constructs are implemented, and as a consequence you'll be better able to use the constructs in an intelligent manner.

    0 讨论(0)
  • 2021-02-05 13:26

    Classic examples are things involving lower level memory management, such as the implementation of a linked list class:

    struct Node
    {
        Data *data;
        Node *next;
    }
    

    Understanding how the pointers are used to iterate the list, and what they signify in terms of the machine architecture will allow you to better understand your high level code.

    Another example which Joel was referring to was the implementation of string concatenation, and the right way to create a string from a set of data.

    // this is efficient
    for (int i=0; i< n; i++)
    {
        strcat(str, data(i));
    }
    
    // this could be too, but you'd need to look at the implementation to be sure
    std::string str;
    for (int i=0; i<n; i++)
    {
      str+=data(i);
    }
    
    0 讨论(0)
  • 2021-02-05 13:26

    Knowing C is not a requirement to being able to effectively use higher-level languages, but it certainly can help ones general understanding of how computers and software work - I think it's similar to an assertion that knowing some assembly language or computer architecture/hardware logic (and/or/nand gates, etc) can help a C programmer be a better programmer.

    Sometimes in order to solve a problem it helps to know how things are working 'underneath' what you're doing.

    I don't think this means a programmer must know C in order to be a good programmer, but I think that knowing C can be helpful to almost any programmer.

    0 讨论(0)
  • 2021-02-05 13:27

    As someone who knows a little C, but loves to write code in perl and other high-level languages, I have never once come across a problem that I was able to solve by writing C.

    I am looking for examples of real-world situations where knowing C would be useful while writing a project in a high-level/dynamic language like perl or python.

    It's easy to start writing high level code and then wonder we it's running slow. The truth is there are many ways to write perl or python code, and some are better (as in more efficient) than the others. If you know the low level details of how your code is executed in perl or python (both of which are written in C) you can code around several inefficiencies --like knowing which looping construct is faster, how memory is retained/released, etc.

    Also, when writing a project in perl or python you sometimes hit a performance wall. The creators of the language (Guido, at least) advocate that you implement that part in C, as a language extension. To do that, well, you'll have to know C.

    So, there.

    0 讨论(0)
  • 2021-02-05 13:27

    I don't think there can be any specific example.

    What learning C does for you is give you an insight, a broadening of the mind, into how computers (and software) work. It's a very abstract thing ..

    It doesn't make you write better code in python, it just makes you more of a computer scientist.

    The reference that Wedge made to Joel's article mentioning Shlemiel the painter is an interesting one but has no relevance here. That algorithm is not tied to C in any particular way (although it manifests itself in null-terminated strings).

    Python's strings are immutable anyway, and completely different from C's model of strings, so I don't quite see the relationship.

    I suppose one concrete example is optimizing a parser or a lexer or a program that keeps writing to a string buffer all the time. If you use normal strings instead of a string buffer, you'll run across a problem when you build very large strings.

    Consider that:

    a = a + b 
    

    makes a copy of both a and b. It doesn't change the string that was referenced by a, it creates a new string, allocating more memory, etc.

    If a becomes considerably large, and you keep adding small things to it, then Shlemiel the painter will manifest himself.

    But then again, knowing this has nothing to do with knowing C, just knowing how your language implements things at the low level. (This is where having an experiece in C will help you).

    0 讨论(0)
  • 2021-02-05 13:30

    It's a mistake to assume that learning C will somehow automatically give you a better understanding of low-level programming concerns. In a lot of cases even C is too high level to give you a good understanding of efficiency concerns.

    A classic is i++ versus ++i. It's over-cited, so perhaps most people know the implications about performance between these two operations. But learning C wouldn't magically teach you this by itself.

    I guess I understand arguments about strings. When string operations are made deceptively simple, people often use them in inefficient ways. But again, knowing that strncat exists doesn't give you a full appreciation for the efficiency concerns. A lot of C programmers probably haven't even thought about the fact that strncat has to do a strlen operation internally.

    Even using C, it's important to understand what's going on behind the scenes if efficiency is a concern. People who know C tend to view things in a progression. Assembly and machine code are the building blocks of C, while C is a building block of higher level languages.

    This isn't specifically true, but it's obvious that C is "closer to the metal" than many higher level languages. This has at least two effects: efficiency concerns aren't as hidden behind implicit behavior, and it's easier to screw up.

    So you want a specific example of how knowing C gives you an advantage. I don't think there is one. I think what people mean when they say this is that knowing what's going on behind the scenes in whatever language you're happening to write for helps you make more intelligent decisions about how to write code. However, it's a mistake to assume that C is "what's going on behind the scenes" in Java, for instance.

    0 讨论(0)
提交回复
热议问题