The 3 Steps To Writing A Good Software Design Pattern

June 15, 2022 5 Comments

Introduction

A good design pattern is the diamond in the rough of software development. It's a go-to solution to a common problem that gets better with each iteration, and it's always there when you need it. The best part about design patterns is that they're made by developers like us: people who just love to code and find better solutions to problems along the way. 

Just because someone else wrote a pattern, though, doesn't mean we can't learn from them; in fact, reading through widely-used patterns is one of the best ways to get better at coding ourselves. So what makes an effective design pattern? And how can we use these strategies in our own work?

Use Good Names

Good names are a core part of writing good software. A name should be:

  • Descriptive, not vague. It should tell the reader what the class does. The more specific the name is in terms of what it does and how it works, the better your design will be.

 

  • Consistent with other names in your system, especially if you're building an open-source library or framework for others to use and build upon. Consistency helps developers understand how things work together so that they can quickly learn new codebases which mean less time spent hunting for bugs or re-implementing features again from scratch because no one else has yet written them!

 

  • Short enough that it doesn't take up too much space on the screen when you're reading code; long enough that it conveys meaning without being overly verbose.

Give Plenty Of Context

After you've gotten the reader's attention and explained what they need to know, make sure to tell them how they can use this new pattern. You might want to:

  • Give an example of when it would be useful.

  • Explain how it works (step by step).

  • Show why it's better than other solutions that have been tried before (or are currently being used).

Provide A Few Examples

  • Provide a few examples of how the pattern can be used.

  • Provide a few examples of how the pattern is different from other patterns.

  • Provide a few examples of how the pattern can be extended.

  • Provide a few examples of how the pattern can be combined with other patterns.

Good Software Design Patterns Are Clear

It is important to be precise in naming your components and give the reader context.

  • Good names should be self-explanatory. For example, 'User Model'.

  • Good names can describe what something does, like 'Login Controller'.

  • Good names can name a category of objects that perform similar tasks, like 'User Interface' or 'Data Access Layer'.

Conclusion

Good design patterns should be clear, concise and easy to understand. A good way of achieving this is by naming your components correctly and giving them enough context. Examples are also helpful for giving the reader some real-life situations where they might use the pattern.