Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Not specific code writing practices. Please also include reasoning.

My start:

  • use GCC or Clang
    • gcc because it is unchallenged in the amount of static checking it can do (both against standards and general errors)
    • clang cause it has such pretty and meaningful error messages
  • when compiling C code using GCC use -Wall -Wextra -Wwrite-strings -Werror
    • in 99,99% the warning is a valid error
  • when compiling C++ code using GCC use -Wall -Wextra -Weffc++ -Werror
    • you could skip -Weffc++ (cause it can be confusing)
  • always code against a standard C (C89, C99), C++ (C++98, C++0x)
    • while compilers change, standards don't, coding against a standard gives at least some level of assurance that the code will also compile in the next version of the compiler or even a different compiler/platform
  • make sure that the compiler checks your code against standard (-std=c99 -pedantic for C99, -std=ansi -pedantic for C++98 in GCC)
    • cause automatic checking always good
  • use valgrind or a similar tool to check for runtime errors (memory, threads, ...)
    • free bug catching
  • never duplicate functionality of the standard libraries (if there is a bug in your compiler, make a temporary patch, wrapper, ...)
    • there is no chance that your code will be better then the code maintained by hundreds of people and tested by tenths of thousands
  • make sure that you actually fix all bugs that are reported by automatic tools (GCC, valgrind)
    • the errors might not cause your program to crash now, but they will
  • never follow recommendations that include "never use feature X"
    • such recommendations are usually outdated, exaggerated or oversimplified
share|improve this question

5 Answers

Learn C++ from a book. Unfortunately, most freely available C++ resources are complete garbage.

share|improve this answer
2  
There is such a thing as a bad C++ book (e.g. anything by Herbert Schildt), and there are C++ online resources that are quite good (stuff by people who actually know what they're talking about, like GotW, C++ FAQ Lite, and of course Stack Overflow). But generally, you're right. – In silico Nov 24 '10 at 0:47

C++ specific - Use the "Resource Acquisition Is Initialization" idiom (RAII)

This takes care of 90% of your memory management problems. The other 10% can be taken care of with smart pointers (which themselves depend on RAII). Even though the language is not garbage-collected, I've never had to use a delete statement or some kind of DestroyXXX() or ReleaseXXX() or CloseXXX() function in application code - they're always somewhere deep in library/wrapper code.

It's the reason why std::vector allows for dynamic arrays without new or delete and fstream allows for manipulation of files without needing fopen() or fclose() in application code - it's all been taken care of.

share|improve this answer

Edit: I just noticed that the title said "new C and C++ developers". The suggestions below are probably better-suited to intermediate- and expert-level C/C++ programmers.

A couple of things to help get in the habit of writing portable code:

One, compile with aggressive optimization when you test (e.g. GCC's -O3 switch). This will often uncover bugs arising from subtle things like violation of strict aliasing rules. By doing so, you become aware of such issues, and your program will work properly in the presence of such optimizations.

Two, test on a PowerPC (or other big-endian machine) from time to time. Better yet, test on a 64-bit PowerPC if you can get your hands on one. Things you can learn by doing so:

  • When reading a binary file, you have to pack/unpack 16-bit, 32-bit, 64-bit, etc. words a byte at a time, or use some sort of endian-aware byte-swapping mechanism.
  • char is not always signed. On PowerPC Linux, GCC defaults to unsigned char. This isn't an endianness issue, but it's a subtlety I picked up on while testing on both x86 and PowerPC.
  • Big endian won't let you get away with long n = ...; printf("%d", n);
  • 64-bit big endian won't let you get away with:

    curl_easy_setopt(handle, CURLOPT_TIMEOUT, 1);
    

    See if you can spot the bug.

share|improve this answer

C: A Reference Manual

This booked cleared things up that I was foggy about for years. I have found that C is not like python where its going to be obvious that your doing something wrong. It takes away the It might work for now, but .... feeling.

share|improve this answer

Always pay attention to recommendations that say "never use feature X".

  • Such recommendations are typically based on the experience of lots of skilled people over a significant period of time.
  • If you choose to disregard such recommendations, make sure that you really understand them and the rationale behind them, before you disregard them.
  • If you choose to ignore them out of hand, don't be surprised if people criticize your code.
share|improve this answer
3  
Like, "Never use exceptions or templates?" – Crazy Eddie Dec 7 '10 at 19:15
Obviously, you also need to filter recommendations based on the credibility of the person making them. But even if the recommendations are bad, there may be a lesson in what the person is saying. (For instance, C++ exceptions and templates can be tricky to use correctly ... or so I've heard.) – Stephen C Dec 8 '10 at 11:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.