vicennial / GSoC-pgRouting

Semi-mirror repository for GSoC students work
GNU General Public License v2.0
0 stars 0 forks source link

Get familiar with C++ #2

Open vicennial opened 5 years ago

vicennial commented 5 years ago
vicennial commented 5 years ago

Michael VanLoon “STL Algorithms in Action ”

Every C++ developer should strive to learn and use STL to its maximum capability.
Some reasons to use STL are:

In STL, algorithms can be divided into:

In the end, using STL ensure that the developer's code is neat, short and works as expected even in cases that the developer might not have been aware of. Also, multiple STL algorithms can be combined to perform very complicated instructions with relative ease.

vicennial commented 5 years ago

T. Winters & H. Wright “All Your Tests are Terrible..."

Tests are integral to not only maintaining a project but also for further expanding it. Tests ensure that the development of the project can continue without depending on the original developers.

A test must follow 5 properties to be considered a "good" test:

Correctness

The test must verify that the system requirements are actually met. Tests that depend on known bugs or faults in the system must not be included.

Readability

Tests should not contain excessive boilerplate code. It must be easy to read and understand for a user.
The test should be optimised for readability even if it takes more time to do so.
Furthermore, the goal of the test is not to display the advanced features of the testing framework. Keep it simple and straight to the point.

Completeness

Tests should not be created with the intention of them passing.
An optimal suite of tests include:

Furthermore, completeness does not imply test every API call. They must only test the API which they are working on.

Demonstrability

While the developers have a deeper understanding of the code than common users, they must not use shortcuts or complicated examples as tests. A test is useless if a user cannot understand what it is trying to do.
Tests should be more like a demonstration which can show the user various different ways to use the API.

Resilience

A test failure must imply a problem in the system and should not be due to unrelated causes. We need to ensure that our tests are free from outside effects and can stand strong on their own.
Some examples of bad tests are:

In the end, a project can quickly spiral down if it lacks effective tests.
Tests, even bad tests, are always better than no tests.

vicennial commented 5 years ago

Kate Gregory “Stop Teaching C"

The premise of this talk is not about whether C is good language or not, but rather "Stop Teaching C When You Want to Teach C++".

It is a common misconception that C is a pre-requisite to C++. In fact, C-Style coding is a bad practice in C++.
A few reasons why learning C as a pre-requisite is not optimal:

It is better to teach core C++ concepts directly rather than at the end. Some advantages of this strategy are:

With C++ taught first, beginners need not go through the complicated usage of printf, char* [], memory handling etc to understand the core concepts.

vicennial commented 5 years ago

Bjarne Stroustrup “Writing Good C++14”

Modern C++ 11/14/17 is almost completely different than the old C++. It is:

However, even with many new advanced features, many people still prefer using C++ in archaic styles or get lost in the details of the language.

One way to prevent this is to have 'rules' on writing code.
However, some drawbacks of typical 'rules' set by some organisations are:

Therefore, we need to specify "good" rules. A structure of a good rule would be:

  1. Reference - Unique number for each rule.
  2. Reason - Why does this rule exist?
  3. Example - How do you apply this rule?
  4. "Don't-do" alternatives - What to avoid?
  5. Exceptions
  6. Enforcement - How is this rule enforced?
  7. See also - Related materials
  8. Note
  9. Discussion - To discuss matters about the rule.

Some properties of a good rule set are:

However, a rule set cannot be too large because people will not be able to apply all of them. Therefore, a set of "core" rules should exist. These core rules could focus on making sure that there are no resource leaks, dangling pointers or any type violations.

ISO C++ has these core rules that one should follow for writing good C++. With these rules followed, there would be:

Since there are quite a lot of rules, a person cannot be expected to remember all of them. Instead, tools should be able to know these rules and should be able to point them out. For ISOC++, clang-tidy is one such tool.

It is not optimal to try building a new language out of these rules since competing with C++ is extremely hard and could split the CPP community. Instead, it is advised to follow these rules in normal C++.

Therefore, good C++ code can be written by following the guidelines of ISO C++.

vicennial commented 5 years ago

Herb Sutter "Back to the Basics! Essentials of Modern C++ Style"

To have a good understanding of Modern C++, the speaker recommends the book " A Tour of C++ " By Bjarne Stroustrup.

Some aspects of the modern C++ coding style:

Loops

Range-based for loops exist in modern C++ and should be used when iterating over collections rather than a typical iteration-based loop. However, if early termination is required, an iteration-based loop is still a good choice.

Pointers & References

In modern C++, there is a tendency to overuse smart pointers rather than the raw * and &. However, this is not always the best practice since there is a performance cost to using smart pointers.

Some guidelines for using pointers are:

Variable Declaration

It is good practice to use the auto keyword where ever possible. The advantages of doing this are"

It is also to be noted that it is very rare that the usage of auto results in a performance drop.
One reason could be that auto x = a; is the same as writing auto x(a);. Thus, the object is not made and copied as the former code block indicates.

Parameter Passing

In old C++, return-by-value was rarely used due to costly copy operations.
However, in recent years Return Value Optimisations takes care of the issue and in Modern C++, it is better to use return by value and let the compiler handle the optimisation.

In short, the defaults of C++ work better than we expect and we should try to adopt them more in our code.

vicennial commented 5 years ago

Bjarne Stroustrup - The Essence of C++

When C++ was created, it had the goals of :

It was created by combining the concepts borrowed from C and Simula. The direct mapping to hardware was taken from C and the zero-overhead abstraction (classes, inheritance, generic programming) were taken from Simula.

Modern C++ places a large amount of value on resource management. For this, it has shared_ptr which implements garbage collection to prevent the misuse of pointers. It also has the unique_ptr implementation for non-shared pointers.
To prevent the old style of returning large data from functions (using pointers), it has move semantics.
Rather than copying the data on return, it instead just returns the handle to the data. This is done through move constructors in classes.

Template Metaprogramming has also become a part of C++ due to its popularity. In C++ 11, constexpr was implemented to replace the misuse of template metaprogramming to generate constant values at compile time.
Furthermore, C++14 improved upon this and provides better interfaces to templates. Thus, Modern C++ can handle generic programming with ease.