Comments (11)
Preferring auto only makes sense in generic code and override makes things unreadable and hard to reason about as you no longer have a type system. Contacts will hopefully fix this allowing the use of a contract instead.
E.g. see random
for (auto foo : foos) {
foo->do_stuff();
do_stuff_with_foos();
}
Such a snippet will not tell you if foo is a pointer, an iterator, a pass by value with operator->, when it is invalidated and when it is safe to access foos and in what ways.
The best you can do here is to add a few ENSURE checks. Maybe.
Being explicit, on the other hand, will help. So better keep auto and decltype in generic code for now and replace with concepts when they become available.
Too reduce the load with std::vector::iterator and the like alias declarations (using) are available and proper IDE support is possible.
from cppcoreguidelines.
Another: Rule of Minimum State Access
"Minimize access to state at all points in the program."
This entails minimizing state itself and also access to it where present, by means of const views. This simple rule has good motivation from the FP experience and captures a ton of tactical rules:
Con.1: By default, make objects immutable
Con.2: By default, make member functions const
Con.3: By default, pass pointers and references to consts
Con.4: Use const to define objects with values that do not change after construction
I.2 Avoid global variables
F.20: Use a const T& parameter for a large object
C.6: Declare a member function that does not modify the state of its object const
R.6: Avoid non-const global variables
ES.21: Don't introduce a variable (or constant) before you need to use it (I'd formulate this as: "make variables as local as possible")
ES.22: Don't declare a variable until you have a value to initialize it with
ES.25: Declare an object const or constexpr unless you want to modify its value later on
(Unclassified) Use const'ness wherever possible: member functions, variables and (yippee) const_iterators
There's a mildly controversial one also caught by this:
- Prefer unnamed temporaries (rvalues) to variables
from cppcoreguidelines.
Rule of Superset of Subset:
Already defined and described by Bjarne. A few items already fit under this rule.
from cppcoreguidelines.
Rule of Earliest Checking
"Checking should occur as early as possible"
- Compile time better than link time
- Link time better than unittest time
- Unittest time better than contract time
- Contract time better than run time
from cppcoreguidelines.
Good example for Rule of Minimum Power: goto is the most powerful looping construct (it can do everything while does, and a lot more such as jumping in the middle of a loop etc.) and the most unrecommended.
from cppcoreguidelines.
Rule of Minimal Rigidity:
"Code should minimize lines touched per unit of meaningful change"
("Rigidity" here corresponds to the definition of Robert Martin)
- Using != over < in generic code falls under this rule (but also under RoMP).
- Coding for the weakest iterator also falls under this rule (but also under RoMP).
- The most important stand-alone incarnation: prefer auto to explicitly typed variables
** minimizes commitment to detail - A number of more specialized tricks in template code are aimed at flexibility
This could be called Rule of Maximal Flexibility but "flexibility" is more vague and overloaded than "rigidity".
from cppcoreguidelines.
Rule of Ownership:
"All resources must be owned"
Owner means:
- A scope (for automatic, static, and namespace-level variables)
- An object, implicitly (direct field)
- An object, explicitly (unique_ptr, shared_ptr, vector, map, ...)
- A control flow (e.g. manual calls to malloc/free or fopen/fclose) - reduce to absolute minimum and use scope guards to help.
from cppcoreguidelines.
For the "Rule of Minimum Rigidity" favoring containment vs. inheritance is not mentioned. If inheriting an interface is not required for another reason encapsulating it with containment means as the type being contained changes your class will not. This minimizes code turn.
from cppcoreguidelines.
Added "immutability" under Philosophy.
from cppcoreguidelines.
Herb will triage these and will discuss with editors and then create a PR. Many of these may fit into existing rules.
from cppcoreguidelines.
Herb, please write this up. Also review R.1. Make note of "A control flow (e.g. manual calls to malloc/free or fopen/fclose) - reduce to absolute minimum and use scope guards to help."
from cppcoreguidelines.
Related Issues (20)
- Remove enforcement rules of rule F.17 HOT 7
- ES.79 is a terrible advice and should be dropped HOT 14
- C.21 Exception for copy-and-swap idiom HOT 4
- Broken links HOT 3
- F.20 should exclude swap HOT 3
- SF.2 allow inline variables and static inline class member variables HOT 1
- F.16 (pass by reference to const / by value): Request for clarification HOT 6
- Poor (and nearly-incorrect) example for rule F.27 (shared_ptr) HOT 4
- Challenging C.153 "Prefer virtual function to casting" HOT 4
- ES.56 Clarification on moving when assigning to reference outside of scope HOT 3
- F.16 Allow pass-by-value for types that are copied HOT 2
- `gsl::suppress` issues collected: Strings, literals, cases HOT 2
- Hi
- New NL Rule suggestion: Place comment-only lines before the code they apply to HOT 1
- Should/can Expects and Ensures be macros?
- Check for invalidated iterator/dangling reference on heap reallocation HOT 2
- Turning off clang unsafe buffer warnings fails when compiling with a gcc based compiler HOT 2
- CodSoft Internship Task 2 /TASK 2 SIMOPLE CALCULATOR #include <iostream> using namespace std; int main() { double num1, num2; char operation; // Input cout << "Enter first number: "; cin >> num1; cout << "Enter second number: "; cin >> num2; cout << "Enter operation (+, -, *, /): "; cin >> operation; // Calculation and output switch (operation) { case '+': cout << "Result: " << num1 + num2; break; case '-': cout << "Result: " << num1 - num2; break; case '*': cout << "Result: " << num1 * num2; break; case '/': if (num2 != 0) cout << "Result: " << num1 / num2; else cout << "Error! Division by zero."; break; default: cout << "Invalid operation!"; } return 0; } HOT 2
- Cpp programs HOT 3
- C.30: Define a destructor if a class needs an explicit action at object destruction - note HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppcoreguidelines.