开发者

How to organize my c code

开发者 https://www.devze.com 2023-01-14 06:28 出处:网络
I have large main file contains about 7,000 lines of C code. If I want to make this code modular and separate the code from this file. what is the main criteria fo开发者_StackOverflow中文版r separatin

I have large main file contains about 7,000 lines of C code. If I want to make this code modular and separate the code from this file. what is the main criteria fo开发者_StackOverflow中文版r separating the functions out of the file and functions that should stay in the file.


  • Follow earlier suggestions.
  • Eliminate any duplicate or almost duplicate code by creating functions.
  • Organize by functionality and dependency. Modules should have as little inter-dependency as possible.
  • Follow SOLID Principles and other design patterns and practices (all of which can be implemented in some degree in C).

I like to use a top-down decomposition of the code. For example:

main()
{
    Initialize();
    Introduce();
    while (some_condition)
    {
        DoSomething();
        DoSomethingElse();
    }
    SayGoodbye();
    Shutdown();
}

The main() should be short and to the point and give you a quick overview of what the program does from a high-level. Each of these functions can be broken down in a similar way. This should continue until the lowest level functions have a single, focused purpose (logical modularity). These functions can be put into additional .c/.h files to have physical modularity.

Good luck!


Break up by functionality/responsibility.

For example, place all string handling in one module/file, place file handling in another.


It is a simple measure: the fewer declarations you have in the .h file, the more modular it is. Grouping by functionality is important. Having extern declarations is very bad, give those an extra 'penalty'.

0

精彩评论

暂无评论...
验证码 换一张
取 消