Skip to main content

Object oriented SDK development

Object oriented programming
If you are someone who is into object oriented SDK development and looking for some best practices on API design, I have got you covered here. I have used C++ as the base to explain the core. OpenCV is one of the frameworks I have come across that does an exceptional job in providing classic and productive APIs in C++. Let's not waste time an quickly jump into the best practices of object oriented API design.


Design easy-to-read API names

First and foremost thing to consider in an object oriented SDK is the API name itself. Your API names should reveal everything. Keeping APIs as small as possible can be a difficult task but that is what you should deliver. The API should do what the name says. Nothing more. Nothing less. 

Write your headers and review it

Before you jump into development, it is important to code what the user wants. To have a crosscheck on that, write a header file(.h or .hpp) containing the public/private functions and data members. In this process, you can eliminate methods and data members that is not needed for the SDK user.

Hide your implementation details

In a closed source SDK where only headers will be given, every single word in your header matters. You would not want to expose any of your implementation details. So hide data members that are not 100% required to be exposed.

In that case where you cant have a data member in private or public, but still you want to access it from another class, you have many options like


  • Friend class
  • Inheritance
  • Pimpl idioms, etc.

A Constructor's workload

Mostly, constructors would be used to initialize the variables. But it is also used to allocate memories and initialize using other functions. The problem with the latter is sometimes when memory allocation fails, constructors cannot return errors. Keeping all these in mind, it is always advised to have a separate method for initialization to allocate memories.

Maintain a standard

When it comes to maintaining your source, there are many places where you should follow standards throughout the code. Listed below are some of them: 
  • If you use pointers, use only pointers throughout. If you use references use only references throughout. Bringing both might lead to some confusion.
  • Use namespaces along with the function names in all the usages.
  • Have a template for prolonging/recursive namespace usages.

Error handling

There are many ways to handle errors if you are a C++ developer. But I like enums. Enums allow you to maintain error messages really well. Have a common numbering pattern to maintain your error messages. 

Getters and setters

Have getters and setters for the private members. In some cases where private variables can only be given read access and not write access, can opt for read-only objects. This is however, a standard practice in object oriented programming.


Object oriented programming
Meme section has its say on OOPS B-)


I have listed only some of the best practices while developing an object oriented SDK."API designs for C++" by Martin Reddy is a book that explains many more practices in brief with realtime examples is a very useful one to consider. If you feel you have some more best practices, you are free to mention them in the comments section.

Comments

Popular posts from this blog

Sachin Tendulkar's legendary journey

"I see myself when I see Sachin play"  -Cricketing legend Sir Don Bradman Sachin Ramesh Tendulkar, had represented India at the age of 16. Rather than asking 'How old are you?', people who were longing to query his age would have asked him 'How young are you?'. Such a prodigy he was and still is one. He was given his first international cap on 15 November 1989 and exactly 23 years and 364 days later, he played his last international cricket match for his nation and on a combo, it was his 200th test match.              Sachin has scored over a 34,000 international runs, and there are no exclamations for that huge milestone after describing him as a prodigy. But still seeing another cricketer of his quality and matching his frequency, is very far away from my eyes. But who knows he might be somewhere playing cricket in schools, or schooling in kinder gardens, or building blocks in play schools, or might even be an unhatched embryo.  ...

The art of sleeping

     They say that living is an art but I am starting to think that sleeping is an art, too! It’s just not as easy as it was when I was a kid. Being an adult, sleep is not just a sleep. It encompasses of a mixture of various feelings that we undergo in the day life. A happy sleep requires 3 things. i.e., satisfaction at heart, peace in mind and the harmony in soul. But I say, the foremost requirement for a good sleep is a good day at the work. When you gave your all, at the work and go to the bed, I bet you it is the most amazing feel in the world.     Now, why is sleeping an art? I had a research on controlling the sleep. And to my fortune, it paid off. The research was about the concept of self alarm. Yes, an automated alarm within us which would wake us up from the sleep. The idea was very simple. I had an inner consiousness while sleeping which kept an indegenous alarm within me which would wake me up around the intended time.(No more ringing bells or 6am ...