Coding is Art: Incorporating Design Patterns Into Your Code

Coding is Art: Incorporating Design Patterns Into Your Code

A recap of Saloni Kaur's, co-director of WWCode Singapore, talk.

Coding is often described as a science or a technical skill, but it is also an art form. Just like painting, music or sculpture, writing code requires creativity and imagination. A great piece of code is not only functional but also aesthetically pleasing, easy to read and understand. One way to achieve this is by incorporating design patterns into your code. Design patterns are proven solutions to common programming problems that can help you write more elegant and efficient code.

In this article, we will recap the insightful talk given by our co-director of WomenWhoCode Singapore, Saloni Kaur, which explores the relationship between coding and art, and how design patterns can help you elevate your code to the next level.

This article is part of a series called Books Reviews and Reflections, where we discuss book reviews or any reflections that I have learned as a part-time ponderer and productivity nerd.

Everything begins with a sketch

img

The foundation of a painting is a sketch. It is how artists draw the rough outline of a drawing before filling in the rest such as colours, lines and details.

In coding, engineers also begin with a 'sketch'. It is called a sequence diagram. In the image below, we have an example of a sequence diagram of an ATM program.

img

A sequence diagram is a type of diagram used in programming to visualize how objects interact with each other and in what order. Just like an artist's sketch, it is rough outline of how the program works and it achieves the following goals:

  • shows the sequence of messages exchanged between objects to perform a particular task or use case

  • help developers to understand the flow of control and the relationships between objects in a system

  • allows modeling complex interactions between objects and for debugging complex systems

  • useful for communication between developers, designers, and stakeholders as they provide a clear and concise representation of how the system works

Why Coding is Art

1) Starts with a blank canvas

Like artists, coding also starts with a blank canvas. Just as a painter begins with a blank canvas and transforms it into a masterpiece, a programmer starts with a blank code editor and transforms it into a fully functional application.

2) Requires effective use of tools

Both artists and programmers use their creativity and imagination to bring their ideas to life. Just as a painter selects the right colors and brushes to create a particular effect, a programmer selects the right tools and technologies to develop software that meets the user's needs.

3) Requires sense of aesthetics

Spaghetti code vs Clean code

Both art and coding also require a sense of aesthetics. While artists strive to create visually pleasing and harmonious compositions, programmers aim to create code that is both functional and easy to read. In both cases, the result should be aesthetically pleasing to the end-user.

Just as an artist considers the composition, color, and balance of a painting, a programmer considers the architecture, organization, and readability of their code. Aesthetics are not just about making something look pretty, but also about improving functionality and usability.

A well-designed program not only works efficiently but is also easy to maintain and modify. In both art and coding, aesthetics play an essential role in the success of the final product.

4) It is a form of expression

img

John Conwayโ€™s Game of Life Code

In both art and coding, you can freely express anything. A perfect example of this is John Conway's Game of Life code, which is a mathematical algorithm simulating living organisms. Despite its simple rules and algorithms, the code produces an incredible diversity of patterns, which are both beautiful and fascinating. The Game of Life code is a perfect example of how a programmer can use their creativity and imagination to create something both functional and aesthetically pleasing.

5) Requires Practice & Skills

It goes without saying but both art and coding requires a lot of practice. An artist must practice their technique and experiment with different styles and mediums to improve their craft. While a programmer must practice writing code and experimenting with different programming languages and design patterns to become proficient.

It takes time and effort to develop the technical skills and creativity needed to produce something of value. The more a programmer practices, the more they learn, and the better they become at developing efficient and clean software.

6) Never finished

Like art, we are never quite done with our coding adventures. Just as an artist is never fully satisfied with their work and always seeks to improve, a programmer is always looking for ways to refine their code and make it better.

Even when a program is functioning correctly, there is always room for improvement, whether it's improving efficiency, adding new features, or making it more user-friendly. An artist can always find a way to add more depth or meaning to their work, while a programmer can always find ways to optimize their code and make it more elegant.

The iterative nature of both art and coding means that there is always something new to discover and learn, and the journey is just as important as the final product. Ultimately, both art and coding are ongoing processes of exploration, discovery, and refinement, driven by a desire for self-expression and creative fulfillment.

Why we need Design Patterns?

img

Examples of Design Patterns: Art vs. Code

img

Example of Design Patterns: Adapter Pattern

img

The adapter pattern is a programming design pattern that helps make two incompatible interfaces work together. It acts as a mediator between two classes that have different interfaces and enables them to communicate with each other.

For example, imagine you have a mobile app that gets and displays all transactions from different banks. The sequence diagram can look something like the image below:

img

And to get all transactions across different banks, you would need to get data from their provided APIs respectively. The APIs would most likely differ in terms of scheme, parameters, etc. as shown in the image below.

img

To efficiently solve this problem, you can create an interface called Transaction and adapters which implements this interface. Each adapter would transform the original API response to the desired data for the app to work with.

img

Adapters would implement the Transaction interface

Here's an example implementation of the DbsBankResponseAdapter:

img

We adapt the raw data into a common Transaction format using a separate adapter for each bank.

img

Once again to recap, the adapter pattern translates the interface of one class into the interface that the other class expects, without changing the original code of either class. This allows code reuse and makes it easier to maintain and modify code in the future.

Example of Design Patterns: Command Pattern

Our next design pattern example is the Command pattern. Picture this simple process of a customer ordering food.

Image source: https://sourcemaking.com/files/v2/content/patterns/Command_example1.png?id=ce87b152402957e842c1200c305b82c1

When we placeOrder , we would like the waiter to order our food items to the cook all at once in a single object like a check. Chicken salad, pasta, ice tea and cheesecake are all inside that check. We expect the waiter to do this instead of ordering the items one by one. That would be just inefficient, wouldn't it?

This is what the Command pattern essentially is. It is a design pattern where commands or actions are encapsulated as objects. Instead of directly executing a command or action, the object representing the command is stored and used to execute it later. In this example, the check is the object.

This pattern decouples the object that invokes the command from the one that knows how to execute it, allowing for more flexibility and easier extensibility. It is commonly used in applications that require undo/redo functionality or for implementing complex user interface operations.

Conclusion

In this article, we learned about the similarities between coding and art, why we need to learn design patterns and some examples of design patterns. Thanks for reading! This talk was brought to you by WomenWhoCode Singapore (speaker: Saloni Kaur).

If you find this valuable, please join our Showwcase community as we organize many of these types of informative events for free. By joining our events, you can ask your questions in real-time and learn from your peers too!

This article was written with AI assistance :)

Join our Community!

If you share the same cause and mission as WomenWhoCode, please feel free to join our Showwcase Community and follow on our socials so you don't miss any upcoming events!

Let's Connect!

Did you find this article valuable?

Support Victoria Lo by becoming a sponsor. Any amount is appreciated!

ย