Goals For Better Programming : Sean Parent C++ Seasoning
(JP 07/11/2014 originally presented this taster for the talk with supporting slides from Sean Parent C++ Seasoning)
Supporting material: Going Native 2013:Sean Parent “C++ Seasoning”
Some Highlights
Goals:
- No Raw Loops
- No Raw Synchronization Primitives
- No Raw Pointers (No even unique or shared pointer: Use value types)
Why? Locality of reasoning:
- Easier to reason about
- Composable, General, Correct, Efficient
Striving with the ‘no raw loops’ goal:
- Know the standard algorithms (and boost ones too).
- Use algorithm to explicit what your code is doing.
- Isolate new algorithm from business logic:
- Improve readability with proper names
- Improve reusability / testability
- Look at Sean Parent’s example with std::rotate
Look for simple example improvments in own code base
// Fail the 'No raw loop' goal
bool HasValidProperty( const Object& object )
{
//
// See if object's property is currently in use.
//
const auto objectProp = GetObjectProperty( object );
for( const auto& prop : CurrentProperties() )
{
if ( prop == objectProp )
{
return true;
}
}
return false;
}
// Achieve the 'No raw loop' goal
bool HasValidProperty( const Object& object )
{
return boost::algorithm::any_of_equal(
CurrentProperties(),
GetObjectProperty( object ) );
}
Presented at IndigoVision during a training session on 07/11/2014 with supporting slides from Sean Parent.
This work by Jean-Philippe DUFRAIGNE is licensed under a Creative Commons Attribution 4.0 International License.