Jump to content

How to abstract


Recommended Posts

  • Member

I have seen lots of code that is tightly tied to specific hardware or to specific frameworks.  This code is OK because it generally satisfies rule #1 (it must work) but as soon as the HW or framework changes this code becomes very difficult to adapt to the new system.  Developers often state that they are abstracted from the hardware by the framework but this is generally never the case because the framework was provided by the hardware vendor.

So what is a developer to do?

Step #1 Ask the right question.

Instead of asking HOW do I do a thing (how do I send bytes over the UART).  The developer should ask WHAT do I need to do.  Ideally the developer should answer this WHAT question at a pretty high level.  WHAT do I need to do? I need to send a packet over RS485.

Step #2 Define an API that satisfies the answers to the WHAT questions.  If I must send a packet over RS485, then perhaps I need a SendPacket(myPacket) function.  In the context of my application this function will be 100% clear to my partner developers.

Step #3 Implement a trial of my new API that runs on my PC.  This is sufficiently abstract that running my application on my development PC should be trivial.  I can access a file, or the network, or the COM ports, or the STDIO and still satisfy the API.  Get my partners to kick it around a bit.  Repeat #1,#2 & #3 until the API is as clear as possible for THIS application.

Step #4 Implement the new API on my HW or framework.

This may seem like contributing to Lasagna code.... i.e. just another layer.  But in fact this is the true definition of the hardware abstraction layer.  ALL details of the HW (or framework) that are not required for THIS application are hidden away and no longer contribute to developer confusion.  100% of what is left is EXACTLY what your application needs.  Now you have a chance at producing that mythical self documenting code.  You will also find that unit testing the business logic can be more easily accomplished because you will MOCK all functions at this new API layer.  Hardware NEVER has to be involved.

Good Luck.

  • Helpful 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Create New...