- Clean code
- Naming conventions
- Code formatting
- KISS
- DRY
- YAGNI
- SOLID
- GRASP
- Testable code
- Dependency injection
- Test doubles: dummy, stub, mock
- TDD
Your company needs a controller that can properly cool down some other devices. It will use 2 devices: a thermometer to read the temperature and a fan to control.
Your architect has already proposed interfaces.
The SlowThermometer will be used as a thermometer in your product. It provides with a current temperature. It is a vendor implementation and you cannot change it.
Fan must be controlled appropriately and must speed up when the temperature is too high or should be disabled when it is too low. Fan speed (in rpm - rotations per minute) can be set from 1000 to 3000. It can also be equal to 0, what means that the fan is disabled.
The Controller require a termomether and a fan to work. Without them it does not work. It also needs a target temperature and a tolerance. When the temperature is in a range <targetTemperature - tolerance, targetTemperature + tolerance>, controller should keep the fan speed at 1000 rpm. When it is below targetTemperature - tolerance, the fan should be disabled. When it is above, the fan speed must must be adjusted by 1 rpm per each 0.001 degree. Max speed is 3000 rpm.
Controller can have an LCD display, which displays current temperature, target temperature and a fan speed. It can also work without a display.
Write them on a flipchart / notepad.
-
Never change both implementation and tests, because if tests fails you don't know if tests or implementation are broken
-
Apply your improvements
Spoiler
- missing virtual destructors
- const correctness (const functions, const params, const variables)
- redundant constructors
- broken Rule of 3, 5, 0
- overcomplicated implementation
- dead code
- use default, delete, override
- pass shared_ptrs via
const &
shared_ptr
's constructor ->std::make_shared
Write an implementation of a Controller class in TDD using either Catch2 framework in BDD style with Hippomocks as a mocking framework or using GTest with GMock. Remember about good programming practices.
- extract magic values to consts (constexpr)
- use dependency injection to inject artificial thermometer for testing
- use exception to simplify the error handling
- use STL algorithms
- use references instead of raw pointers
- avoid code duplication