I am willing to bet that at this point, you would be sick of hearing me say this but I always want to approach technical writing as a story-telling experience. I want this because I understand that story-telling taps into our innate love for narratives and the art of weaving tales. In this article, I will be looking at presenting one foundational programming concept that has revolutionized how software is built and maintained in a story-telling manner. I implore you to check out my previous piece on the inspiring story of OpenAI's journey from scratch - but promise me you would check it out only after reading this one. As you settle in, fasten your seatbelts not just for a ride, but for an immersive narrative.
Introduction
In the world of creating software, a strong foundation is key. Before even writing a single line of code, there is a vital step called object-oriented analysis and design (OOAD). The method this step offers helps software developers plan solutions smartly, thereby ensuring smooth development. It lets software creators build applications that aren't just functional, but also adaptable, easy to maintain, and ready for future growth. With object-oriented programming languages now being the standard for everything from web development to running desktop applications, mastering and understanding object-oriented analysis and design has become essential.
In this article, I will guide you through the foundational concepts and principles of the process that allows you to take your ideas for an application into the right pieces so you know directly what code to write (i.e. Object-Oriented Design) in the form of a story-telling experience.
Object-Oriented Design: A relatable analogy
Many of today's widely adopted programming languages follow the object-oriented paradigm. However, it is important to note that this isn't the only programming approach available. To comprehend the advantages of object-oriented languages, I would draw a comparison with the alternative method i.e. procedural programming languages, such as the plain C language. In procedural coding, a program is composed of a sequence of operations to be executed. While certain portions might be structured into named functions for modularity, the primary objective is to move from Point X to Point Y in a bid to accomplish a task.
Permit me to compare these approaches by drawing a parallel with a familiar analogy. Think of traditional storytelling versus interactive theater. In traditional storytelling, it is almost like a classic book where the author guides you through the plot, describing each event and character's actions in a linear sequence. The narrative flows step by step, following a predefined path. This linear approach can be likened to procedural programming in software development as a program is designed as a series of instructions, much like the chapters of a book. While some sections might be grouped for better organization, the overall structure is sequential similar to how the author crafts a coherent story by arranging events.
- A split-screen image showing a traditional storytelling scene on one side and actors performing in interactive theater on the other, representing the comparison between procedural programming and object-oriented programming.
Now, shift your mind to interactive theater. In this dynamic setting, instead of a single narrator guiding the story, different actors portray distinct characters, each with their motives and actions. This can be likened to object-oriented programming where a program is divided into objects. Much like the characters in the interactive theater, each object encapsulates its data and behavior, and these objects interact with each other, similar to actors on a stage. The characters' exchanges shape the unfolding story, just as the interactions between objects influence the software's behavior.
In both traditional storytelling and interactive theater, through the ages, we have heard some compelling and captivating narratives created. Similarly, in procedural and object-oriented programming, over the years, a couple of functional software or dare I say, life-changing software have been developed. However, the way these narratives or programs are structured and experienced differ. Just as interactive theater adds layers of complexity and engagement to the storytelling experience, object-oriented programming enhances software development by promoting reusability, adaptability, and modular design. Both paradigms have their merits, and their selection depends on the nature of the project. However, understanding the analogy between traditional storytelling and interactive theater would help you appreciate when and how to apply object-oriented programming, leading to software that's not only functional but also flexible, maintainable, and capable of adapting to changing requirements, much like an interactive theater experience that evolves based on audience interaction.
Reusability: Another working analogy
Now, let's relate this to code reusability. Imagine if a particular character or plot element in the story could only be used once and never appeared again. For sure, this would limit the possibilities for creating new stories with the same characters or elements. Similarly, in procedural programming, code segments are often tailored for a specific task and aren't easily adaptable for reuse in different contexts. Each piece of code is designed for a specific purpose, making it less flexible when applied to different scenarios.
On the other hand, in interactive theater where actors play distinct characters, each with their actions and roles, a character from one interactive theater production could seamlessly be integrated into another production, bringing their unique personality and actions. Real-life instances of this include when Olivia Pope from the American political thriller television series, Scandal showed up for an episode on How to Get Away with Murder or when Jake Peralta from the American police procedural comedy television series, Brooklyn Nine-Nine made a cameo appearance on New Girl. In object-oriented programming, this translates to the ability to create classes or objects that can be reused across different projects whilst encapsulating specific functionalities. This promotes efficient development as it is like having a library of well-developed characters ready to perform in different stories. To summarize, object-oriented programming excels in code reusability due to its modular and encapsulated nature.
Collaboration: Another viable comparison
Imagine having to rewrite an entire chapter of a book just to incorporate a new plot twist. That would be challenging and may disrupt the coherence of the entire story. In a similar sense, with traditional storytelling, the narrative follows a predetermined linear path - once the story is written and published, making significant changes would require altering the entire text, which can be a complex and time-consuming process. This rigidity is akin to procedural programming.
On the other hand, let's consider interactive theater. In this context, to incorporate a new plot twist - say the villain's origin story, only the attributes of the villain need to change and it seamlessly fits into the narrative. This adaptability mirrors the flexibility of object-oriented programming which in turn encourages collaboration.
Collaboration is essential in software development as it brings multiple individuals together to collectively create high-quality software products. For instance, another person's perspective can shed new light on your code, potentially identifying flaws, optimizations, or alternative solutions that you might have missed. Given that using object-oriented programming, the codebase is divided into discrete objects, each encapsulating specific behavior and data, it is easier to edit and collaborate. This parallels how scripts can be changed more easily with the interactive theater point of view.
APIE: Using the analogy
APIE stands for the four core principles of object-oriented programming – abstraction, polymorphism, inheritance and encapsulation. Now, in terms of the earlier analogy of interactive theater, here is what they imply:
Abstraction
Abstraction involves focusing on what an object does rather than how it does it. In other words, abstraction distills the essential characteristics of an object while hiding unnecessary details. In the context of our analogy, it suggests that when crafting a compelling story, you use the essential elements while omitting unnecessary details.
- A diagram illustrating abstraction, with layers of details being abstracted away, leaving only the essential features.
Just as a skilled storyteller focuses on the core narrative, characters, and pivotal events, abstraction in object-oriented programming involves hiding all but the relevant data about an object to reduce complexity and increase efficiency.
Polymorphism
Polymorphism as a fundamental concept in object-oriented programming allows objects of different classes to be treated as if they are objects of a common base class. It enables you to write more flexible and versatile code by abstracting away the specific implementation details of each class.
In the context of our analogy, imagine you're the director of a superhero interactive theater franchise and you have two iconic characters: Superman and Wonder Woman. Each of these characters has unique abilities, background stories, and characteristics that make them distinct. As the director, you have a crucial scene where both Superman and Wonder Woman need to save the day. However, you want to keep your script and scenes as flexible as possible, allowing for different superheroes to be included in the future. This is where polymorphism comes into play: You decide to create a common base concept called "Superhero" that captures the core traits of all superheroes: their ability to save the day and make a difference. This base concept includes elements like wearing a signature costume, having a secret identity, and using unique powers to combat evil.
Now, in your movie scenes, whenever there's a moment that requires a superhero to step in and use their abilities, you simply set the stage for a "Superhero" to shine. You don't need to know in advance whether it will be Wonder Woman or Superman or any other superhero that comes along later. You trust that whoever steps into the role of the "Superhero" will embody those essential traits and characteristics.
In this analogy, the "Superhero" base concept represents the polymorphic aspect. It allows you, the director, to create scenes and scenarios that are open to interpretation by different specific superheroes, each with their unique abilities and stories. The audience sees these characters as superheroes, regardless of whether they have super strength like Wonderwoman or rely on gadgets like Batman.
This flexible approach not only makes your movies more adaptable for future additions to the superhero cast but also reinforces the idea that the superhero archetype is more about the core values and actions rather than the specific individual wearing the cape and mask. This is similar to how polymorphism in object-oriented programming allows you to interact with objects of different classes through a common interface, regardless of their specific implementations.
Inheritance
Inheritance is a mechanism that allows a new class (subclass or derived class) to inherit properties and behaviors from an existing class (superclass or base class). This promotes code reuse and hierarchy. This mimics the way characters' traits are inherited by their successors in a theatrical series.
- A visual representation of inheritance, with a hierarchy of people, showing how properties and behaviors are inherited from a base class.
Just as a new character in a show retains certain characteristics from its predecessor, subclasses inherit and extend the functionalities of their parent class, fostering code reuse and maintaining a structured hierarchy. As a test, would you say Tariq St. Patrick from the spin-off and sequel to the popular crime drama television series Power, Ghost is a sub-class of James St. Patrick from the original show? I think yes.
Encapsulation
Encapsulation involves bundling data and methods that operate on the data into a single unit, known as a class. This paradigm serves as a protective barrier that prevents outside code from directly accessing or modifying an object's internal state. This shields the object's internal complexities and ensures that changes to the object's implementation do not disrupt other parts of the codebase.
Encapsulation is similar to the backstage management of an interactive theater production. Behind the scenes, various aspects such as costumes, props, and scripts are carefully organized and managed to ensure a smooth and cohesive performance. Just as audience members at the theater only interact with the on-stage actors and props, encapsulation restricts external access to an object's internal state, allowing interactions through well-defined interfaces. This safeguards the integrity of the object and maintains the overall stability of the codebase.
In all, these principles contribute to well-structured, maintainable, and adaptable software systems, just as they enhance the dynamics and effectiveness of storytelling and theatrical performances. Finally, I admit that in some instances, the analogies might have been flawed. After all, we can rarely have two situations with the same peculiarities. However, I hope I have been able to shed more light on the concept of Object-Oriented Design a little more for you.