r/cprogramming • • Dec 30 '25
Principle Of Least Concern
Hello there. I love the language and am by no means a newbie, having done many sorts of programs with it, been a few years.
For me, the language is almost perfect. Although, there are some things which bothers me by a lot, and I deny using something else such as C++. I like having only what is necessary, nothing more, so C with assembly is my way to go. I could not find resources online to solve my issue, so I need to resort to someone with more experience. Neither the llms are able to solve it.
The issue is the inability of one to use a principle, the principle of the least concern/visibility. The solution to this problem seems double: make more files or make do. And this makes me very much depressed.
Python, Java, C++ for example, all have features that enables the user to organize code within a single source file. They mainly solve the issue by proposing access modifiers. Please, know that I am not talking about OOP, this has nothing to do with OOP. Please, also know that one adheres to the gcc compiler and all it's features.
I already know how the language works, the only this I haven't used much in those years is the _Generic along with other more obscure features. But only having the ability to static global variables so they be localized in the object file, seems to not be enough.
One may wish to have a source file be made of various parts, and that each part have only what is needed to be visible. I talk like this because I assume this problem is well known and that you guys already know where I am going with this. But I argue that this makes prototypes, for instance, completely useless. Since I assume they are not useless, then there sure must be a way for one to apply the principle.
I will suppose that some of you may also contest my above affirmation. No damm shall be given about the traditional way of separating code into two files, put some prototype in one, definitions in the other, call one header, the other source, and call it a day. No. That's needless, unclean in my opinion and even senseless unless one may really find benefit at having an interface file for multiple source, implementation files. Since I don't mind using my compiler's features, there is no need to be orthodox.
I simply cannot fathom that one of the most efficient languages to cover assembly have been this way for so long and that no one bothered to patch it up. I have created parsers before, hence other languages. I state that the solution to this issue consumes 0 runtime. Not only that, the grammar will not be changed, but added upon, so the solution would be backward compatible with any other code written in the past. I guess as many have said it, it is like this due to historical reasons :/ and worse, I am incapable of changing the gcc source or even making a good front end with those features for the llvm. I can't compete with the historical optimizations.
To be more clear about the principle in the language, suppose a single source file with three functions for example, A, B and C. It is impossible to define them all in the same file such that A can call B, B can call C but A cannot call C. Sure you may with prototypes, but you cannot follow the pattern if I add more functions. One may do such a thing in C++ for example, using protected modifiers and having other structures inherit it, enabling one to divide well enough code without needing to create more files. One may extern the variable in C, which for the usage of the principle, should have other means to encapsulate the variables. Was it clear or would I need to further formalize the problem?
I assume you guys already know about this. According to me, this is the only issue that doesn't make C a scalable language :( Help
14
u/JaguarMammoth6231 Dec 30 '25
Many people have "patched it up," but then it's not C anymore, it's a different language with C-like syntax. It's happened many times.
Also, having lots of files in a project is not really a bad thing. Having a single large file is usually much harder to deal with.
2
u/gdvs Dec 30 '25 edited Dec 30 '25
"Not having more than 1 file" is not a concern people typically have. C may not have the keywords and support for encapsulation other modern languages have, you can still program with those principles in mind. When you end up in situation where you have a circular dependency, you may want to evaluate your program's structure.
The principle of least concern (knowledge?) pleads to have implementation split from the interface to hide complexity and achieve encapsulation. You can't have this, and at the same time have the guarantee there's only 1 possible implementation. That's the exact opposite.
You can always create your own extension to the language. You wouldn't be the first. Having said that, not having more than 1 file is not a very typical concern in programming language paradigms. I suppose it's easy in tiny programs, but it becomes impossible in real applications.
2
u/dcpugalaxy Dec 30 '25
C supports modularity through separate compilation translation units. That's the unit of modularity. If you don't want to use them, that's fine. I basically don't anymore either. Unity build or all in one file. But that's the trade off.
Why do you need compiler-enforced encapsulation!
You can write a preprocessor for C that compiles to C then gets compiled further using GCC.
2
u/Typical_Ad_2831 Dec 31 '25
This seems very interesting! I wonder if you could do something in a
makefilewithgrep -n,tail,head, andgcc -x. You could also make an alias/function to combine these in the requisite way. I might be totally misunderstanding what you're saying, though.2
u/WittyStick Dec 31 '25 edited Dec 31 '25
While I largely disagree with your take, I'll admit there are use-cases for encapsulation within a translation unit - but such thing is not going to replace C's separate translation unit and linking process.
I occasionally abuse GCC's
poisonpragma to achieve some encapsulation in header-only libraries. For example, If I want to define a struct whose fields should be encapsulated:See in Godbolt
1
u/Numerous_Economy_482 Dec 30 '25
I’m a newbie, can someone explain me what is the problem? Why would I be bothered to have A calling C, or a real example please? 🙏
1
u/ebmarhar Dec 30 '25
What are you doing where you need assembly? And what hardware platform?