Techopedia defines abstraction in programming as
“The act of representing essential features without including the background details or explanations.”
I like this definition because it’s simple and gets the point across. When our goal is to write code that’s abstract, we focus on separating the meat and potatoes functionality (the code that gets used a lot), and let the details (the situation-specific code) fall in place after that. An example that I ran into while developing LunchBunch is the coding of API calls within the app. I coded all the calls into the UI classes, so each UI class had its own implementation of the API calls it used (with lots of repeat code). This means the class would build the call, receive it, parse it, and act on it. The building and receiving of the API calls was the same for every call, but was being done in every class that called an API. I needed something better. I needed abstraction. The value of abstraction is just that, reducing repeat code and keeping the meat and potatoes in one place, while the details (the veggies, if you will) are elsewhere, where they should be.
One issue I ran into while trying to keep my code abstract was deciding on when to stop. By adding some extra arguments and doing some fancy thingy-majigs, I could take almost all the code out of my UI class that didn’t directly affect the UI. I didn’t do this, though. Why? I think almost half of coding is creating friendly, readable code. Adding arguments that make little sense and doing fancy thingy-majigs isn’t always the answer. The final solution was to have special classes dedicated to building the API calls and receiving them. Each calling UI class implements an interface associated with that call which forces the class to define two methods, one to handle a successful return, and one to handle an error return. I found this to be the perfect amount of abstraction, and it greatly reduced the amount of repeat code, while keeping code readable and maintainable.
So abstraction is definitely something to keep in mind as you code. Think to yourself, am I writing the same code a lot? Can I make my code cleaner, simpler, and more fun to maintain? Make use of abstract classes and interfaces!
Brandon from The Bunch