Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why use raft_server_t* in all functions? instead just use raft_server_t #5

Open
dimitar9 opened this issue Nov 30, 2014 · 1 comment
Labels

Comments

@dimitar9
Copy link

Hi Willemt,
I like your work so much. I have a question here:

void raft_set_current_term(raft_server_t* me_, int term)
{
raft_server_private_t* me = (void*)me_;
me->current_term = term;
}

Like this,
why not just use raft_server_t, since it is already void*.
I don't quite understand. Could you please tell me?

Thank you

@willemt
Copy link
Owner

willemt commented Dec 1, 2014

Hey that's a good question

The one and only reason why raft_server_t is void* is because of encapsulation.

A void* typedef means I can do whatever with my implementation and keep the details private.

There's no other reason for me using the void* typedef

Why do I use raft_server_t* instead of raft_server_t, which is a void* pointer already?
I don't like creating types that are pointers by default. It makes the code harder to understand. If you have pointer-less types then the person reading the code never needs to ask "is this type a pointer?". Seeing the explicit * makes the source more readable.

Why don't I use just void* in every function prototype?
The compiler does some handy type checking for you even if the typedef is a void_. For example, if someone passes a raft_peer_t_ into a function that expects a raft_server_t* it will complain. This is a win.

So if I'm effectively using void* * for all my raft_server_t* references why does it work?
This works because the void** is the same as void*. It's just a pointer. They're equivalent in this case.

Hope this makes sense.

In the last month I've found a better technique for encapsulation. If you're interested you might want to look at the heap_t typedef in https://github.com/willemt/heap/blob/master/heap.h

@willemt willemt added the FAQ label Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants