Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Hands-On Design Patterns and Best Practices with Julia
  • Table Of Contents Toc
Hands-On Design Patterns and Best Practices with Julia

Hands-On Design Patterns and Best Practices with Julia

By : Kwong
4.6 (14)
close
close
Hands-On Design Patterns and Best Practices with Julia

Hands-On Design Patterns and Best Practices with Julia

4.6 (14)
By: Kwong

Overview of this book

Design patterns are fundamental techniques for developing reusable and maintainable code. They provide a set of proven solutions that allow developers to solve problems in software development quickly. This book will demonstrate how to leverage design patterns with real-world applications. Starting with an overview of design patterns and best practices in application design, you'll learn about some of the most fundamental Julia features such as modules, data types, functions/interfaces, and metaprogramming. You'll then get to grips with the modern Julia design patterns for building large-scale applications with a focus on performance, reusability, robustness, and maintainability. The book also covers anti-patterns and how to avoid common mistakes and pitfalls in development. You'll see how traditional object-oriented patterns can be implemented differently and more effectively in Julia. Finally, you'll explore various use cases and examples, such as how expert Julia developers use design patterns in their open source packages. By the end of this Julia programming book, you'll have learned methods to improve software design, extensibility, and reusability, and be able to use design patterns efficiently to overcome common challenges in software development.
Table of Contents (19 chapters)
close
close
1
Section 1: Getting Started with Design Patterns
3
Section 2: Julia Fundamentals
7
Section 3: Implementing Design Patterns
15
Section 4: Advanced Topics

Software design principles

While this book does not cover object-oriented programming, some object-oriented design principles are universal and could be applied to any programming language and paradigm. Here, we will take a look at some of the most well-known design principles. In particular, we will cover the following:

  • SOLID: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
  • DRY: Don't Repeat Yourself
  • KISS: Keep It Simple, Stupid!
  • POLA: Principle of Least Astonishment
  • YAGNI: You Aren't Gonna Need It
  • POLP: Principle of Least Privilege

Let's start with SOLID.

SOLID

The SOLID principle consists of the following:

  • S: Single Responsibility Principle
  • O: Open/Closed Principle
  • L: Liskov Substitution Principle
  • I: Interface Segregation Principle
  • D: Dependency Inversion Principle

Let's understand each concept in detail.

Single Responsibility Principle

The Single Responsibility Principle states that every module, class, and function should be responsible for a single functional objective. There should be only one reason to make any changes.

The benefits of this principle are listed here: 

  • The programmer can focus on a single context during development.
  • The size of each component is smaller.
  • The code is easier to understand.
  • The code can be tested more easily.

Open/Closed Principle

The Open/Closed Principle states that every module should be open for extension but closed for modification. It is necessary to distinguish between enhancement and extension—enhancement refers to a core improvement of the existing module, while an extension is considered an add-on that provides additional functionality.

The following are the benefits of this principle

  • Existing components can be easily reused to derive new functionalities.
  • Components are loosely coupled so it is easier to replace them without affecting the existing functionality.

Liskov Substitution Principle

The Liskov Substitution Principle states that a program that accepts type T can also accept type S (which is a subtype of T), without any change in behavior or intended outcome.

The following are the benefits of this principle

  • A function can be reused for any subtype passed in the arguments.

Interface Segregation Principle

The Interface Segregation Principle states that a client should not be forced to implement interfaces that it does not need to use.

The following are the benefits of this principle: 

  • Software components are more modular and reusable.
  • New implementations can be created more easily.

Dependency Inversion Principle

The Dependency Inversion Principle states that high-level classes should not depend on low-level classes; instead, high-level classes should depend on an abstraction that low-level classes implement.

The following are the benefits of this principle: 

  • Components are more decoupled.
  • The system becomes more flexible and can adapt to changes more easily. Low-level components can be replaced without affecting high-level components.

DRY

We'll now cover the DRY principle:

  • D: Don't
  • R: Repeat
  • Y: Yourself

This acronym is a good way of reminding programmers that duplicate code is bad. It is obvious that duplicate code can be difficult to maintain—whenever logic is changed, multiple places in the code are affected.

What do we do when duplicate code is found? Eliminate it and create a common function that is reusable from multiple source files. 

In addition, sometimes code is not 100% duplicated but instead is 90% similar. That is not an uncommon scenario. In that case, consider redesigning the relevant components, possibly refactoring code to a common interface.

KISS

Let's talk about the KISS principle:

  • K: Keep
  • I: It
  • S: Simple
  • S: Stupid!

Often, when we design software, we like to think ahead and try to deal with all kinds of future scenarios. The trouble with building such future-proof software is that it takes exponentially more effort to design and code properly. Practically speaking, it's a conundrum—there is no 100% future-proof solution because technology changes, business changes, and people change. Also, over-engineering could lead to excessive abstraction and indirection, making a system more difficult to test and maintain.

In addition, when using Agile software development methods, we value faster and high-quality delivery over perfection or excess engineering. Keeping the design and code simple is a virtue that every programmer should keep in mind.

POLA

Let's look at the POLA principle:

  • P: Principle
  • O: Of
  • L: Least
  • A: Astonishment

POLA states that a software component should be easy to understand and its behavior should never be a surprise (or, more accurately, astonishing) to the client. How do we do that?

The following are some things to keep in mind:

  • Make sure that the names of the module, function, or function arguments are clear and unambiguous.
  • Ensure that modules are right-sized and well maintained.
  • Ensure that interfaces are small and easy to understand.
  • Ensure that functions have few positional arguments. 

YAGNI

Let's move on to the YAGNI principle:

  • Y: You
  • A: Aren't
  • G: Gonna
  • N: Need
  • I: It

YAGNI says you should only develop software that is needed today. This principle came from Extreme Programming (XP). See what Ron Jeffries, co-founder of XP, wrote in his blog:

"Always implement things when you actually need them, never when you just foresee that you need them."

Software engineers are sometimes tempted to develop functionality that they feel the customer will need in the future. It's been proven time and time again that this is not the most effective way to develop software. Consider the following scenarios:

  • The functionality is never needed by the customer and so the code is never used.
  • The business environment changes and the system has to be redesigned or replaced.
  • The technology changes and the system has to be upgraded to use a new library, a new framework, or a new language. 

The cheapest software is the one that you didn't write. You aren't gonna need it!

POLP

Now, for POLP:

  • P: Principle
  • O: Of
  • L: Least 
  • P: Privilege

POLP states that a client must be given access only to the information or functions that they need. POLP is one of the most important pillars for building secure applications, and it is widely adopted by cloud infrastructure vendors such as Amazon, Microsoft, and Google.

There are quite a few benefits when POLP is applied:

  • Sensitive data is protected and not exposed to non-privileged users.
  • The system can be tested more easily since the number of use cases is limited.
  • The system becomes less prone to misuse because only limited access is given and the interface is simpler.

The software design principles that we have learned about so far are great tools. Although SOLID, DRY, KISS, POLA, YAGNI, and POLP seem to be just a bunch of acronyms, they are useful in designing better software. While SOLID principles came from the object-oriented programming paradigm, SOLID's concepts can still be applied to other languages and environments. As we work through the rest of the chapters in this book, I would encourage you to keep them in mind.

In the next section, we will go over several software quality objectives when designing software.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Hands-On Design Patterns and Best Practices with Julia
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon