R
R
Rudtoha2018-11-13 10:41:04
C++ / C#
Rudtoha, 2018-11-13 10:41:04

How to properly split a C project into files?

Hello! The question is extremely amateurish: are there any recommendations / requirements / rules for creating a project architecture? Do you need to include .h files in .c or .h? How many extern variables are already considered "bad"? etc.
If more locally: I have two .c and two .h files. Each of them uses cross variables. I want to arrange everything so that it is readable and logical.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Melnikov, 2018-11-13
@Rudtoha

Well, at least the use of cross variables is already illogical. It's better to wrap it with functions. How to encapsulate in a module.
The correct way is to draw the modules / objects of the system on paper and indicate how they interact with each other and the outside world. And already proceeding from this to break. Ideally, the more a module looks like a black box, the better in terms of consistency.
As a litmus test, it can be used that from one module you suddenly need to dig into the guts of another, or know how something is implemented there.
As an example for C - the standard library. You open a file, you get a pointer to the FILE structure, but it doesn't matter to you what fields there are, whether it is created by malloc or a pre-created object from some array of such objects. This is already hidden by the implementation. There are functions that work with this pointer and that's it.
As a bad example of a "not-so-black box", consider the strtok string that is passed as input will be modified and corrupted, which may not be obvious from the semantics. But done for the sake of speed and memory usage.

F
FD4A, 2018-11-21
@FD4A

Good architecture is only indirectly dependent on the programming language. I would recommend reading Robert Martin's Clean Architecture, easy to read and to the point.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question