Thursday, October 26, 2017

Defining Objects

Earlier in my career, I loved browsing through the Linux kernel's source code. Mostly to satisfy my curiosity about how certain things are internally implemented. As I read through more and more files of code, I started noticing one common pattern. For the most part, the code was organised around  abstract concepts (spin-lock, work-queue, kthreads etc). And every abstract concept had

  • An object representation, a C structure that holds details about that structure
  • An init/alloc call, that initialises the object
  • The init/alloc call also returns a handle to this object
  • A number of operations that accept this handle as a parameter and then operate on that object
  • And finally a deinit/free call that destroys that object and also makes the handle invalid for any further use

Oh yes, you say, we had learnt it in our classes, no big deal! No, no, no. I mean, I had also learnt about it in my classes, but I had never ever used it practically in my code, apart from the stack example in my class assignment.

This was a good way to see it in practice in the Linux kernel, and that too used at multiple places. We even use it in our everyday programming lives. For example,

  •  File IO:
    • fd = open(...);
    • read(fd, ...);
    • write(fd, ...);
    • close(fd);
  • Socket IO:
    • socket_fd = socket(...);
    • bind(socket_fd, ...);
    • connect(socket_fd, ...);
    • send(socket_fd, ...);
    • close(socket_fd);
  • Pthreads: (pthread_create)
  • OpenSSL: (SSL_new)
  • Semaphores: (sem_init)
  • and numerous others

So what is really my point here? Thinking in this way, helps you focus on defining these objects. And thinking about these objects helps you have a more modular and cleaner approach to software development. Design your data structures first, and the rest automatically follows. 

Next time you add a global int/char variable, think if there is a relationship among those data structures. Do they belong into a structure together? Can you make things easier by defining an abstract concept/object and operations around that object?

Here is what Linus Torvalds (the founder of the Linux kernel and git) has to say about this:

I'm a huge proponent of designing your code around the data,
rather than the other way around, and I think it's one of the
reasons git has been fairly successful (*).
...
(*) I will, in fact, claim that the difference between a bad
programmer and a good one is whether he considers his code or
his data structures more important. Bad programmers worry
about the code. Good programmers worry about data structures
and their relationships.


No comments:

Post a Comment