SlideShare a Scribd company logo
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
1,350,000
Our vision is
0
Fatal accidents every year globally.
|
The Zenseact Team
600
Employees
150+
Perception Experts
60+
Deep Learning Engineers
10+
World First Active Active
SafetyServices
COMPETENCES
▪ Pure software company with ONE product focus
▪ Global AI and Edge learning center HQ in Sweden
▪ Built around the inventors of Active Safety
▪ 20+ years of production experience worldwide
Shanghai, China
Gothenburg
HQ
California
|
OnePilot: From somewhere to everywhere
ZAS: ONE PILOT
Driver Vehicle
Driver
DRIVE CRUISE RIDE
|
OnePilot Real life example
Towards ZERO
faster.
How to write SOLID C++
...and why it matters!
Join at slido.com
#solid
About me Dimitrios Platis
● Grew up in Rodos, Greece
● Software Engineer @ Zenseact, Sweden
● Course responsible @ DIT112, DAT265
● C++ courses
○ Beginner
○ Advanced
○ Software Design & Architecture
● Interests:
○ Embedded systems
○ Open source software & hardware
○ Robots, Portable gadgets, IoT, 3D printing
● Meetups
○ Embedded@Gothenburg
○ grcpp
● Website: https://platis.solutions
What do you work with?
ⓘ Start presenting to display the poll results on this slide.
How familiar are you with
the SOLID principles?
ⓘ Start presenting to display the poll results on this slide.
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
SOLID
SOLID is an acronym for five design
principles intended to make software
designs more understandable,
flexible and maintainable. They are a
subset of many principles promoted by
Robert C. Martin. Though they apply to
any object-oriented design, the SOLID
principles can also form a core
philosophy for agile development.
- https://en.wikipedia.org/wiki/SOLID
High cohesion
● Responsibility over a single
purpose
● The class should only have one
"reason" to change
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
struct PhotoUploader {
PhotoUploader(Camera& camera, CloudProvider& cloudProvider);
Image getImage();
void resizeImage(int x, int y, Image& image);
void uploadImage(Image& image);
};
const string kPrefix = "user-";
struct UserManager {
UserManager(Database& database) : db{database} {}
void createUser(string username) {
if (!username.startsWith(kPrefix)) {
username = kPrefix + username;
}
db << "insert into users (name) values (?);" << username;
}
string getUsersReport() {
string users;
db << "select name from users" >> [&users](string user) {
users += user.erase(0, kPrefix.length()) + ",";
};
return users;
}
};
struct DatabaseManager {
virtual ~DatabaseManager() = default;
virtual void addUser(string username) = 0;
virtual vector<string> getAllUsers() = 0;
};
struct MyDatabaseManager : public DatabaseManager {
MyDatabaseManager(Database& database);
void addUser(string username) override;
vector<string> getAllUsers() override;
};
struct UsernameFormatter {
virtual ~UsernameFormatter() = default;
virtual string format(string username) = 0;
virtual string getReadableName(string input) = 0;
};
Abstract interfaces
● Open for extension, closed for
modification
● Add new functionality without
having to change existing code
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
enum class SensorModel {
Good,
Better
};
struct DistanceSensor {
DistanceSensor(SensorModel model) : mModel{model} {}
int getDistance() {
switch (mModel) {
case SensorModel::Good :
// Business logic for "Good" model
case SensorModel::Better :
// Business logic for "Better" model
}
}
};
struct DistanceSensor {
virtual ~DistanceSensor() = default;
virtual int getDistance() = 0;
};
struct GoodDistanceSensor : public DistanceSensor {
int getDistance() override {
// Business logic for "Good" model
}
};
struct BetterDistanceSensor : public DistanceSensor {
int getDistance() override {
// Business logic for "Better" model
}
};
Substitutability
● A subclass should satisfy not
only the syntactic but also the
behavioral expectations of the
parents
● The behavior of the various
children of a class should be
consistent
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
struct InertialMeasurementUnit {
virtual ~InertialMeasurementUnit() = default;
virtual int getOrientation() = 0;
};
struct Gyroscope : public InertialMeasurementUnit {
/**
* @return orientation in degrees [0, 360)
*/
int getOrientation() override;
};
struct Accelerometer : public InertialMeasurementUnit {
/**
* @return orientation in degrees [-180, 180)
*/
int getOrientation() override;
};
struct InertialMeasurementUnit {
virtual ~InertialMeasurementUnit
() = default;
/**
* Sets the frequency of measurements
* @param frequency (in Hertz)
* @return Whether frequency was valid
*/
virtual bool setFrequency(double frequency) = 0;
};
struct Gyroscope : public InertialMeasurementUnit {
// Valid range [0.5, 10]
bool setFrequency(double frequency) override;
};
struct Accelerometer : public InertialMeasurementUnit {
// Valid range [0.1, 100]
bool setFrequency(double frequency) override;
};
struct InertialMeasurementUnit {
virtual ~InertialMeasurementUnit() = default;
/**
* Sets the frequency of measurements
* @param frequency (in Hertz)
* @throw std::out_of_range exception if frequency is invalid
*/
virtual void setFrequency(double frequency) = 0;
/**
* Provides the valid measurement range
* @return <minimum frequency, maximum frequency>
*/
virtual pair<double, double> getFrequencyRange() const = 0;
};
Low coupling
● No one should depend on
methods they do not use
● Multiple single-purpose
interfaces are better than one
multi-purpose
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
struct Runtime {
virtual ~Runtime() = default;
virtual void sendViaI2C(vector<byte> bytesToSend) = 0;
virtual vector<byte> readViaI2C(int numberOfBytesToRead) = 0;
virtual void sendViaUART(vector<byte> bytesToSend) = 0;
virtual vector<byte> readViaUART(int numberOfBytesToRead) = 0;
virtual void setPinDirection(int p, PinDirection d) = 0;
virtual void setPin(int pin) = 0;
virtual void clearPin(int pin) = 0;
};
struct SerialManager {
virtual ~SerialManager() = default;
virtual void registerReceiver(function<void(string)> receiver) = 0;
virtual void send(string message) = 0;
virtual void readLine() = 0;
};
struct MySerialManager : public SerialManager {
void registerReceiver(function<void(string)> receiver) override;
void send(string message) override;
void readLine() override;
};
struct AsynchronousReader {
virtual ~AsynchronousReader() = default;
virtual void registerReceiver(function<void(string)> receiver) = 0;
}
struct SerialClient {
virtual ~SerialClient() = default;
virtual void readLine() = 0;
virtual void send(string message) = 0;
};
struct MySerialManager : public SerialClient, public AsynchronousReader {
void registerReceiver(function<void(string)> receiver) override;
void send(string message) override;
void readLine() override;
};
Loose coupling
● High-level modules should not
depend on low-level modules,
both should depend on
abstractions
● Abstractions should not depend
on, or leak, details from the
implementation domain
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
struct AwsCloud {
void uploadToS3Bucket(string filepath) { /* ... */ }
};
struct FileUploader {
FileUploader(AwsCloud& awsCloud);
void scheduleUpload(string filepath);
};
struct Cloud {
virtual ~Cloud() = default;
virtual void uploadToS3Bucket(string filepath) = 0;
};
struct AwsCloud : public Cloud {
void uploadToS3Bucket(string filepath) override { /* ... */ }
};
struct FileUploader {
FileUploader(Cloud& cloud);
void scheduleUpload(string filepath);
};
struct Cloud {
virtual ~Cloud() = default;
virtual void upload(string filepath) = 0;
};
struct AwsCloud : public Cloud {
void upload(string filepath) override { /* ... */ }
};
struct FileUploader {
FileUploader(Cloud& cloud);
void scheduleUpload(string filepath);
};
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
Takeaways
● SOLID enables agility
● SOLID is indirectly promoted by
C++ Core guidelines
○ C.120, C.121, I.25 etc
● SOLID results in testable code
● Many of the SOLID principles
can be adopted by non-OOP
languages
How applicable are SOLID
principles in real-life
projects?
ⓘ Start presenting to display the poll results on this slide.
Quiz 1. Go to SOCRATIVE.COM
2. Select Login -> Student login
3. Room name: PLATIS
Let's keep in
touch!
https://www.linkedin.com/in/platisd
dimitris@platis.solutions
@PlatisSolutions
JetBrains lottery
1 year license for any Jetbrains IDE!
1. Go to: http://plat.is/jetbrains
2. If you won, please stick around until I
contact you
3. If you did not win, better luck next time!
Did you know that as a university student you
can get a free JetBrains license anyway?
Ad

More Related Content

What's hot (20)

Bind me if you can
Bind me if you canBind me if you can
Bind me if you can
Ovidiu Farauanu
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
Chaitanya Kn
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
vidyamittal
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
Alfonso Peletier
 
Encoder + decoder
Encoder + decoderEncoder + decoder
Encoder + decoder
COMSATS Abbottabad
 
The State of JavaScript
The State of JavaScriptThe State of JavaScript
The State of JavaScript
Domenic Denicola
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
Rajiv Gupta
 
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
Eduardo Lundgren
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
Eduardo Lundgren
 
Aho-Corasick string matching algorithm
Aho-Corasick string matching algorithmAho-Corasick string matching algorithm
Aho-Corasick string matching algorithm
Takatoshi Kondo
 
Box2D with SIMD in JavaScript
Box2D with SIMD in JavaScriptBox2D with SIMD in JavaScript
Box2D with SIMD in JavaScript
Intel® Software
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
Binary Studio
 
Ricky Bobby's World
Ricky Bobby's WorldRicky Bobby's World
Ricky Bobby's World
Brian Lonsdorf
 
Arduino coding class part ii
Arduino coding class part iiArduino coding class part ii
Arduino coding class part ii
Jonah Marrs
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
Brian Lonsdorf
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
Day 1
Day 1Day 1
Day 1
Pat Zearfoss
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
vidyamittal
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
Alfonso Peletier
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
Rajiv Gupta
 
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
Eduardo Lundgren
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
Eduardo Lundgren
 
Aho-Corasick string matching algorithm
Aho-Corasick string matching algorithmAho-Corasick string matching algorithm
Aho-Corasick string matching algorithm
Takatoshi Kondo
 
Box2D with SIMD in JavaScript
Box2D with SIMD in JavaScriptBox2D with SIMD in JavaScript
Box2D with SIMD in JavaScript
Intel® Software
 
Arduino coding class part ii
Arduino coding class part iiArduino coding class part ii
Arduino coding class part ii
Jonah Marrs
 

Similar to Writing SOLID C++ [gbgcpp meetup @ Zenseact] (20)

服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript
Qiangning Hong
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT Talk
Constantine Mars
 
Architecture Components
Architecture Components Architecture Components
Architecture Components
DataArt
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
Chris Weldon
 
Android Networking
Android NetworkingAndroid Networking
Android Networking
Maksym Davydov
 
Testing in android
Testing in androidTesting in android
Testing in android
jtrindade
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
DroidConTLV
 
WebXR if X = how?
WebXR if X = how?WebXR if X = how?
WebXR if X = how?
Luis Diego González-Zúñiga, PhD
 
JavaTalks: OOD principles
JavaTalks: OOD principlesJavaTalks: OOD principles
JavaTalks: OOD principles
stanislav bashkirtsev
 
Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02
Sven Ruppert
 
Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the network
Mu Chun Wang
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
DataStax Academy
 
HDTR images with Photoshop Javascript Scripting
HDTR images with Photoshop Javascript ScriptingHDTR images with Photoshop Javascript Scripting
HDTR images with Photoshop Javascript Scripting
David Gómez García
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
Maksym Davydov
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]
Nilhcem
 
Gwt.create
Gwt.createGwt.create
Gwt.create
Mauricio (Salaboy) Salatino
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
Yekmer Simsek
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
Dimitrios Platis
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
Ben Lin
 
NoSQL meets Microservices
NoSQL meets MicroservicesNoSQL meets Microservices
NoSQL meets Microservices
ArangoDB Database
 
服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript服务框架: Thrift & PasteScript
服务框架: Thrift & PasteScript
Qiangning Hong
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT Talk
Constantine Mars
 
Architecture Components
Architecture Components Architecture Components
Architecture Components
DataArt
 
Testing in android
Testing in androidTesting in android
Testing in android
jtrindade
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
DroidConTLV
 
Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02
Sven Ruppert
 
Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the network
Mu Chun Wang
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
DataStax Academy
 
HDTR images with Photoshop Javascript Scripting
HDTR images with Photoshop Javascript ScriptingHDTR images with Photoshop Javascript Scripting
HDTR images with Photoshop Javascript Scripting
David Gómez García
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
Maksym Davydov
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]
Nilhcem
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
Yekmer Simsek
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
Dimitrios Platis
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
Ben Lin
 
Ad

More from Dimitrios Platis (11)

Let's get comfortable with C++20 concepts - Cologne C++ User group
Let's get comfortable with C++20 concepts - Cologne C++ User groupLet's get comfortable with C++20 concepts - Cologne C++ User group
Let's get comfortable with C++20 concepts - Cologne C++ User group
Dimitrios Platis
 
Let's get comfortable with C++20 concepts (XM)
Let's get comfortable with C++20 concepts (XM)Let's get comfortable with C++20 concepts (XM)
Let's get comfortable with C++20 concepts (XM)
Dimitrios Platis
 
[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
Dimitrios Platis
 
OpenAI API crash course
OpenAI API crash courseOpenAI API crash course
OpenAI API crash course
Dimitrios Platis
 
Builder pattern in C++.pdf
Builder pattern in C++.pdfBuilder pattern in C++.pdf
Builder pattern in C++.pdf
Dimitrios Platis
 
Interprocess communication with C++.pdf
Interprocess communication with C++.pdfInterprocess communication with C++.pdf
Interprocess communication with C++.pdf
Dimitrios Platis
 
Introduction to CMake
Introduction to CMakeIntroduction to CMake
Introduction to CMake
Dimitrios Platis
 
Pointer to implementation idiom
Pointer to implementation idiomPointer to implementation idiom
Pointer to implementation idiom
Dimitrios Platis
 
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Dimitrios Platis
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)
Dimitrios Platis
 
[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++
Dimitrios Platis
 
Let's get comfortable with C++20 concepts - Cologne C++ User group
Let's get comfortable with C++20 concepts - Cologne C++ User groupLet's get comfortable with C++20 concepts - Cologne C++ User group
Let's get comfortable with C++20 concepts - Cologne C++ User group
Dimitrios Platis
 
Let's get comfortable with C++20 concepts (XM)
Let's get comfortable with C++20 concepts (XM)Let's get comfortable with C++20 concepts (XM)
Let's get comfortable with C++20 concepts (XM)
Dimitrios Platis
 
[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
Dimitrios Platis
 
Builder pattern in C++.pdf
Builder pattern in C++.pdfBuilder pattern in C++.pdf
Builder pattern in C++.pdf
Dimitrios Platis
 
Interprocess communication with C++.pdf
Interprocess communication with C++.pdfInterprocess communication with C++.pdf
Interprocess communication with C++.pdf
Dimitrios Platis
 
Pointer to implementation idiom
Pointer to implementation idiomPointer to implementation idiom
Pointer to implementation idiom
Dimitrios Platis
 
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Afry software safety ISO26262 (Embedded @ Gothenburg Meetup)
Dimitrios Platis
 
How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)How to create your own Linux distribution (embedded-gothenburg)
How to create your own Linux distribution (embedded-gothenburg)
Dimitrios Platis
 
[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++[grcpp] Refactoring for testability c++
[grcpp] Refactoring for testability c++
Dimitrios Platis
 
Ad

Recently uploaded (20)

Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 

Writing SOLID C++ [gbgcpp meetup @ Zenseact]

  • 2. 1,350,000 Our vision is 0 Fatal accidents every year globally.
  • 3. | The Zenseact Team 600 Employees 150+ Perception Experts 60+ Deep Learning Engineers 10+ World First Active Active SafetyServices COMPETENCES ▪ Pure software company with ONE product focus ▪ Global AI and Edge learning center HQ in Sweden ▪ Built around the inventors of Active Safety ▪ 20+ years of production experience worldwide Shanghai, China Gothenburg HQ California
  • 4. | OnePilot: From somewhere to everywhere ZAS: ONE PILOT Driver Vehicle Driver DRIVE CRUISE RIDE
  • 7. How to write SOLID C++ ...and why it matters! Join at slido.com #solid
  • 8. About me Dimitrios Platis ● Grew up in Rodos, Greece ● Software Engineer @ Zenseact, Sweden ● Course responsible @ DIT112, DAT265 ● C++ courses ○ Beginner ○ Advanced ○ Software Design & Architecture ● Interests: ○ Embedded systems ○ Open source software & hardware ○ Robots, Portable gadgets, IoT, 3D printing ● Meetups ○ Embedded@Gothenburg ○ grcpp ● Website: https://platis.solutions
  • 9. What do you work with? ⓘ Start presenting to display the poll results on this slide.
  • 10. How familiar are you with the SOLID principles? ⓘ Start presenting to display the poll results on this slide.
  • 11. Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle SOLID SOLID is an acronym for five design principles intended to make software designs more understandable, flexible and maintainable. They are a subset of many principles promoted by Robert C. Martin. Though they apply to any object-oriented design, the SOLID principles can also form a core philosophy for agile development. - https://en.wikipedia.org/wiki/SOLID
  • 12. High cohesion ● Responsibility over a single purpose ● The class should only have one "reason" to change Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 13. struct PhotoUploader { PhotoUploader(Camera& camera, CloudProvider& cloudProvider); Image getImage(); void resizeImage(int x, int y, Image& image); void uploadImage(Image& image); };
  • 14. const string kPrefix = "user-"; struct UserManager { UserManager(Database& database) : db{database} {} void createUser(string username) { if (!username.startsWith(kPrefix)) { username = kPrefix + username; } db << "insert into users (name) values (?);" << username; } string getUsersReport() { string users; db << "select name from users" >> [&users](string user) { users += user.erase(0, kPrefix.length()) + ","; }; return users; } };
  • 15. struct DatabaseManager { virtual ~DatabaseManager() = default; virtual void addUser(string username) = 0; virtual vector<string> getAllUsers() = 0; }; struct MyDatabaseManager : public DatabaseManager { MyDatabaseManager(Database& database); void addUser(string username) override; vector<string> getAllUsers() override; }; struct UsernameFormatter { virtual ~UsernameFormatter() = default; virtual string format(string username) = 0; virtual string getReadableName(string input) = 0; };
  • 16. Abstract interfaces ● Open for extension, closed for modification ● Add new functionality without having to change existing code Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 17. enum class SensorModel { Good, Better }; struct DistanceSensor { DistanceSensor(SensorModel model) : mModel{model} {} int getDistance() { switch (mModel) { case SensorModel::Good : // Business logic for "Good" model case SensorModel::Better : // Business logic for "Better" model } } };
  • 18. struct DistanceSensor { virtual ~DistanceSensor() = default; virtual int getDistance() = 0; }; struct GoodDistanceSensor : public DistanceSensor { int getDistance() override { // Business logic for "Good" model } }; struct BetterDistanceSensor : public DistanceSensor { int getDistance() override { // Business logic for "Better" model } };
  • 19. Substitutability ● A subclass should satisfy not only the syntactic but also the behavioral expectations of the parents ● The behavior of the various children of a class should be consistent Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 20. struct InertialMeasurementUnit { virtual ~InertialMeasurementUnit() = default; virtual int getOrientation() = 0; }; struct Gyroscope : public InertialMeasurementUnit { /** * @return orientation in degrees [0, 360) */ int getOrientation() override; }; struct Accelerometer : public InertialMeasurementUnit { /** * @return orientation in degrees [-180, 180) */ int getOrientation() override; };
  • 21. struct InertialMeasurementUnit { virtual ~InertialMeasurementUnit () = default; /** * Sets the frequency of measurements * @param frequency (in Hertz) * @return Whether frequency was valid */ virtual bool setFrequency(double frequency) = 0; }; struct Gyroscope : public InertialMeasurementUnit { // Valid range [0.5, 10] bool setFrequency(double frequency) override; }; struct Accelerometer : public InertialMeasurementUnit { // Valid range [0.1, 100] bool setFrequency(double frequency) override; };
  • 22. struct InertialMeasurementUnit { virtual ~InertialMeasurementUnit() = default; /** * Sets the frequency of measurements * @param frequency (in Hertz) * @throw std::out_of_range exception if frequency is invalid */ virtual void setFrequency(double frequency) = 0; /** * Provides the valid measurement range * @return <minimum frequency, maximum frequency> */ virtual pair<double, double> getFrequencyRange() const = 0; };
  • 23. Low coupling ● No one should depend on methods they do not use ● Multiple single-purpose interfaces are better than one multi-purpose Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 24. struct Runtime { virtual ~Runtime() = default; virtual void sendViaI2C(vector<byte> bytesToSend) = 0; virtual vector<byte> readViaI2C(int numberOfBytesToRead) = 0; virtual void sendViaUART(vector<byte> bytesToSend) = 0; virtual vector<byte> readViaUART(int numberOfBytesToRead) = 0; virtual void setPinDirection(int p, PinDirection d) = 0; virtual void setPin(int pin) = 0; virtual void clearPin(int pin) = 0; };
  • 25. struct SerialManager { virtual ~SerialManager() = default; virtual void registerReceiver(function<void(string)> receiver) = 0; virtual void send(string message) = 0; virtual void readLine() = 0; }; struct MySerialManager : public SerialManager { void registerReceiver(function<void(string)> receiver) override; void send(string message) override; void readLine() override; };
  • 26. struct AsynchronousReader { virtual ~AsynchronousReader() = default; virtual void registerReceiver(function<void(string)> receiver) = 0; } struct SerialClient { virtual ~SerialClient() = default; virtual void readLine() = 0; virtual void send(string message) = 0; }; struct MySerialManager : public SerialClient, public AsynchronousReader { void registerReceiver(function<void(string)> receiver) override; void send(string message) override; void readLine() override; };
  • 27. Loose coupling ● High-level modules should not depend on low-level modules, both should depend on abstractions ● Abstractions should not depend on, or leak, details from the implementation domain Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 30. struct AwsCloud { void uploadToS3Bucket(string filepath) { /* ... */ } }; struct FileUploader { FileUploader(AwsCloud& awsCloud); void scheduleUpload(string filepath); };
  • 31. struct Cloud { virtual ~Cloud() = default; virtual void uploadToS3Bucket(string filepath) = 0; }; struct AwsCloud : public Cloud { void uploadToS3Bucket(string filepath) override { /* ... */ } }; struct FileUploader { FileUploader(Cloud& cloud); void scheduleUpload(string filepath); };
  • 32. struct Cloud { virtual ~Cloud() = default; virtual void upload(string filepath) = 0; }; struct AwsCloud : public Cloud { void upload(string filepath) override { /* ... */ } }; struct FileUploader { FileUploader(Cloud& cloud); void scheduleUpload(string filepath); };
  • 33. Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle Takeaways ● SOLID enables agility ● SOLID is indirectly promoted by C++ Core guidelines ○ C.120, C.121, I.25 etc ● SOLID results in testable code ● Many of the SOLID principles can be adopted by non-OOP languages
  • 34. How applicable are SOLID principles in real-life projects? ⓘ Start presenting to display the poll results on this slide.
  • 35. Quiz 1. Go to SOCRATIVE.COM 2. Select Login -> Student login 3. Room name: PLATIS
  • 37. JetBrains lottery 1 year license for any Jetbrains IDE! 1. Go to: http://plat.is/jetbrains 2. If you won, please stick around until I contact you 3. If you did not win, better luck next time! Did you know that as a university student you can get a free JetBrains license anyway?