Introduction

Welcome to the final lesson of the "Applying Clean Code Principles in Rust" course! Throughout this journey, we've explored essential principles like DRY (Don't Repeat Yourself) and KISS (Keep It Simple, Stupid), delving into how Rust's ownership and borrowing rules promote modular design. In this culminating lesson, we'll dive into the SOLID Principles, a set of design guidelines introduced by Robert C. Martin, famously known as "Uncle Bob." Mastering these principles is crucial for crafting Rust code that is robust, maintainable, and easy to extend. Let's explore how to apply the SOLID Principles in Rust together.

SOLID Principles at a Glance

Before we delve deeper, here's a quick overview of the SOLID Principles and their purposes within the context of Rust:

  • Single Responsibility Principle (SRP): A module, class, or function should have one, and only one, reason to change.
  • Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification.
  • Liskov Substitution Principle (LSP): Subtypes should be substitutable for their base types without altering the correctness of the program.
  • Interface Segregation Principle (ISP): Clients should not be forced to depend upon interfaces that they do not use.
  • Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules; both should depend on abstractions.

These principles guide us in writing code that is flexible, easy to understand, and maintainable. Now, let's explore each principle in the context of Rust, with practical examples.

Single Responsibility Principle

The Single Responsibility Principle (SRP) emphasizes that a module or struct should have one, and only one, reason to change. This means keeping functionality focused and avoiding coupling unrelated responsibilities.

Consider a struct that handles both data representation and file operations:

use std::fs::File;
use std::io::{self, Read, Write};

struct Config {
    filename: String,
    data: String,
}

impl Config {
    fn new(filename: String, data: String) -> Self {
        Config { filename, data }
    }

    fn read(&mut self) -> io::Result<()> {
        let mut file = File::open(&self.filename)?;
        file.read_to_string(&mut self.data)?;
        Ok(())
    }

    fn write(&self) -> io::Result<()> {
        let mut file = File::create(&self.filename)?;
        file.write_all(self.data.as_bytes())?;
        Ok(())
    }
}

In this code, the Config struct handles both configuration data and file I/O operations. Any change in file handling or data structure affects the Config struct; in other words, it violates SRP by mixing data management with file operations.

Let's refactor by separating concerns:

use std::io;

struct Config {
    data: String,
}

impl Config {
    fn new(data: String) -> Self {
        Config { data }
    }
}

struct FileHandler;

impl FileHandler {
    fn read(filename: &str) -> io::Result<Config> {
        use std::fs::File;
        use std::io::Read;
        let mut file = File::open(filename)?;
        let mut data = String::new();
        file.read_to_string(&mut data)?;
        Ok(Config::new(data))
    }

    fn write(filename: &str, config: &Config) -> io::Result<()> {
        use std::fs::File;
        use std::io::Write;
        let mut file = File::create(filename)?;
        file.write_all(config.data.as_bytes())?;
        Ok(())
    }
}

In the refactored code, Config is solely responsible for holding configuration data, while FileHandler deals exclusively with file operations. This implies that changes in file handling only affect FileHandler, while changes in data structure only affect Config.

Adhering to SRP promotes cleaner code and reduces the likelihood of bugs.

Open/Closed Principle

The Open/Closed Principle (OCP) states that software entities should be open for extension but closed for modification. In Rust, we achieve this through traits and implementations, allowing us to add new behavior without altering existing code.

Let's consider this example about a function that calculates areas for different shapes:

enum Shape {
    Circle(f64),         // radius
    Rectangle(f64, f64), // width, height
}

fn calculate_area(shape: &Shape) -> f64 {
    match shape {
        Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
        Shape::Rectangle(width, height) => width * height,
    }
}

In this setup, adding a new shape, like a Triangle, requires modifying the Shape enum and the calculate_area function. This violates OCP since we have to modify existing code to extend functionality.

Let's use traits to make the code open for extension:

trait Shape {
    fn area(&self) -> f64;
}

struct Circle {
    radius: f64,
}

impl Shape for Circle {
    fn area(&self) -> f64 {
        std::f64::consts::PI * self.radius * self.radius
    }
}

struct Rectangle {
    width: f64,
    height: f64,
}

impl Shape for Rectangle {
    fn area(&self) -> f64 {
        self.width * self.height
    }
}

// Adding a new shape doesn't modify existing code
struct Triangle {
    base: f64,
    height: f64,
}

impl Shape for Triangle {
    fn area(&self) -> f64 {
        0.5 * self.base * self.height
    }
}

fn calculate_area(shape: &dyn Shape) -> f64 {
    shape.area()
}

In the refactored code, the Shape trait defines a common interface that should be implemented by each shape struct. Adding a new shape involves creating a new struct and impl block without modifying existing code, not even the calculate_area function since it operates on any object that implements Shape.

By adhering to OCP, we ensure our codebase is resilient to change and easy to extend.

Liskov Substitution Principle

The Liskov Substitution Principle (LSP) asserts that subtypes must be substitutable for their base types without altering the correctness of the program. In Rust, this means that any type implementing a trait should be usable wherever that trait is expected, without causing unexpected behavior or errors.

Consider the following example where we define a Bird trait:

trait Bird {
    fn fly(&self);
}

struct Eagle;

impl Bird for Eagle {
    fn fly(&self) {
        println!("The eagle soars high into the sky.");
    }
}

struct Penguin;

impl Bird for Penguin {
    fn fly(&self) {
        panic!("Penguins can't fly!");
    }
}

In this code, both Eagle and Penguin implement the Bird trait, which requires a fly method. While an Eagle can fly, a Penguin cannot. The Penguin's fly method panics when called. Substituting a Penguin where a Bird is expected could lead to a runtime panic, violating the LSP. The fly method's contract implies that any Bird can fly, but Penguin does not fulfill this contract.

Let's refactor the code to adhere to the LSP by separating flying behavior from the Bird trait:

trait Bird {
    fn lay_egg(&self);
}

trait Flyable {
    fn fly(&self);
}

struct Eagle;

impl Bird for Eagle {
    fn lay_egg(&self) {
        println!("The eagle lays an egg.");
    }
}

impl Flyable for Eagle {
    fn fly(&self) {
        println!("The eagle soars high into the sky.");
    }
}

struct Penguin;

impl Bird for Penguin { // Note: Penguin does not implement Flyable
    fn lay_egg(&self) {
        println!("The penguin lays an egg.");
    }
}

In the refactored code, we've split the behaviors into two traits: Bird and Flyable. The Bird trait includes behaviors common to all birds, such as lay_egg, while the Flyable trait defines the flying behavior. Eagle implements both Bird and Flyable because it can lay eggs and fly. Penguin implements only Bird since it lays eggs but cannot fly.

By doing this, we ensure that any type implementing Flyable truly supports flying, preventing runtime panics. This adheres to the LSP because substituting any Bird or Flyable with their respective implementors will not lead to unexpected behavior.

Interface Segregation Principle

The Interface Segregation Principle (ISP) advises that clients should not be forced to depend upon interfaces they do not use. In Rust, this means designing small, focused traits.

Here's a trait that combines multiple unrelated functionalities:

trait Entity {
    fn save(&self);
    fn load(&self);
    fn validate(&self) -> bool;
}

struct User;

impl Entity for User {
    fn save(&self) {
        println!("Saving user");
    }

    fn load(&self) {
        println!("Loading user");
    }

    fn validate(&self) -> bool {
        // User validation logic
        true
    }
}

struct Config;

impl Entity for Config {
    fn save(&self) {
        println!("Saving config");
    }

    fn load(&self) {
        println!("Loading config");
    }

    fn validate(&self) -> bool {
        // Config doesn't need validation, but forced to implement
        true
    }
}

In this code, Config is forced to implement validate, which may not be relevant. Thus, ISP is violated by requiring structs to implement methods they don't really need.

Let's split the trait into smaller, more focused traits:

trait Persistable {
    fn save(&self);
    fn load(&self);
}

trait Validatable {
    fn validate(&self) -> bool;
}

struct User;

impl Persistable for User {
    fn save(&self) {
        println!("Saving user");
    }

    fn load(&self) {
        println!("Loading user");
    }
}

impl Validatable for User {
    fn validate(&self) -> bool {
        // User validation logic
        true
    }
}

struct Config;

impl Persistable for Config {
    fn save(&self) {
        println!("Saving config");
    }

    fn load(&self) {
        println!("Loading config");
    }
}

// No need to implement `Validatable` for `Config`

In this code, we have more focused interfaces, that is, smaller traits representing specific behaviors. This means that types can implement only the traits relevant to them, making it easier to compose behaviors without unnecessary code.

By adhering to ISP, we create a modular and flexible codebase where components are not burdened by unnecessary dependencies.

Dependency Inversion Principle

The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules; both should depend on abstractions. In Rust, we use traits to define abstractions that decouple modules.

Consider a NotificationService that directly depends on a concrete EmailSender:

struct EmailSender;

impl EmailSender {
    fn send(&self, to: &str, body: &str) {
        println!("Sending email to {}: {}", to, body);
    }
}

struct NotificationService {
    email_sender: EmailSender,
}

impl NotificationService {
    fn new(email_sender: EmailSender) -> Self {
        NotificationService { email_sender }
    }

    fn notify(&self, user: &str, message: &str) {
        self.email_sender.send(user, message);
    }
}

In this code, NotificationService depends directly on EmailSender, which is a concrete implementation. This makes it difficult to substitute EmailSender with another sender (e.g., SmsSender), thus violating the DIP by coupling high-level logic with low-level implementation.

Let's introduce an abstraction using a trait:

trait Messenger {
    fn send(&self, to: &str, body: &str);
}

struct EmailSender;

impl Messenger for EmailSender {
    fn send(&self, to: &str, body: &str) {
        println!("Sending email to {}: {}", to, body);
    }
}

struct SmsSender;

impl Messenger for SmsSender {
    fn send(&self, to: &str, body: &str) {
        println!("Sending SMS to {}: {}", to, body);
    }
}

struct NotificationService<'a> {
    messenger: &'a dyn Messenger,
}

impl<'a> NotificationService<'a> {
    fn new(messenger: &'a dyn Messenger) -> Self {
        NotificationService { messenger }
    }

    fn notify(&self, user: &str, message: &str) {
        self.messenger.send(user, message);
    }
}

In the refactored code, Messenger trait defines an abstraction for sending messages. NotificationService depends on the Messenger trait, not on a concrete implementation; hence, we can inject any messenger that implements Messenger, promoting flexibility.

By adhering to DIP, we create a modular architecture where components are interchangeable and extensible.

Review and Next Steps

In this lesson, we've explored how the SOLID Principles can be applied in Rust to create clean, maintainable, and extensible code:

  • Single Responsibility Principle: Keep modules focused on a single task.
  • Open/Closed Principle: Use traits and implementations to extend functionality without modifying existing code.
  • Liskov Substitution Principle: Ensure implementations honor the contracts of their traits for substitutability.
  • Interface Segregation Principle: Design small, focused traits to avoid forcing implementations to depend on unused interfaces.
  • Dependency Inversion Principle: Depend on abstractions using traits to decouple high-level and low-level modules.

As you proceed to the practice exercises, consider how these principles can guide your coding decisions. Applying them thoughtfully will enhance the quality of your Rust code and contribute to robust software design. Happy coding! 🚀

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal