cover-img

Coding is Art: Incorporating Design Patterns Into Your Code

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

29 March, 2023

5

5

1

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.

Everything begins with a sketch

https://media.tenor.com/oPhQ6Hhm94MAAAAC/arts-drawing.gif

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.

image.png

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

image.png

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


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?

image.png

Examples of Design Patterns: Art vs. Code


image.png

Example of Design Patterns: Adapter Pattern

image.png

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:

image.png

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.


image.png

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.

image.png

Here's an example implementation of the DbsBankResponseAdapter:
image.png

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

image.png

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.png

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 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!

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!


5

5

1

WWCode Singapore
Women Who Code is a global community dedicated to inspiring women to excel in technology careers.
Victoria Lo
A nerd in books, tea, games and software.

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

Β© Copyright 2024. Showcase Creators Inc. All rights reserved.