trainings/CreatingReliableSoftwareCpp/Presentation/good_practise_dry.md

39 lines
No EOL
1.5 KiB
Markdown

# DRY
___
## Don't Repeat Yourself
* <!-- .element: class="fragment fade-in" --> If you have two identical functionality, like searching for some resource, just make a separate function and always call it
* <!-- .element: class="fragment fade-in" --> Create some utility files, which will contain functions reused in your project
* <!-- .element: class="fragment fade-in" --> It could contain functions for:
* <!-- .element: class="fragment fade-in" --> Filtering
* <!-- .element: class="fragment fade-in" --> Searching
* <!-- .element: class="fragment fade-in" --> Removing
* <!-- .element: class="fragment fade-in" --> Operate with string etc...
* <!-- .element: class="fragment fade-in" --> If you have some value/ string which is used in few places, create one <code>constexpr</code> variable for it and always refer to this variable instead magic value/ string
___
```C++
class Foo {
constexpr static const char* DATABASE_NAME = "superDb";
constexpr static size_t CONNECTION_TIMEOUT_SECONDS = 30;
public:
// Don't hardcode value here, instead of create a variable which store this info
void connect(const std::string& dbName = DATABASE_NAME) {
db.createConnection(dbName, CONNECTION_TIMEOUT_SECONDS);
}
bool connected() const { return db.connected(); }
json sendRequest(const json& request) {
// use already implemented functions
if (!connected()) {
connect();
}
return db.request(request);
}
};
```
<!-- .slide: style="font-size: 0.84em" -->