Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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 better code." He also always uses some sort of story involving string manipulation and how knowing C would allow you to write more efficient string routines in a different language.

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.

Edit: Reading some of the answers you guys have submitted have been great, but still doesn't make any sense to me in this regard:

Take the strcat example. There's a right way and a wrong way to combine strings in C. But why should I (as a high-level developer) think that I am smarter than Larry Wall? Why wouldn't the language designers write the string manipulation code the right way?

like image 624
Eric Ryan Harrison Avatar asked Apr 06 '09 01:04

Eric Ryan Harrison


People also ask

Why is it important for me as an individual to learn C programming?

It helps you understand how a computer works This can include aspects like allocation and memory management along with their architecture and the overall concepts that drive programming. As a programming language, C also allows you to write more complex and comprehensive programs.


1 Answers

The classic example that Joel Spolsky uses is on misuse of strcat and strlen, and spotting "Shlemiel the painter" algorithms in general.

It's not that you need C to solve problems that higher-level languages can't solve, it's that knowing C well gives you a perspective on what's going on underneath all those levels of languages that allows you to write better software. Because just such a perspective helps you avoid writing code which is, unknown to you, actually O(n^2), for example.

Edit: Some clarification based on comments.

Knowing C is not a prerequisite for such knowledge, there are many ways to acquire the same knowledge.

Knowing C is also not a guarantee of these skills. You may be proficient in C and yet still write horrible, grotty, kludgy code in every other language you touch.

C is a low-level language, yet it still has modern control structures and functions so you aren't always getting caught up in the fiddly details. It's very difficult to become proficient at C without gaining a mastery of certain fundamentals (such as the details of memory management and pointers), mastery of which often pays rich dividends when working in any language.

It's always about the fundamentals.

This is true in many pursuits as well as software engineering. It is not secret incantations that make the best programmers the best, rather it is a greater mastery of the fundamentals. Experience has shown that knowledge of C tends to have a higher correlation to mastery of certain of those fundamentals, and that learning C tends to be one of the easier and more common routes to acquiring such knowledge.

like image 104
Wedge Avatar answered Oct 20 '22 00:10

Wedge