Skip to content

Repository files navigation

Smart home refactoring kata in C++

CI C++17 License: MIT

Refactor a smart home controller toward the Dependency Inversion Principle without changing its behavior. Setup is complete when all four starter tests pass.

Overview

This kata complements Clean Code: SOLID, Ep. 13 - Dependency Inversion Principle.

In this exercise, you'll practice refactoring code to adhere to the Dependency Inversion Principle (DIP). You'll be working with a smart home system that controls different types of lights and air conditioner.

Instructions

Exercise 1

The initial code consists of several device classes and the SmartHomeController class, which is responsible for controlling all the devices (lights and air conditioner).

However, the initial code violates the Dependency Inversion Principle, as the SmartHomeController class directly depends on concrete classes. In the tests, we also directly manipulate the dimmable_light and networkable_light instances to check their unique behaviors.

Your goal is to refactor the code to adhere to the Dependency Inversion Principle and make it easier to implement new devices.

Here are some guidelines to follow:

  1. Introduce an interface or several interfaces (or abstract base classes) for the devices that the SmartHomeController interacts with.

  2. Update the SmartHomeController class to depend on this interface, instead of the concrete device classes.

  3. Update the devices to implement these interfaces.

  4. Modify any constructors or method calls as necessary to accommodate the refactoring.

Note

Remember that in this exercise, we assume all devices are in an ideal state and don't account for error cases such as a network connection failure or an attempt to dim a light below 0% or above 100% brightness.

Make sure the program still behaves the same way after your refactoring. There's a unit test in place that checks that on a very rudimentary level by just looking at the output of the program.

Exercise 2

When you're done with refactoring, test the quality of your refactoring by implementing two additional scenarios (methods) in SmartHomeController:

  1. Add the make_quick_breakfast scenario to open the blinds and make a predefined coffee type.

    1. Add Blinds device class with open and close methods (blinds are always powered and does not have an on/off function).
    2. Add make_quick_breakfast method to SmartHomeController.
  2. Add automatic vacuum cleaner and the schedule_night_cleaning scenario, which should start the cleaning once everything else is turned off.

    1. Add VacuumCleaner device class with start_cleaning and stop_cleaning methods.
    2. Add schedule_night_cleaning method to SmartHomeController. After calling this method, the vacuum cleaner should start cleaning once all other devices are turned off.

Evaluate whether your refactoring made life easier for you or not.

Prerequisites

Required:

Optional:

  • GNU Make, for shorter commands. Every required task also has direct CMake and CTest commands. Make may be unavailable on Windows.

You do not need to install GoogleTest separately. CMake finds an installed copy or downloads the pinned release when needed.

Set up the kata

  1. Clone the repository:

    git clone https://github.com/Coding-Cuddles/smart-home-refactoring-cpp-kata.git
  2. Enter the repository directory:

    cd smart-home-refactoring-cpp-kata
  3. Run the starter tests. Use Make when it is installed:

    make test

    Otherwise, use CMake and CTest directly:

    cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
    cmake --build build --config Debug
    ctest --test-dir build --build-config Debug --output-on-failure

The first run may download and build GoogleTest. CTest should report 100% tests passed.

If a command reports a missing compiler or CMake, install that prerequisite and run the setup commands again.

Setup is complete when CTest reports 100% tests passed.

Work on the kata

  1. Refactor smart_home.h while preserving the behavior covered by test_smart_home.cpp.

  2. Run the tests after each change. Use Make when it is installed:

    make test

    Otherwise, use CMake and CTest directly:

    cmake --build build --config Debug
    ctest --test-dir build --build-config Debug --output-on-failure

    Continue when CTest reports 100% tests passed.

Make command reference

Make is optional. Run make or make help to list these commands in the terminal.

Command Result
make all Build and run the test suite
make help Show the Make command reference
make build Configure and build without running tests
make test Build and run the test suite
make format Format tracked C++ and header files
make format-check Check formatting without changing files
make clean Remove generated build artifacts

About

Smart home refactoring kata in C++

Topics

Resources

Stars

1 star

Watchers

2 watching

Forks

Used by

Contributors

Languages