Design principles, guidelines and design patterns for C (or procedural programming in general)?

Are there any well-known design principles, best practices, and design patterns that can be used in developing Project C? Or useful design principles for procedural (imperative) programming in general?

(I am a child of the "object-oriented generation" and must design a large C project for the first time)

+89
c design design-patterns principles
Mar 22 '10 at 13:16
source share
5 answers

Information Hiding - Like Parnas ( Software Basics ).

Careful management of headers and visibility:

  • Everything in the source file, which can be hidden from the outside world, should be; Only a documented external interface should be open.
  • Everything that is exposed is declared in the title.
  • This header is used where functionality is needed (and where it is defined).
  • The header is self-sufficient - when you need it, you use it and you don’t have to worry about β€œwhat other headers I should include as well,” because the title ensures that it works, including everything you need to create it. Work.
  • The header is self-protecting, so it doesn’t matter if it is turned on several times.

    #ifndef HEADER_H_INCLUDED #define HEADER_H_INCLUDED ...rest of header contents, including other #include lines if necessary #endif /* HEADER_H_INCLUDED */ 
  • Design sets of functions to work with "objects" (usually structures) - and use these functions instead of understanding the structure internals in the code that uses it. Think of it as voluntary encapsulation.

+62
Mar 22 '10 at 13:34
source share

There is a free, free online book called Object Oriented Programming with ANSI-C that covers the topic of writing object-oriented code in C. A google search for β€œobject-oriented C” also provides some other good examples and resources.

If your project is critical, MISRA-C is a good set of rules. It is intended primarily for embedded c, but may be useful in other areas.

I consider myself an OO encoder, and I work a lot with the built-in C. The best advice I can give, especially for large projects, is not to overdo it. Creating a complete OO structure on top of ANSI C can be very tempting, but it takes a lot of time and effort to fix it. The more you have an interlocutor, the more time you spend on debugging your infrastructure instead of working on a real project. Approach the task with a clear head and a good, reliable understanding of YAGNI . Good luck

+21
Mar 22 '10 at 13:36
source share

My three tips:

  • Write unit tests. They help you focus on the design that suits your problem as you move forward. Much better than relying (solely) on meditation before meditation.
  • Have a memory leak detector (there are all kinds of libraries out there) installed and working from day one. Let this library print all leaks as soon as the program / tests are complete. This will allow you to detect a leak as soon as you inject it, which will make it much less painful to fix it.
  • Writing OOP code in C. Not so difficult. Although you can emulate method overrides, I suggest starting with emulating simple objects. Even this simple mechanism can give you great mileage.

Here is an example:

 typedef struct Vector { int size; int limit; int* ints; } Vector; Vector* Vector_new() { Vector* res = (Vector*) malloc(sizeof(Vector)); res->limit = 10; res->size = 0; res->ints = (int*) malloc(sizeof(int) * res.limit); return res; } void Vector_destroy(Vector* v) { free(v->ints); free(v); } void Vector_add(Vector* v, int n) { if(v->size == v->limit) { v->limit = v->limit * 2 + 10; v->ints = realloc(v->ints, v->limit); } v->ints[v->size] = n; ++v->size; } int Vector_get(Vector* v, int index) { if(index >= 0 && index < v->size) return v->ints[index]; assert false; } 
+20
Mar 23 '10 at 5:22
source share

OOP is a methodology, not a technology. Therefore, my first advice is to stop thinking of it as procedural programming.

At e.James, you don’t want to try to recreate an object-oriented language or pretend that you have the opportunity. You can still do everything right by clinging to a few simple principles:

  • Check everything out.
  • Find what is changing and encapsulate it.
  • Design for interfaces.
+6
Mar 23 '10 at 5:08
source share

The SEI CERT C encoding standard provides a good set of rules and common practices , as well as what you should try to avoid using.

+4
Mar 02 '17 at 14:23
source share



All Articles