8 ms·
The C Programming Language: Myths and Reality
- lelanthran 3y agoThe first in a series of blogs posts I am putting together around the C programming language (Not the book!)
- sfpotter 3y agoOne of the advantages C has over C++ here is that the addition of templates in C++ makes this kind of encapsulation more difficult. See the proliferation of header-only libraries where nearly everything is templatized, as well as the pimpl idiom. You still get some encapsulation in the form of private, but with "C-style" encapsulation, if the header file doesn't change and the source files implementing what's declared in that header don't change, then none of the files need to be recompiled when rebuilding. This makes recompiling much faster. Of course, you could do much the same thing if you didn't use templates in C++, or if you were very disciplined and limited with your use of templates, but this seems to go against the grain of how I've seen C++ used.
- cozzyd 3y agoIt is different, but provides much better encapsulation. Keeping ABI compatibility is much easier this way.
- pjmlp 3y agoNo longer the case since C++20 modules.
- sfpotter 3y agoThanks for the downvote...? Not sure what the point of that was. I haven't used C++ much since modules were introduced. From doing some quick reading, it seems unclear whether they significantly improve compilations times in practice. I wasn't able to find anything which addressed how they relate to the ABI... I suspect this is a pretty complex topic. If you have any good references related to either of these points, I'd be very eager to read them. Either way, "C++ with modules" appears to me to be unlikely to clear the bar set by "C with opaque types" (which, for all intents and purposes, can be done in C++) in terms of 1) ABI stability and 2) encapsulation. I consider point (1) to be related to point (2), since details which are leaked into the ABI are not encapsulated.
- pjmlp 3y agoFirst of all, who told you I downvoted you?!? Importing the whole standard library as defined by C++23 import std; is quicker than doing a plain #include <iostream>, as shared per Microsoft employees in some talk they have done regarding upcoming C++23 support. Unfortunely I can't remeber which one it was, but someone else can glady share the link. Second by having template details marked as private on the module metadata, that isn't directly exposed to consumers. As for the ABI specifically, that is compiler dependent anyway.
- commandlinefan 3y agoWhen I first encountered Object Orient Programming around '97 or so, all of the literature focused on the three pillars of OO: encapsulation, inheritance and polymorphism. I was struck at the time at how OO mostly just formalized and gave specific names to what good programmers were already doing informally.
- rightbyte 3y agoThe words are also confusing and too much latin. Encapsulation - Hidden Inheritance - Same fields in beginning Polymorphism - Tagged struct
- slavapestov 3y ago“Hidden” from whom and how? “Same fields in beginning” doesn’t characterize multiple inheritance. “Tagged structs” are just one implementation strategy for one kind of polymorphism, and “tagged” and “struct” are themselves jargon. Using and knowing the precise terminology is important when communicating technical concepts.
- jcelerier 3y ago> I was struck at the time at how OO mostly just formalized and gave specific names to what good programmers were already doing informally. I mean, that's the point of everything, isn't it? giving names and defining good practices so that everyone can benefit from them? because even today you can find a LOT of codebases which are an imperative mess
- wvenable 3y agoA lot of comments/articles complaining about Object Oriented Programming -- especially the style implemented by C++ and similar languages -- start with premise that OOP is some academic prescription declared from up high like Moses and 10 Commandments. But the reality is that the best practices of imperative programming where already much like object oriented programming and that OOP is a formalization of those practices.
- 3y ago
- gjulianm 3y agoI wouldn't call this equivalent to the private keyword: * It does not work on a field, but on the whole struct. Either all fields are public or all are private. The latter case forces you to write getters/setters for properties you want users to access, and that in C can be even more cumbersome as you need to write the definition in the .h and the implementation in the .c. * It breaks, without an explicit and specific and error message, several actions. As it's mentioned in the article, malloc isn't possible, but neither are copies by value, sizeof() breaks... And those will break with an "incomplete type" error message, not a "this type is intentionally made private", which can add confusion in some situations. * Completely incompatible with inlining code. Considering a lot of people still use C precisely for its performance, I think this can be a drawback in a lot of usecases. I honestly think that hiding struct declarations should be done sparingly, and preferably limiting it to cases where it's actually necessary (for example, a library that doesn't expose internal struct fields so the same executable works with different versions of the dynamic library; or proprietary libraries that want to expose as little as possible). In the end it's still easy to bypass, and the distinction between header and code files already provide an indication of which functions you should use and which ones you shouldn't.
- Joker_vD 3y ago> It does not work on a field, but on the whole struct. struct PrivateFieldsOfMyShinyClass; struct MyShinyClass { int somePublicData; double morePublicData; struct PrivateFieldsOfMyShinyClass *p; }; > malloc isn't possible, but neither are copies by value, sizeof() breaks Those are implementation details which are deliberately being hidden. > Completely incompatible with inlining code. They are as incompatible with code inlining as public/private modifiers in C++ are. That is, LTO is your best friend here. Also, have you ever tried to maintain binary compatibility with several versions of a third-party C++ library that keeps adding/removing private fields to/from its classes?
- jstimpfle 3y agoHiding members in this way is only possible with pointer indirection, which isn't satisfying. However, having only a boolean private/public access state isn't generally satisfying either. It often leads to violation of the principle of separation of concerns when all the functions (methods) acting on certain "private" fields need to live in the same class. In simple classes, like std::vector, it's possible to get away with private. But in many cases that are more complex than that, it seems to me that the best approach is still to expose the data and to be just very clear about the exact purpose of each member.
- simias 3y agoI see where you're coming from but IMO this "Cheshire cat" idiom to hide the implementation details is not exactly like private, it fact it can do things that private can't do, and doesn't do things private does. The advantage of hiding your state behind an opaque struct with builders and accessors is that you can change the size and layout of said struct without it being a breaking API change. The code remains binary compatible even, no need for a recompile if you're shipping a shared lib. This is something just using private members doesn't achieve since with private members the compiler still knows and uses the layout of the struct, it just forbids access to it. That's why you can even find C++ libraries use this idiom even though C++ obviously has `private`. It's about having a stable, opaque API. On the other hand because of this added indirection, there's usually a greater performance hit to accessing these opaque structs since code can't be inlined. With private since the compiler can still see inside the struct, it's able to more aggressively optimize the code. You can also store the objects directly on the stack without requiring malloc. IMO the right way to have private members in C structs is... to document that members shouldn't be touched directly, perhaps using a special naming convention or embedding the publicly-accessible members in a dedicated sub-struct to prevent confusion.
- zokier 3y ago> The code remains binary compatible even, no need for a recompile if you're shipping a shared lib. This is something just using private members doesn't achieve since with private members the compiler still knows and uses the layout of the struct, it just forbids access to it. There is somewhat common PIMPL idiom to work around the binary compat issue. Iirc there were some macros floating around to make it easier to manage.
- maleldil 3y agoWhat do the macros do? Isn't this as easy as forward declaring the impl class, adding std::unique_ptr<Impl> as a private field and have public methods refer to the field? I'm struggling to understand why macros would help here.
- 3y ago
- jasode 3y agoTo the author : your explanation can be interpreted as "correct" but also be aware that -- for some readers -- your argument is a variation of the Turing Tarpit: https://en.wikipedia.org/wiki/Turing_tarpit https://en.wikipedia.org/wiki/Turing_tarpit In other words, the 2 different possible receptions to your post: - YES, file-level modularity with opaque structs is _equivalent_ to class private members --> for those mindsets already sympathetic to C Language - NO, using file-scoping rules and structs is not equivalent to class private members because it's a bunch of extra ceremonial syntax to implement a workaround. (The "Turing Tarpit"). It's using the opaque struct as a "design pattern" and as Peter Norvig famously said, "Design patterns are bug reports against your programming language."
- alpaca128 3y agoI don't think boilerplate code has much to do with Turing Tarpits. It might be annoying but not Brainfuck-grade insanity.
- colonwqbang 3y agoI would argue that C++ "pimpl" design pattern brings more "ceremonial syntax" than the C equivalent. C++ style (without "pimpl") requires recompilation of the whole dependent tree when adding a new private member function. It's encapsulation only in a formal sense
- adwn 3y ago> Hell, they cannot even malloc() their own StringBuilder instance, because even the size of the StringBuilder is hidden. They have to use creation and deletion functions provided in the implementation as specified in the interface. And you've just made it impossible for the users of your StringBuilder to pass it around by-value. Every instance has to be malloc'ed by your library, even though it's just a tiny, word-sized struct. Awesome! And each access needs to go through an additional pointer indirection. All this just to pretend that C supports proper encapsulation. Hooray! I'm sorry that I'm targeting your blog post specifically, but it's just so stereotypical of C proponents, that can't (or won't?) realize that their favorite programming language is inherently limiting and limited along several very important dimensions. It makes me think that although some of them might be excellent programmers, they make for terrible software engineers.
- xbar 3y agoThe best software engineers I ever worked with were all C programmers. Further, and quite separate, the term "software engineer" was coined on behalf of assembly programmers, whose language lacks even further features. Finally, I am not sure what you mean when you say they make bad software engineers. Perhaps we have different definitions of software engineer.
- adwn 3y agoIf someone doesn't recognize when and how their tools limit the quality of their work, they can't be good craftsmen. There might still be good reasons to use those tools (e.g., no better alternatives, or an existing ecosystem), but if you don't realize that your programming language is fundamentally limiting in ways that other languages are not, then you'll never know how to build better software.
- pjmlp 3y agoThe term Software Engineer is something that in plenty of countries is validated by the engineering organization and is a legal title, not something one feels like calling themselves. Which also validates that any university teaching software engineering has a certain quality level, and portofolio of lectures, to create a general background across all subjects of engineering practices besides writing code.
- ryu2k2 3y ago>You don’t have inheritance [..] Modern PL features are more or less wrappers around comparatively complex C code. Object inheritance is actually one that isn't too difficult or complex to implement: https://www.youtube.com/watch?v=443UNeGrFoM&t=4275s https://www.youtube.com/watch?v=443UNeGrFoM&t=4275s
- jll29 3y ago> Modern PL features are more or less wrappers around comparatively complex C code. Object inheritance is actually one that isn't too difficult or complex to implement: https://www.youtube.com/watch?v=443UNeGrFoM&t=4275s https://www.youtube.com/watch?v=443UNeGrFoM&t=4275s True, and if you implement it as a preprocessor, that's exactly how C++ started in 1979 (Stroustrup's "C with classes" Cfront pre-processor: https://en.cppreference.com/w/cpp/language/history https://en.cppreference.com/w/cpp/language/history).
- oneeyedpigeon 3y agoGreat article, OP, but you forgot to populate the href on your "swig" link.
- lelanthran 3y agoThanks, fixed.
- mojosam 3y agoI think there’s a better example, but whether it applies it depends on one of two major divisions of C code: that designed to run on systems with a MMU (as typically used for Linux and other large OSes) — where virtual memory makes dynamic momory allocation practical — and those without — which today is primarily the very large world of embedded devices. For the latter, the industry best practice is to avoid malloc(), except maybe at init time, and instead allocate memory statically. And in that use case, you break your code into modules, which can contain private data, public data, private functions, and public functions. In other words, building an app out of C modules is a lot like building an app in a more modern language just using static classes, with no instantiation. And that design pattern — which is extremely common in the embedded world — we have a direct equivalent to the “private” qualifier, which is “static”, which restricts the rest of the app from accessing so-marked file-scope variables and functions. Where this breaks down — as always with C — is when you need multiple instantiations of a module, which modern programming languages refer to as an object. The closest we can get in C is to pass the module’s public functions a struct with some sort of data structure containing the object’s n9n-static data. And the author explains, there are standard ways make that data structure opaque to calling code, but those are definitely workarounds to language shortcomings. But the bottom line is that those language shortcomings — the lack of objects and a private qualifier for its members — are only shortcomings if you need those features, and in the embedded world, most applications don’t, they only require all the advantages offered by C. So as always, this is about picking the right language for the project, there’s no one size fits all.
- nvy 3y agoJumping through all these hoops just to, what? Avoid the garbage collector? It's okay to admit C has shortcomings.
- lelanthran 3y ago> It's okay to admit C has shortcomings. I did that, didn't I? >> Just to be clear, C is an old language lacking many, many, many modern features. One of the features it does not lack is encapsulation and isolation.
- zer8k 3y agoThe OP article seems to be someone trying to cram modern concepts into a language that explicitly rejects them. A symptom that the OP should've probably just used another language.
- ape4 3y agoI've seen code that prefixes private members with an underscore and adds a comment saying that its private. Not saying that's great but it does send a message.
- 3cats-in-a-coat 3y agoSo let's do polymorphic virtual methods now.
- 0xfedbee 3y agoGreat article lelanthran. It’s always refreshing to see someone going against the popular opinion here and piss everyone off. Keep it up!
- ReflectedImage 3y agoActually, you can just have a public version of a struct and a private version of a struct with more fields. You can give callers the public version and then cast it to the private version for internal usage.
- bluejekyll 3y agoThese will have different sizes though, so it’s only safe to cast when used off the heap and where it’s memory is allocated with the larger variant, right? I guess my point is that there’s a whole bunch of caveats in regards to safety that need to be considered in your solution.
- ReflectedImage 3y agoIt's not my solution, it's just something I've seen done in commercial C software.
- zh3 3y agoThat seemed a complicated way of reminding us that C++ (at least originally) is/was compiled down to pure C and thus can't do anything that C can't do.
- mcnichol 3y agoI'm appreciative for the person who wrote the article. These are my favorite types of debates and while I disagree with it for much better articulated reasons already mentioned, primarily the exploitative nature of header files used as security through obscurity, I think it stirs up a lot of debate and keeps the spaghetti meatball of knowledge around patterns/anti-patterns, "best-practices", etc. moving forward in a much more passionate way. I think this is the truest sense of the term "hacker" which lives up to the title of the site pushing us into these debates. Putting stuff together that doesn't always work as intended or expected but arguing for and against it. A long winded thank you to everyone, OP and all the threads responding.
- bjourne 3y agoSure, but it still goes against the grain of the language. You could write a similar article explaining why everyone who thinks C doesn't have automatic memory management are wrong. Opaque structs are relatively annoying to work with especially if sibling modules in the same package have good reasons to manipulate the private parts directly. It is not nearly as convenient as it is in a language with better support for encapsulation (e.g Java). Most of the C code I write do not encapsulate anything. It's not worth the bother. Especially not when unit-testing for which encapsulation would force you to write lots of redundant getters and setters just for the unit tests themselves. My view is that you simply shouldn't use C if you need encapsulation.
- throaway23423 3y agoIn practice, please don't do this... it breaks inlining and adds unneeded allocations.
- up2isomorphism 3y agoThe funny thing is each time when you find C has some “hacky” way of doing things other programming languages can officially support, then you find that hacky is probably the only thing you actually need.
- bryancoxwell 3y ago> My Makefiles already have rules to automatically generate the interface so that the C code I write in this manner is callable from within Android applications. I don’t know a lot about C but this interests me. Can anyone point me to where I might learn more about how this works?
- variadix 3y agoThis is a much stronger form of encapsulation than public/private. You basically have to do this if you have a generic API that wraps system specific APIs to avoid polluting your code with system specific includes/types/functions/etc.