Edition 7: Principles and Patterns
Image Credit: M365 Stock Images

Edition 7: Principles and Patterns

I’ll be using the Strategy pattern to abstract the sorting algorithm for orders. Since the algorithm itself should be configurable, I’ll be using the Factory pattern to instantiate the right class based on config. I’ll also implement the Cache Aside pattern to cache the sorted collections of orders”, Surya announced brimming with confidence, as he and Rohit navigated through some code during the code review. Surya was a junior developer on the team and Rohit was their Lead Engineer.

Rohit looked up towards the ceiling while scratching his beard. “I am trying to recollect that saying…Yeah. If your only tool is a hammer, then everything looks like a nail”, he said sighing. “You see, just because you attended a 32-hour course on design patterns last week, it doesn’t mean you start every design with a pattern. Get my drift?

But what is wrong with my approach?”, Surya contested, his face showing some disappointment and confusion at the same time.

While I may not be close to all user stories, I do understand the data model and the architecture. If I were you, I would delegate the sorting entirely to the database as it might be the most efficient option for this situation. I think the sorting algorithms you are talking about are not actual algorithms - you know, like Quick or Hash sort, but several combinations of SQL WHERE clauses - based on fields like Priority, Pin Code etc. Is that not right?”, Rohit questioned while being a little animated.

True”, replied Surya exhaling aloud. “But the way I see it, without the strategy pattern, I might end up with a whole bunch of if blocks or switch blocks, which are not good for maintainability right? Also, I am sure there will be future requirements to make this configurable

Oh! There is no user story for configurability, and you just assumed that?”, Rohit interrupted, with a slightly angered tone. “Look, don’t get too ahead of yourself. I made this mistake myself when I was younger. It is easy to get carried away with something cool and losing sight of the actual task. One should always try to achieve the simplest and cleanest design that meet all requirements first. In fact, my approach is to start with SOLID principles and not patterns while thinking about design

SOLID principles?”, Surya questioned, making it obvious that he had never heard of this before.

I am shocked that your 4-day design patterns training didn’t cover SOLID principles”, Rohit exclaimed with visible frustration. “In general, you continuously refactor code to eliminate smells that violate SOLID principles. Often, you would end up with a pattern automatically. In other words, you ‘Refactor to Patterns’, not start with it.

I hear what you are saying. Let me work on this and we’ll review again tomorrow”, Surya said, his face showing a lot of disappointment.

Look, this is a constructive exercise. We all have been through this. Will look forward to the second iteration”, Rohit replied with an assuring tone as he got up, getting ready to leave. “And hey, look up Uncle Bob. I would strongly recommend all his books. OK then, see you later. Thanks!

Good read Manoj. Interesting way to explain the topic.

To view or add a comment, sign in

More articles by Manoj Ganapathi

Others also viewed

Explore content categories