Redundant Coding is No More

02 Dec 2020

Am I Coding The Smart Way or The Wrong Way?

When starting off as a beginner programmer or a student wanting to learn the wonders of programming, you would often have to face with repetitive or redundant coding and wonder to yourself if this can be improved upon to mitigate the redundancy and make it more flexible. Well…. that’s what a design pattern is! The formal definition of a design pattern in software engineering is that it is a general repeatable solution to a commonly occurring problem in software design. While a design pattern isn’t a finished design that can be just a plug in solution to any code but more of a description or template for solving the problem that can be used in different situations. Now that you know what a design pattern is, lets look at if your code is using it. Is the code able to speed up the development process by providing tested, proven development paradigms or does it prevent subtle issues that can cause major problems that improves code readability? If the answer is yes then your coding the smart way and there many other ways that design pattern can be utilized in coding. Design patterns can also be split up into three different types being creational, structural, and behavioral where creational is all about class instantiation, structural is about Class and Object composition, and behavioral is about Class’s objects communication. Now that we know the general overview of design patterns lets look into examples of how its used in everyday coding!

Using Design Patterns In My Code Without Realizing It

Looking back through all my coding work I’ve done so far, I never realized that the simple things in my coding were design patterns like using a iterator. But why is a iterator a design pattern? It’s because it is a behavioral design pattern that lets you traverse elements of a collection without exposing the content inside like a list, stack, tree, etc. Since collections are one of the most used data types in programming there must be a some way of accessing the elements so other code and use these elements and they all require some sort of traversal and the solution to that is to use a iterator. Implementing the algorithm allows the iterator object to encapsulate all of the traversal details like where is the current position or how many elements are left in the traversal. A iterator has a couple uses like you would want to use it to reduce duplication of traversal code across your app development, when you want your code to traverse different data structures or when the types of these structures are unknown, and if the collection is a complex data structure and would want to hide it from clients. Another design pattern that I most commonly used is facade which is part of the structural design pattern that provides a simplified interface to a library, framework, or any other complex set of classes. Lets say that my code needed a single method from a library that contains a bunch of methods that I will never use but just want that tiny bit of its functionality to make my code work. That’s basically what a facade is and you use it so much in programming that you would often overlook a simple thing that you have been using. The basic use of a facade pattern is when you need to have a limited but straightforward interface to a complex subsystem or you want to structure a subsystem into layers. Looking at these two design patterns I mentioned there may be many more that I have used or probably all of them without even realizing it and how useful they are to my coding. Its best to start learning about design patterns because it can offer a lot of tried and tested solutions to common problems in software design and it defines a common language that can be used among peers to communicate to each other efficiently.