Skip to main content

45 docs tagged with "Clean Code"

View All Tags

Composition over Inheritance

Inheritance is often overused, even by experienced developers. A sound rule of software engineering is to minimize coupling: if a relationship can be expressed in more than one way, use the weakest relationship that's practical.

Deleted functions

You can delete functions to prevent them from beeing used. This is mostly used to prevent a type from being copied ; for example std::sharedptr.

Don't overfocus on performance

Because most applications don't really need performance, and correctness is their main concern. Often, the compute-heavy parts are handled by libraries and you don't need to worry about that. If the code that you write doesn't weigh much in the performance scale, don't waste time optimizing it, or worse, don't optimize it at the expense of readability / maintainability !

friend

There are *very few* good reasons to use `friend`.

Naming

There are only two hard problems in computer science: naming, cache invalidation, and off-by-1 errors.

Prefer free functions

C.4: Make a function a member only if it needs direct access to the representation of a class.

Smart Pointers

No naked new : this doesn't mean no dynamic allocations, it just means they should always be done through a smart pointer or a container.

State and Strategy

Difference : State handles the changes itself, it is an implementation of a state machine. For Strategy, the decision of changing the strategy comes from the outside, it doesn't depend on what the current strategy is.

Strong Types

I.4: Make interfaces precisely and strongly typed.

Testing

https://youtu.be/ta3S8CRN2TM?t=1306

The Rule of 5

C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all.

Use structs to group data

C.2: Use class if the class has an invariant; use struct if the data members can vary independently.

Write libraries

Even if it's only for your personal use, it will help you a lot with code reuse across projects and also inside one project. And also it will force you to make your code a little more generic, which most of the time will improve it at the same time. Moreover, it will make sure that the code is decoupled from the rest of your application.