December 9, 2025

Hide and C: Encapsulation Brawl

Brent's Encapsulated C Programming Rules (2020)

C fans clash over “hidden” code: genius or headache

TLDR: Brent champions hiding code details in C to make libraries safer, with a performance-friendly alternative using const. Commenters split: some cheer, others call it burdensome, and warnings about “casting away const” and debates over void pointers highlight the core fight—safety and clarity versus speed and control.

Brent dropped his 2020 “Encapsulated C” rules and the crowd promptly split into two camps: the “hide everything” believers and the “just show me the code” pragmatists. One fan, jjgreen, cheered, “Outstanding,” while others rolled their eyes and grabbed popcorn.

The fireworks started when Brent praised “pure encapsulation” — hiding inner details so developers use only friendly functions. Jean-Papoulos slammed that as busywork, saying if the tools don’t help, they’ll open the hood anyway: “This just adds burden.” Cue the vibe: header purists vs code-peekers.

Then came the const wars. Unwind suggested a safer pattern and admitted, “The const is perhaps over-doing it,” lighting debates over clarity versus performance. Meanwhile, writebetterc defended the mysterious void* (a “anything goes” pointer) as essential, turning the thread into Void-Star Wars. Brent’s performance mode — exposing struct members but locking them with const — sounded slick, until fpotier yelled “casting away const is UB” (undefined behavior), and linked docs like a referee’s whistle.

Memes flew: “intelli-sense vs common sense,” “peek-a-header,” and “const-cabulary.” Under the jokes, it’s a serious split: do you protect users with strict interfaces, or trust them and chase speed? The community answered: why not argue about both — loudly.

Key Points

  • The article outlines two approaches to encapsulation in C: pure encapsulation and a performance-oriented variant.
  • Pure encapsulation uses opaque structs with forward declarations in headers to hide implementation details.
  • Exposing struct members can improve performance but risks misuse; const qualifiers are used to prevent accidental reassignment.
  • Rule of thumb: place const after the type for pointers and before the type for non-pointers when exposing fields.
  • C17 _Generic macros or inline functions are recommended to safely modify const-exposed fields within implementation files.

Hottest takes

"Outstanding, why hadn't I come across this before?" — jjgreen
"I hate this. If my intellisense isn't providing sufficient info (generated from doc comments), then I need to go look at the implementation. This just adds burden." — Jean-Papoulos
"I believe that 'Casting away the const' is UB" — fpotier
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.