Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I learn to write efficient and maintainable C code? [closed]

Tags:

c

My current experience with programming is limited to hacking together some shell scripts and some assembly in the past. However, I learned the basic syntax of C code in college.

I want to learn how to write efficient C code, I an confused whether to start with K&R or C Programming: A Modern Approach. Also should I study some algorithm books alongside so that I don't end up writing inefficient code right from the beginning?

like image 266
jck Avatar asked Jan 30 '11 12:01

jck


4 Answers

Don't worry too much about efficient code at this stage. Concern yourself more with clear and readable code.

As a rule, keep functions small, performing one task. If it takes too many sentences to describe what one function does, it probably needs breaking down into smaller functions.

Use descriptive variable names, rather than just x and n etc.

Start by writing programs you will enjoy developing, rather than "boring exercises that the book tells you to do." However, do follow the book's advice and guidelines.

Everybody has their own style, don't worry if your style doesn't quite match the next person's.

Above all, enjoy it! If you find learning one thing a bit boring, move on to something else, there's always loads to learn.

EDIT: Additionally, don't try to run before you can walk - I'm specifically thinking a) pointers and b) dynamic memory allocation. There's no need to use either of them at this early stage until you're comfortable.

like image 199
Dave Avatar answered Oct 16 '22 09:10

Dave


First get wet with the basics. Learn the gotchas, learn a good C style. You can't learn how to write efficient C code in one run, so it's okay to make mistakes in the beginning.

I found the best way to learn how to write efficient code is to learn how to avoid memory leaks. Maintainable C code requires good documentation and comments in the source. Also, it requires writing code that resists change.

Examples:

bad example:

int* ptr = malloc(5 * 4); //4 here being size of int.
... do something with ptr here... //<-- this is wrong!

Why bad? int may not always be 4. Also, in the second line you're doing something with ptr (probably assignment) without checking if it was NULL.

better example:

int* ptr = malloc(5*sizeof(int)); // better, always allocate with respect to int size
if (ptr) ..do something..

Why better? First you allocated with respect to the size of int, so even if in another architecture int's size differed, you are good. You also check if ptr is NULL before using

Best example:

int* ptr = malloc(5* sizeof(*ptr));
if (ptr) .. do something.
free(ptr); // done with ptr

Why is this the best way? First you linked the size of allocation not with int's size, but directly with ptr's type. Now if someone for any reason changed int to long in ptr's declaration (especially if it was declared somewhere else) without changing to long inside malloc's argument; your allocation will still be correct because it directly allocates according to whatever type ptr is of.

We also free'd ptr after being done with it, to prevent a memory leak.

like image 41
Orca Avatar answered Oct 16 '22 10:10

Orca


You should try reading "Writing solid code" from Steve Maguire, it's an old book, but it can teach you what you need.

like image 40
manueru Avatar answered Oct 16 '22 09:10

manueru


First and foremost, care for portable programming. There is a C Standard (ISO 9899:1999 aka C99 and :2011 aka C11--you need to invest ~20 bucks) and a "Unix" Standard, called POSIX (freely accessible). Read them, live them. Stay away from any and all Windows and graphics programming until you're no longer a grasshopper.

As the wise man says: The novice programs his machine. The expert programs a set of machines. The guru programs for no particular machine.

That's the essence of portability and it will save you from a lot of questions of the form It compiled/ran under FOO-OS with the Frobozz magic compiler, but doesn't under BAR-OS, WTF?

like image 28
Jens Avatar answered Oct 16 '22 08:10

Jens