147
Copyright Jason Turner @leicus emptycrate.com/idocpp C++ Code Smells C++ Code Smells 1.1

C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Code SmellsC++ Code Smells

1.1

Page 2: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Jason TurnerJason TurnerCo-host of CppCast https://cppcast.comHost of C++ Weekly https://www.youtube.com/c/JasonTurner-le�icusProjects

https://chaiscript.comhttps://cppbestpractices.comhttps://github.com/le�icus/cpp_boxhttps://coloradoplusplus.info

Microso� MVP for C++ 2015-present

1.2

Page 3: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Jason TurnerJason TurnerIndependent and available for training or contracting

https://articles.emptycrate.com/idocpp

1.3

Page 4: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

About my TalksAbout my TalksMove to the front!Please interrupt and ask questionsThis is approximately how my training days look

1.4

Page 5: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best Practices

2.1

Page 6: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best Practices

2.2

Page 7: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesC++ Core Guidelines: 496 (Herb Sutter, Bjarne Stroustrup, et al)

2.2

Page 8: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesC++ Core Guidelines: 496 (Herb Sutter, Bjarne Stroustrup, et al)Effective Modern C++: 42 (Scott Meyers)

2.2

Page 9: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesC++ Core Guidelines: 496 (Herb Sutter, Bjarne Stroustrup, et al)Effective Modern C++: 42 (Scott Meyers)C++ Best Practices: 109 (Me, et al)

2.2

Page 10: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesC++ Core Guidelines: 496 (Herb Sutter, Bjarne Stroustrup, et al)Effective Modern C++: 42 (Scott Meyers)C++ Best Practices: 109 (Me, et al)C++ Coding Standards: 101 (Herb Sutter, Andrei Alexandrescu)

2.2

Page 11: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesC++ Core Guidelines: 496 (Herb Sutter, Bjarne Stroustrup, et al)Effective Modern C++: 42 (Scott Meyers)C++ Best Practices: 109 (Me, et al)C++ Coding Standards: 101 (Herb Sutter, Andrei Alexandrescu)

Just from these 4 items: 748 best practices!

2.2

Page 12: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesThis has a lot of questions…

2.3

Page 13: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesThis has a lot of questions…

How many are unique?

2.3

Page 14: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesThis has a lot of questions…

How many are unique?How many are important?

2.3

Page 15: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesThis has a lot of questions…

How many are unique?How many are important?Which ones can tools tell us about?

2.3

Page 16: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

C++ Best PracticesC++ Best PracticesThis has a lot of questions…

How many are unique?How many are important?Which ones can tools tell us about?

Herb Sutter (CppCon 2018)

We don’t have to teach things all compilers warn on

2.3

Page 17: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode Smells

3.1

Page 18: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsCode Smell:

In computer programming, a code smell is any characteristic in thesource code of a program that possibly indicates a deeper problem.(Wikipedia)

3.2

Page 19: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsCode Smell:

Is it possible to swap these around and look for “smells” instead?

In computer programming, a code smell is any characteristic in thesource code of a program that possibly indicates a deeper problem.(Wikipedia)

3.2

Page 20: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsCode Smell:

Is it possible to swap these around and look for “smells” instead?Do the smells help us reduce the set of best practices?

In computer programming, a code smell is any characteristic in thesource code of a program that possibly indicates a deeper problem.(Wikipedia)

3.2

Page 21: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsCode Smell:

Is it possible to swap these around and look for “smells” instead?Do the smells help us reduce the set of best practices?

I asked Twitter for their favorite C++ Code Smells

In computer programming, a code smell is any characteristic in thesource code of a program that possibly indicates a deeper problem.(Wikipedia)

3.2

Page 22: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

#include <string> void do_work(){  std::string str;  // do some stuff  str = "Hello World";  // work with str} https://godbolt.org/z/7zl9t_

3.3

Page 23: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

#include <string> void do_work(){  std::string str; /// construction  // do some stuff  str = "Hello World"; /// assignment  // work with str} https://godbolt.org/z/baIKi2

3.4

Page 24: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Construction Separate From Assignment -Construction Separate From Assignment -Ben DeaneBen Deane

12345678

#include <string> void do_work(){  // do some stuff  const std::string str = "Hello World";  // work with str} https://godbolt.org/z/mQw9HG

3.5

Page 25: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Construction vs AssignmentConstruction vs Assignment- Ben Deane- Ben Deane

4.1

Page 26: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

10

#include <string> void get_value(std::string &out_param); int main(){  std::string value;  get_value(value);  // use value} https://godbolt.org/z/egT7ec

4.2

Page 27: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Out Variables - Ólafur WaageOut Variables - Ólafur Waage123456789

10

#include <string> std::string get_value(); int main(){   const auto value = get_value();  // use value} https://godbolt.org/z/eL1fLw

4.3

Page 28: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Out Variables - Ólafur WaageOut Variables - Ólafur Waage123456789

10

#include <string> std::string get_value(); int main(){  /// How many parameters does this function take?  const auto value = get_value();  // use value} https://godbolt.org/z/4LqxYi

4.4

Page 29: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Out Variables - ÓlafurOut Variables - ÓlafurWaageWaage

5.1

Page 30: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Construction / Assignment / Out VariablesConstruction / Assignment / Out VariablesSee also this article from Sean Parent:

https://stlab.cc/tips/stop-using-out-arguments.html

5.2

Page 31: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

1011121314151617

#include <vector> void process_more(const std::vector<double> &); void process_data(const std::vector<double> &values) {  bool in_range = true;  for (const auto &v : values) {    if (v < 5.0 || v > 100.0) {      in_range = false;      break;    }  }   if (in_range) {    process_more(values);  }} https://godbolt.org/z/PXsqPk

5.3

Page 32: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Raw Loops - Sean ParentRaw Loops - Sean Parent

Raw loops don’t express intent, but algorithms can.

123456789

1011121314151617

#include <vector>#include <algorithm> void process_more(const std::vector<double> &); void process_data(const std::vector<double> &values) {  const auto in_range = [](const double d) {    return d >= 5.0 && d <= 100.0;  };   // this now reads as a sentence  const bool all_in_range = all_of(begin(values), end(values), in_range);   if (all_in_range) {    process_more(values);  }} https://godbolt.org/z/JtbNhg

5.4

Page 33: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Raw Loops - Sean ParentRaw Loops - Sean Parent(Not via Twitter Tho)(Not via Twitter Tho)

6.1

Page 34: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

101112131415161718

double Data::total_area(){  int value = 0;   // step 1: pipe area  for (int i = 0; i < pipes.size(); ++i) {    value += pipes[i].radius * pipes[i].radius * M_PI;  }   // step 2: hose area  for (int i = 0; i < hose.size(); ++i) {    value += hose[i].radius * pipes[i].radius * M_PI;  }   // and many more   return value;} https://godbolt.org/z/X30YWl

6.2

Page 35: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

101112131415161718

double Data::total_area(){  int value = 0;   // step 1: pipe area  for (int i = 0; i < pipes.size(); ++i) {    value += pipes[i].radius * pipes[i].radius * M_PI;  }   // step 2: hose area  for (int i = 0; i < hose.size(); ++i) {    value += hose[i].radius * pipes[i].radius * M_PI; ///  }   // and many more   return value;} https://godbolt.org/z/4ifcyl

6.3

Page 36: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Multi-Step FunctionsMulti-Step FunctionsInstead decompose steps into functions and/or lambdas.

123456789

1011121314

constexpr double area(const double r) { return r * r * M_PI; } double Data::total_area(){  const auto accumulate_area = [](const auto lhs, const auto rhs) {    return lhs + area(rhs);  }   const auto total_area = [&](const auto &container) {    return std::accumulate(begin(container), end(container), 0.0, accumulate_area);  };   return total_area(pipes) + total_area(hoses) /* + other things */;} https://godbolt.org/z/AXbKzX

6.4

Page 37: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Multi-Step FunctionsMulti-Step FunctionsAre comments necessary in this code?

123456789

1011121314

constexpr double area(const double r) { return r * r * M_PI; } double Data::total_area(){  const auto accumulate_area = [](const auto lhs, const auto rhs) {    return lhs + area(rhs);  }   const auto total_area = [&](const auto &container) {    return std::accumulate(begin(container), end(container), 0.0, accumulate_area);  };   return total_area(pipes) + total_area(hoses) /* + other things */;} https://godbolt.org/z/AXbKzX

6.5

Page 38: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Multi-Step Functions - BjörnMulti-Step Functions - BjörnFahller, Tony Van Eerd &Fahller, Tony Van Eerd &

Peter SommerladPeter Sommerlad

7.1

Page 39: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?12345678

struct Data {  int x;  int y;   bool operator==(Data &rhs) {    return x == rhs.x && y == rhs.y;  }}; https://godbolt.org/z/KTSfn1

7.2

Page 40: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Non-Cannonical OperatorsNon-Cannonical OperatorsWhat is the issue with this code?

12345678

struct Data {  int x;  int y;   bool operator==(const Data &rhs) const { ///    return x == rhs.x && y == rhs.y;  }}; https://godbolt.org/z/swKlu5

7.3

Page 41: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Non-Cannonical OperatorsNon-Cannonical Operators

8.1

Page 42: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

ConversionsConversionsFrom earlier… what do we see?

123456789

101112131415161718

double Data::total_area(){  int value = 0;   // step 1: pipe area  for (int i = 0; i < pipes.size(); ++i) {    value += pipes[i].radius * pipes[i].radius * M_PI;  }   // step 2: hose area  for (int i = 0; i < hose.size(); ++i) {    value += hose[i].radius * hose[i].radius * M_PI;  }   // and many more   return value;} https://godbolt.org/z/5csxkf

8.2

Page 43: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

ConversionsConversionsConversions in at least 1 place, probably 3, loss of data.

123456789

101112131415161718

double Data::total_area(){  int value = 0;   // step 1: pipe area  for (int i = 0; i < pipes.size(); ++i) {    value += pipes[i].radius * pipes[i].radius * M_PI; ///  }   // step 2: hose area  for (int i = 0; i < hose.size(); ++i) {    value += hose[i].radius * hose[i].radius * M_PI; ///  }   // and many more   return value; ///} https://godbolt.org/z/-7HFjp

8.3

Page 44: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456789

1011

#include <string> void use_string(const std::string &s); std::string get_string(); int main(){  const std::string str = get_string();  use_string(str.c_str());} https://godbolt.org/z/Du2E-i

8.4

Page 45: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With Implicit ConstructorsCode With Implicit Constructors

Always exists in code that’s been refactored over a long period.

123456789

1011

#include <string> void use_string(const std::string &s); std::string get_string(); int main(){  const std::string str = get_string();  use_string(str.c_str()); /// string->c_str->string} https://godbolt.org/z/QVamMx

8.5

Page 46: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With ConversionsCode With ConversionsWho can tell me what std::move is?

8.6

Page 47: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With ConversionsCode With ConversionsWho can tell me what std::move is?

An unconditional cast to an r-value reference of the given type.

8.6

Page 48: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With ConversionsCode With ConversionsWho can tell me what std::move is?

An unconditional cast to an r-value reference of the given type.12

std::string s;std::move(s); /// unconditional cast to `std::string &&`

8.6

Page 49: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?1234567

#include <string> std::string get_value(){  std::string s = "Hello There World";  return std::move(s);} https://godbolt.org/z/caqj1e

8.7

Page 50: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With ConversionsCode With Conversions

Pessimizing “return by move” prevents move elision.

1234567

#include <string> std::string get_value(){  std::string s = "Hello There World";  return std::move(s); ///} https://godbolt.org/z/4peiWk

8.8

Page 51: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With ConversionsCode With Conversions

Pessimizing “return by move” prevents move elision.

std::move is another type of conversion that is a code smell.

1234567

#include <string> std::string get_value(){  std::string s = "Hello There World";  return std::move(s); ///} https://godbolt.org/z/4peiWk

8.8

Page 52: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?123456

int main(){  const int i = 4;  const_cast<int &>(i) = 13;  return i; /// what is returned?} https://godbolt.org/z/LylmN3

8.9

Page 53: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Casting Away Casting Away constconst

Modifying a const object during its lifetime is UB. const_cast is anotherexplicit conversion that is a code smell.

123456

int main(){  const int i = 4;  const_cast<int &>(i) = 13;  return i; /// 4 returned} https://godbolt.org/z/MIi_T1

8.10

Page 54: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Weak Typing - or “Which Weak Typing - or “Which intint

is which?”is which?”

9.1

Page 55: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code
Page 56: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp 9.2

Page 57: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With Conversions -Code With Conversions -implicit/explicit/castsimplicit/explicit/casts

(many people)(many people)

10.1

Page 58: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

WarningsWarningsCan our compilers warn us on this code?

123456789

101112131415161718

double Data::total_area(){  int value = 0;   // step 1: pipe area  for (int i = 0; i < pipes.size(); ++i) {    value += pipes[i].radius * pipes[i].radius * M_PI; ///  }   // step 2: hose area  for (int i = 0; i < hose.size(); ++i) {    value += hose[i].radius * pipes[i].radius * M_PI; ///  }   // and many more   return value; ///} https://godbolt.org/z/nmu738

10.2

Page 59: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

WarningsWarningsWhat about here?

1234567

#include <string> std::string get_value(){  std::string s = "Hello There World";  return std::move(s);} https://godbolt.org/z/caqj1e

10.3

Page 60: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code With Warnings - BjörnCode With Warnings - BjörnFahller, Dimitar MirchevFahller, Dimitar Mirchev

11.1

Page 61: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?What are the implications of using a static variable?

123456789

10111213

#include <string> void log_error(std::string const &location,               std::string const &desc); void do_things(bool const error){  static std::string const FunctionName{"do_things"};   if (error) {    log_error(FunctionName, "Error Occured!");  }} https://godbolt.org/z/vNItBg

11.2

Page 62: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?What are the implications of using a static variable?

Each time the variable is accessed it must be checked to see if it’s beeninitialized.

123456789

10111213

#include <string> void log_error(std::string const &location,               std::string const &desc); void do_things(bool const error){  static std::string const FunctionName{"do_things"};   if (error) {    log_error(FunctionName, "Error Occured!");  }} https://godbolt.org/z/vNItBg

11.2

Page 63: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

static conststatic const

Compare to this123456789

10111213

#include <string_view> void log_error(std::string_view const &location,               std::string_view const &description); void do_things(bool const error){  constexpr static std::string_view FunctionName{"do_things"};   if (error) {    log_error(FunctionName, "Error Occured!");  }} https://godbolt.org/z/QsPDhn

11.3

Page 64: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

static conststatic const

Compare to this

static const is a code smell that should probably should be constexpr .

123456789

10111213

#include <string_view> void log_error(std::string_view const &location,               std::string_view const &description); void do_things(bool const error){  constexpr static std::string_view FunctionName{"do_things"};   if (error) {    log_error(FunctionName, "Error Occured!");  }} https://godbolt.org/z/QsPDhn

11.3

Page 65: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

static conststatic const

12.1

Page 66: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?12

// Data.hppextern int const Value;

123

// Data.cpp#include <Data.hpp>int const Value = 5;

12345

// Value.cpp#include <Data.hpp>int getValue() {  return Value;} https://godbolt.org/z/0oUBDO

12.2

Page 67: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?Or simplified:

12345

extern int const Value; int getValue() {  return Value; ///} https://godbolt.org/z/H1buHN

12.3

Page 68: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

extern extern constconst

It’s like we’re telling the compiler:

This is also a code smell.

I have some really important information for you, but I’m not goingto tell you what it is.

12.4

Page 69: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

extern extern constconst

It’s like we’re telling the compiler:

This is also a code smell.

What’s the better option?

I have some really important information for you, but I’m not goingto tell you what it is.

12.4

Page 70: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

extern extern constconst

constexpr

123456

// Data.hppconstexpr int Value = 5; int getValue() {  return Value;} https://godbolt.org/z/-5Z1DH

12.5

Page 71: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

extern extern constconst

13.1

Page 72: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Do We Think?What Do We Think?12345

void use_int(){  int *i = new int(5);  delete i;} https://godbolt.org/z/qU7C8A

13.2

Page 73: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Raw Raw newnew and and deletedelete

Of course this is wasteful and the heap should be avoided if possible.

123456

#include <memory> void use_int(){  auto i = std::make_unique<int>(5);} https://godbolt.org/z/OODlM2

13.3

Page 74: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Raw Raw newnew and and deletedelete

14

Page 75: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode Smells

15

Page 76: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from Assignment

15

Page 77: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut Variables

15

Page 78: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw Loops

15

Page 79: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step Functions

15

Page 80: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical Operators

15

Page 81: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With Conversions

15

Page 82: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With ConversionsCasting Away const

15

Page 83: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With ConversionsCasting Away constCode With Warnings

15

Page 84: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With ConversionsCasting Away constCode With Warningsstatic const

15

Page 85: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With ConversionsCasting Away constCode With Warningsstatic constextern const

15

Page 86: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Code SmellsCode SmellsConstructions Separate from AssignmentOut VariablesRaw LoopsMulti-Step FunctionsNon-Canonical OperatorsCode With ConversionsCasting Away constCode With Warningsstatic constextern constRaw new and delete

15

Page 87: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Some Refactoring CodeSome Refactoring CodeReviewsReviews

16.1

Page 88: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Let’s Update This Code SampleLet’s Update This Code Sample123456789

101112

#include <iostream>using namespace std; int main(){  int length;  string greet1 = "Hello";  string greet2 = ", world!";  string greet3 = greet1 + greet2;   length = greet3.size();} https://godbolt.org/z/hJEDYV

16.2

Page 89: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Let’s Update This Code SampleLet’s Update This Code Sample123456789

1011

#include <iostream>using namespace std; int main(){  const string greet1 = "Hello";  const string greet2 = ", world!";  const string greet3 = greet1 + greet2;   const auto length = greet3.size();} https://godbolt.org/z/eSwuNb

16.3

Page 90: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Let’s Update This Code Sample #2Let’s Update This Code Sample #2123456789

10111213141516

#include <iostream> int main(){  int i, n, fact = 1;   std::cout << "Enter a whole number: ";  std::cin >> n;   for (i = 1; i <= n; ++i) {    fact *= i;  }   std::cout << "\nFactorial of " << n << " = " << fact << std::endl;  return 0;} https://godbolt.org/z/Q2D71b

16.4

Page 91: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Let’s Update This Code Sample #2Let’s Update This Code Sample #2123456789

10111213141516171819202122232425

#include <iostream> template<typename Type> Type read_input() {  Type obj;  std::cin >> obj;  return obj;}constexpr int factorial(int value) {  int result = 1;  while (value > 0) {    result *= value;    --value;  }  return result;} int main() {  std::cout << "Enter a whole number: ";   const auto n = read_input<int>();  const auto fact = factorial(n);   std::cout << "\nFactorial of " << n << " = " << fact << '\n';  return EXIT_SUCCESS;} https://godbolt.org/z/IfdGtn

16.5

Page 92: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

ConclusionsConclusions

17.1

Page 93: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

There Is One Thing That Keeps Coming UpThere Is One Thing That Keeps Coming Up…That Hasn’t Been Explicitly Mentioned

17.2

Page 94: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

ReviewReview

18.1

Page 95: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Construction Separate From AssignmentConstruction Separate From Assignment

vs

1234

void do_work() {  std::string str;  str = "Hello World";} https://godbolt.org/z/sEyCts

123

void do_work() {  const std::string str = "Hello World";}

18.2

Page 96: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Out VariablesOut Variables

vs.

123456

void get_value(std::string &out_param); int main() {  std::string value;  get_value(value);} https://godbolt.org/z/doe0q8

12345

std::string get_value(); int main() {  const auto value = get_value();} https://godbolt.org/z/Sb44wG

18.3

Page 97: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Raw LoopsRaw Loops

vs

123456789

101112

void process_data(const std::vector<double> &values) {  bool in_range = true;  for (const auto &v : values) {    if (v < 5.0 || v > 100.0) {      in_range = false;      break;    }  }  if (in_range) {    process_more(values);  }} https://godbolt.org/z/ugp5Sz

123456789

void process_data(const std::vector<double> &values) {  const auto in_range = [](const double d) {    return d >= 5.0 && d <= 100.0;  };  const bool all_in_range = all_of(begin(values), end(values), in_range);  if (all_in_range) {    process_more(values);  }} https://godbolt.org/z/SHzs6h

18.4

Page 98: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

What Kept Coming Up?What Kept Coming Up?

19

Page 99: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

constconst

20.1

Page 100: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

It’s not like this is the first time It’s not like this is the first time constconst has hasbeen mentioned at a conferencebeen mentioned at a conference

20.2

Page 101: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

0:00 / 1:18

20.3

Page 102: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

constconst

Any lack of const is a code smellconst forces us into more organized codeconst prevents common errorsconst encourages more use of algorithms

20.4

Page 103: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?12345678

#include <cstdio> void hello_world(int count){  for (int i = 0; i < count; ++count) {    puts("Hello World");  }} https://godbolt.org/z/xQ6MfK

20.5

Page 104: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?

This is a real error I’ve made before.

12345678

#include <cstdio> void hello_world(int count){  for (int i = 0; i < count; ++count) { ///    puts("Hello World");  }} https://godbolt.org/z/WB6XyB

20.6

Page 105: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?12345678

#include <cstdio> void hello_world(const int count) /// fails to compile{  for (int i = 0; i < count; ++count) {    puts("Hello World");  }} https://godbolt.org/z/y65vs4

20.7

Page 106: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?12345678

#include <string> struct S {  S(const std::string s) : data{std::move(s)}  {}   std::string data;} https://godbolt.org/z/u-h7Y6

20.8

Page 107: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?12345678

#include <string> struct S {  S(const std::string s) : data{std::move(s)} /// prevented move  {}   std::string data;} https://godbolt.org/z/X9BVTZ

20.9

Page 108: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Parameters? Value Parameters?12345678

#include <string> struct S {  S(std::string s) : data{std::move(s)} /// better  {}   std::string data;} https://godbolt.org/z/ck6rhw

20.10

Page 109: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?123456789

#include <string> void consume_string(std::string); int main(){  const std::string str = "Hello World";  consume_string(std::move(str));} https://godbolt.org/z/P-cn5P

20.11

Page 110: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?123456789

#include <string> void consume_string(std::string); int main(){  const std::string str = "Hello World";  consume_string(std::move(str)); /// silently reverts to copy} https://godbolt.org/z/f2S-JJ

20.12

Page 111: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?

How do we resolve this problem?

123456789

#include <string> void consume_string(std::string); int main(){  std::string str = "Hello World"; /// :( not const  consume_string(std::move(str));} https://godbolt.org/z/nZZYub

20.13

Page 112: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?

How do we resolve this problem?

12345678

#include <string> void consume_string(std::string); int main(){  consume_string("Hello World");} https://godbolt.org/z/NgwJ0a

20.14

Page 113: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?12345678

#include <string> void consume_string(std::string); int main(){  consume_string("Hello World"); /// avoid temporary} https://godbolt.org/z/Gb5atn

20.15

Page 114: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Temporary Values? Temporary Values?123456789

#include <string> void consume_string(std::string);std::string get_string(); int main(){  consume_string(get_string()); /// write a function!} https://godbolt.org/z/OvSh39

20.16

Page 115: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?1234567

#include <string> std::string get_string(){  const std::string value{"Hello World"};  return value; /// is this OK?} https://godbolt.org/z/6nr0bT

20.17

Page 116: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?123456789

10111213

#include <string> std::string get_string(const bool hello){  const std::string value{"Hello"};  const std::string value2{"World"};   if (hello) {    return value; /// is this OK?  } else {    return value2;  }} https://godbolt.org/z/9F1l2o

20.18

Page 117: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?

NRVO likely does not apply here.

123456789

10111213

#include <string> std::string get_string(const bool hello){  const std::string value{"Hello"};  const std::string value2{"World"};   if (hello) {    return value; /// is this OK?  } else {    return value2;  }} https://godbolt.org/z/9F1l2o

20.18

Page 118: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?

Without NRVO, this is implicitly a move . How do we resolve this problem?

123456789

10111213

#include <string> std::string get_string(const bool hello){  const std::string value{"Hello"};  const std::string value2{"World"};   if (hello) {    return std::move(value); /// equiv  } else {    return std::move(value2); /// equiv  }} https://godbolt.org/z/164dsc

20.19

Page 119: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?123456789

101112

#include <string> std::string get_string(const bool hello){  if (hello) {    const std::string value{"Hello"};    return value;  } else {    const std::string value2{"World"};    return value2;  }} https://godbolt.org/z/MFDs_M

20.20

Page 120: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Returned Objects? Returned Objects?

Avoiding the temporary so we don’t have to worry about it gives us theoptimal solution.

123456789

10

#include <string> std::string get_string(const bool hello){  if (hello) {    return "Hello";  } else {    return "World";  }} https://godbolt.org/z/JvqoUu

20.21

Page 121: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

std::movestd::move With Returned Values With Returned ValuesI found many examples like this in LLVM while preparing for this talk:

123456

auto do_things() {  std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));  Ret->parseSymbols();  Ret->parseMetadata();  return std::move(Ret);} https://godbolt.org/z/mKl4fo

20.22

Page 122: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

std::movestd::move With Returned Values With Returned ValuesI found many examples like this in LLVM while preparing for this talk:

This does generate a warning…

123456

auto do_things() {  std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));  Ret->parseSymbols();  Ret->parseMetadata();  return std::move(Ret);} https://godbolt.org/z/mKl4fo

20.22

Page 123: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

std::movestd::move With Returned Values With Returned ValuesI found many examples like this in LLVM while preparing for this talk:

This does generate a warning…

“Redundant move in return statement”

123456

auto do_things() {  std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));  Ret->parseSymbols();  Ret->parseMetadata();  return std::move(Ret);} https://godbolt.org/z/mKl4fo

20.22

Page 124: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Value ReturnTypes?Types?

21.1

Page 125: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?12345

#include <string> const std::string get_value() {  return "Hello There World!";} https://godbolt.org/z/rE_vYp

21.2

Page 126: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?

Do you want to forbid this?

123456789

10

#include <string> std::string get_value() {  return "Hello There World!";} int main(){  get_value() += "Weird...";} https://godbolt.org/z/Si8VcC

21.3

Page 127: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?123456789

10

#include <string> const std::string get_value() { ///  return "Hello There World!";} int main(){  get_value() += "Weird..."; // fails to compile...} https://godbolt.org/z/Asn9Qm

21.4

Page 128: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?123456789

10

#include <string> int get_value() { ///  return 5;} int main(){  get_value() += 10; /// not allowed on built in types} https://godbolt.org/z/EKKsYB

21.5

Page 129: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?123456789

1011

#include <string> std::string get_value() {  return "Hello There World";} int main(){  std::string s;  s = get_value(); /// move-assignment} https://godbolt.org/z/5i3_li

21.6

Page 130: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?123456789

101112

#include <string> const std::string get_value() { ///  return "Hello There World";} int main(){  std::string s;  s = get_value(); /// copy-assignment                   /// (you cannot move from `const`)} https://godbolt.org/z/xaTgFb

21.7

Page 131: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?You don’t want to const value return types, it breaks move operationsOf course we could likely have rewritten this code so the issue didn’teven come up…

21.8

Page 132: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Do You Do You constconst Value Return Types? Value Return Types?You don’t want to const value return types, it breaks move operationsOf course we could likely have rewritten this code so the issue didn’teven come up…123456789

1011

#include <string> const std::string get_value() { /// (but still not a good idea)  return "Hello There World";} int main(){  // not copy or move!  const auto s = get_value();} https://godbolt.org/z/5Wl9K3

21.8

Page 133: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

We Find 3 Smells:We Find 3 Smells:

22.1

Page 134: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

22.2

Page 135: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

cppcheck can help you reduce variable scope

22.2

Page 136: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

cppcheck can help you reduce variable scope"variable can be const from various tools

22.2

Page 137: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

cppcheck can help you reduce variable scope"variable can be const from various toolsC++ Core Guidelines checks reduce raw pointer / memory usage

22.2

Page 138: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

cppcheck can help you reduce variable scope"variable can be const from various toolsC++ Core Guidelines checks reduce raw pointer / memory usage“Pessimizing move ” warnings

22.2

Page 139: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

1. Missing and Ignored Compiler Warnings1. Missing and Ignored Compiler WarningsSpecial checks for many of these things.

cppcheck can help you reduce variable scope"variable can be const from various toolsC++ Core Guidelines checks reduce raw pointer / memory usage“Pessimizing move ” warningsconst return values in clang-tidy

22.2

Page 140: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

2. Missing 2. Missing constconst and and constexprconstexpr , Misplaced, Misplaced constconst

Why isn’t that value or member function const ?If it’s known at compile time it should be constexpr or an enum .

This forces us into more efficient and more organized code, utilizing:

std::array

<algorithm>

<numeric>

I’m not a AAA fan, but it does push us in the same direction as const .

“east const ? west const ? I don’t care, just use const !”

22.3

Page 141: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

3. Weak Types And Casts3. Weak Types And CastsUnfortunately the C++ standard library does not help us here:

string , filesystem::path , const char * , and string_view have manyconversions with optional , variant , and shared_ptr contributing to theissues with non- explicit constructors.

Use stronger typingSee #1 and #2, they catch someFor the rest

read the codeuse autoUse the correct types to avoid castingavoid named temporaries to avoid std::move

22.4

Page 142: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

OthersOthersThere are other things, of course, but most can be distilled down intothese 3.Sometimes the correct answer to “why isn’t that const ?” is “becauseit’s not the right time to use const ”

22.5

Page 143: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocppCopyright Jason Turner @le�icus emptycrate.com/idocpp

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Bonus Code ReviewBonus Code Review123456789

10111213141516171819

#include <vector>#include <limits> int range(std::vector<int> &values){  int min = std::numeric_limits<int>::max();  int max = std::numeric_limits<int>::min();   for (int i = 0; i < values.size(); ++i) {    if (values[i] < min) {      min = values[i];    }    if (values[i] > max) {      max = values[i];    }  }   return max - min;} https://godbolt.org/z/WbJtyO

22.6

Page 144: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Bonus Code ReviewBonus Code Review

22.7

Page 145: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Bonus Code ReviewBonus Code Review123456789

10

#include <algorithm> template<typename Itr>auto range(const Itr begin, const Itr end){  const auto [min_elem, max_elem] =    std::minmax_element(begin, end);   return *max_elem - *min_elem;} https://godbolt.org/z/BmUPbf

22.8

Page 146: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Jason TurnerJason TurnerCo-host of CppCast https://cppcast.comHost of C++ Weekly https://www.youtube.com/c/JasonTurner-le�icusProjects

https://chaiscript.comhttps://cppbestpractices.comhttps://github.com/le�icus/cpp_boxhttps://coloradoplusplus.info

Microso� MVP for C++ 2015-present

22.9

Page 147: C++ Code Smells...Code Smell: Is it possible t o swap these around and look f or “smells” instead? In computer programming, a code smell is any char acteristic in the source code

Copyright Jason Turner @le�icus emptycrate.com/idocpp

Jason TurnerJason TurnerIndependent and available for training or contracting

https://articles.emptycrate.com/idocpp

22.10