Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Why is private encapsulation "good" in (C) programming?

I have read several times that it is implied that it is "good" programming style to have private encapsulation. But I'm curious to know what exactly makes it good. For example, the code below does...

1 answer  ·  posted 1d ago by Carl‭  ·  last activity 19h ago by Olin Lathrop‭

Question c private-encapsulation
#2: Post edited by user avatar Carl‭ · 2026-09-17T08:55:08Z (1 day ago)
  • I have read several times that it is implied that it is "good" programming style to have private encapsulation. But I'm curious to know what exactly makes it good.
  • For example, the code below does not use private encapsulation because the user of the ring-buffer (main.c) has access to all the ring-buffer's members. When we think about it, the user should really only be able to put and get items.
  • ```C
  • /* ring_buffer.h */
  • #define RINGBUFFER_SZ 32
  • typedef struct
  • {
  • uint8_t buf[RING_BUFFER_SZ];
  • volatile size_t head;
  • volatile size_t tail;
  • } ringbuffer_t;
  • void rb_init(ringbuffer_t* rb);
  • void rb_put(ringbuffer_t* rb, uint8_t item);
  • uint8_t rb_get(ringbuffer_t* rb);
  • ```
  • ```C
  • /* main.c */
  • #include "ring_buffer.h"
  • static ringbuffer_t rb;
  • int main(void)
  • {
  • rb_init(&rb);
  • rb.head = 0u; //User can do this if he wanted to - for some reason
  • }
  • ```
  • If the user of the ring-buffer wanted to, he could set all the members to arbitrary values. I see why this is bad but why would we ever expect the user to want to do that? Private encapsulation via forward declaring the ring-buffer struct and defining the struct in `ring_buffer.c`, or by simply defining the ring-buffer struct statically in `ring_buffer.c` as a variable at file scope would prevent this problem.
  • I'm seeking a clear explanation of why private encapsulation is good programming style in general.
  • I have read several times that it is implied that it is "good" programming style to have private encapsulation. But I'm curious to know what exactly makes it good.
  • For example, the code below does not use private encapsulation because the user of the ring-buffer (main.c) has access to all the ring-buffer's members. When we think about it, the user should really only be able to put and get items.
  • ```C
  • /* ring_buffer.h */
  • #define RINGBUFFER_SZ 32
  • typedef struct
  • {
  • uint8_t buf[RINGBUFFER_SZ];
  • volatile size_t head;
  • volatile size_t tail;
  • } ringbuffer_t;
  • void rb_init(ringbuffer_t* rb);
  • void rb_put(ringbuffer_t* rb, uint8_t item);
  • uint8_t rb_get(ringbuffer_t* rb);
  • ```
  • ```C
  • /* main.c */
  • #include "ring_buffer.h"
  • static ringbuffer_t rb;
  • int main(void)
  • {
  • rb_init(&rb);
  • rb.head = 0u; //User can do this if he wanted to - for some reason
  • }
  • ```
  • If the user of the ring-buffer wanted to, he could set all the members to arbitrary values. I see why this is bad but why would we ever expect the user to want to do that? Private encapsulation via forward declaring the ring-buffer struct and defining the struct in `ring_buffer.c`, or by simply defining the ring-buffer struct statically in `ring_buffer.c` as a variable at file scope would prevent this problem.
  • I'm seeking a clear explanation of why private encapsulation is good programming style in general.
#1: Initial revision by user avatar Carl‭ · 2026-09-17T08:54:18Z (1 day ago)
Why is private encapsulation "good" in (C) programming?
I have read several times that it is implied that it is "good" programming style to have private encapsulation. But I'm curious to know what exactly makes it good.

For example, the code below does not use private encapsulation because the user of the ring-buffer (main.c) has access to all the ring-buffer's members. When we think about it, the user should really only be able to put and get items.

```C
/* ring_buffer.h */
#define RINGBUFFER_SZ 32
typedef struct 
{
    uint8_t buf[RING_BUFFER_SZ];
    volatile size_t head;
    volatile size_t tail;
} ringbuffer_t;

void rb_init(ringbuffer_t* rb);
void rb_put(ringbuffer_t* rb, uint8_t item);
uint8_t rb_get(ringbuffer_t* rb);
```

```C
/* main.c */
#include "ring_buffer.h"
static ringbuffer_t rb;

int main(void)
{
    rb_init(&rb);
    rb.head = 0u; //User can do this if he wanted to - for some reason 
}
```

If the user of the ring-buffer wanted to, he could set all the members to arbitrary values. I see why this is bad but why would we ever expect the user to want to do that? Private encapsulation via forward declaring the ring-buffer struct and defining the struct in `ring_buffer.c`, or by simply defining the ring-buffer struct statically in `ring_buffer.c` as a variable at file scope would prevent this problem.

I'm seeking a clear explanation of why private encapsulation is good programming style in general.