git
commit. The commit message will describe the purpose of the code change1 #include <iostream> | |
2 #include <iomanip> | |
3 #include <vector> | |
4 | |
5 using namespace std; | |
6 | |
7 int main() { | |
8 ····vector<float> rainfall; | |
9 ····float t, m, n; | |
10 ····while (cin >> n) { | |
11 ········rainfall.push_back(n); | |
12 ····} | |
13 ····if (!rainfall.size()) { | |
14 ········cout << "Error: no rainfall data"; | |
15 ········return 1; | |
16 ····} | |
17 ····t = rainfall[0]; | |
18 ····m = rainfall[0]; | |
19 ····for (vector<float>::size_type i = 1; i < rainfall.size(); ++i) { | |
20 ········t += rainfall[i]; | |
21 ········if (rainfall[i] > m) | |
22 ············m = rainfall[i]; | |
23 ····} | |
24 ····cout << "| Hourly Rainfall | Inches in 100s |" << '\n'; | |
25 ····cout << "|:----------------|----------------|" << '\n'; | |
26 ····cout << "| Average ········| ····" << left << setw(10) << fixed << setprecision(2) << (t / rainfall.size()) << " |" << '\n'; | |
27 ····cout << "| Heaviest ·······| ····" << left << setw(10) << m << " |" << '\n'; | |
28 } | |
29 |
Every program file needs a header comment
main()
) or what the file is for/** | |
* @file CPUCount.hpp | |
* | |
* @copyright Copyright (C) 2014-2019 srcML, LLC. (www.srcML.org) | |
* | |
* This file is part of the srcml command-line client. | |
* | |
* The srcML Toolkit is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* The srcML Toolkit is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with the srcml command-line client; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ |
// SPDX-License-Identifier: GPL-3.0-only | |
/** | |
* @file CPUCount.hpp | |
* | |
* @copyright Copyright (C) 2021 srcML, LLC. (www.srcML.org) | |
* | |
* This file is part of the srcml command-line client. | |
*/ |
Include files should be at the top of the file before any other statements
return
In main()
main()
requires areturn
for all code paths
main()
has a return type of int
return
leads to an undefined return value when the program runs0
indicates that the program was successfulusing namespace std;
main()
functionWrite error and log messages to standard error
std::cout
is for the standard output streamstd::cerr
is for the standard error streamC++ Stream | Standard Stream | Standard File Descriptors | Buffered |
---|---|---|---|
std::cin | standard input | stdin | Yes |
std::cout | standard output | stdout | Yes |
std::cerr | standard error | stderr | No |
std::clog | standard error | stderr | Yes |
End final output of the program with a newline
Separate each section of statements with a single, blank line
Every section of code needs a comment describing the purpose
Declare only one variable in a declaration statement
int
Prefer
int
for the integer variable type
int
is the type of a literal integer, e.g., 123
int
as the standard C++ integer number type. Only use long
, e.g., 123l
, unsigned int
, e.g., 123u
, and unsigned long long
, e.g., 123ull
, etc., if specifically needed.size_t
and std::size_type
double
Prefer
double
for the floating-point variable type
double
is the type of a literal floating-point value, e.g., 1.23
double
is the type for all of the standard math functions, e.g., log()
double
as the standard C++ floating-point number type. Only use float
if specifically needed, e.g., SSE instructionsInitialize local variables where they are declared
All variable names should be descriptive of what they store
auto
KeywordUse
auto
when initializing a declaration that includes a non-literal expression
auto
when initializing with a literal value to make certain you get the intended type
int total = 0;
double x = 0;
Put the calculations of each result in a separate section
// calculate the average and heaviest rainfall | |
// auto total = rainfall[0]; | |
auto heaviest = rainfall[0]; | |
for (std::vector<double>::size_type i = 1; i < | |
// total += rainfall[i]; | |
if (rainfall[i] > heaviest) | |
heaviest = rainfall[i]; | |
} | |
// output the rainfall report | |
std::cout << "| Hourly Rainfall | Inches in 100s |" << | |
std::cout << "|:----------------|----------------|" << | |
// std::cout << "| Average | " << std::left | |
std::cout << "| Heaviest | " << std::left |
// calculate the heaviest rainfall | |
auto heaviest = rainfall[0]; | |
for (std::vector<double>::size_type i = 1; i < | |
if (rainfall[i] > heaviest) | |
heaviest = rainfall[i]; | |
} | |
// // calculate the average rainfall | |
// auto total = rainfall[0]; | |
// for (std::vector<double>::size_type i = 1; i < | |
// total += rainfall[i]; | |
// } | |
// output the rainfall report | |
std::cout << "| Hourly Rainfall | Inches in 100s |" << | |
std::cout << "|:----------------|----------------|" << | |
// std::cout << "| Average | " << std::left | |
std::cout << "| Heaviest | " << std::left |
// calculate the average and heaviest rainfall | |
auto total = rainfall[0]; | |
// auto heaviest = rainfall[0]; | |
for (std::vector<double>::size_type i = 1; i < | |
total += rainfall[i]; | |
// if (rainfall[i] > heaviest) | |
// heaviest = rainfall[i]; | |
} | |
// output the rainfall report | |
std::cout << "| Hourly Rainfall | Inches in 100s |" << | |
std::cout << "|:----------------|----------------|" << | |
std::cout << "| Average | " << std::left | |
// std::cout << "| Heaviest | " << std::left |
// // calculate the heaviest rainfall | |
// auto heaviest = rainfall[0]; | |
// for (std::vector<double>::size_type i = 1; i < | |
// if (rainfall[i] > heaviest) | |
// heaviest = rainfall[i]; | |
// } | |
// calculate the average rainfall | |
auto total = rainfall[0]; | |
for (std::vector<double>::size_type i = 1; i < | |
total += rainfall[i]; | |
} | |
// output the rainfall report | |
std::cout << "| Hourly Rainfall | Inches in 100s |" << | |
std::cout << "|:----------------|----------------|" << | |
std::cout << "| Average | " << std::left | |
// std::cout << "| Heaviest | " << std::left |
Use only simple expressions in output statements
Prefer range-based
for
over indexing
The for
statement with indexing can lead to an infinite loop
Range-based for is safer and does not lead to infinite loops with standard containers
Extension of using iterators
for
Statementint total = 0; | |
for (int i = 0; i < v.size(); i++) { | |
total += v[i]; | |
} | |
int total = 0; | |
for (int i = 0; i < v.size(); ++i) { | |
total += v[i]; | |
} | |
for (INIT; CONDITION; INCR) { | |
STATEMENT; | |
} |
int total = 0; | |
{ | |
int i = 0; | |
while (i < v.size()) { | |
total += v[i]; | |
i++; | |
} | |
} | |
int total = 0; | |
{ | |
int i = 0; | |
while (i < v.size()) { | |
total += v[i]; | |
++i; | |
} | |
} | |
// EXCEPTION: continue statement | |
{ | |
INIT; | |
while (CONDITION) { | |
STATEMENT; | |
INCR; | |
} | |
} |
const
VariablesUse the
const
specifier on a type whenever possible
Prefer
std::istream_iterator
over an input loop
const
Create new functions for sections of code with loops and multiple calculations
// output the rainfall report
because that code belongs to main()
/* | |
Functions.hpp | |
Include file for ... | |
*/ | |
#ifndef INCLUDED_FUNCTIONS_HPP | |
#define INCLUDED_FUNCTIONS_HPP | |
// Insert: Include files necessary for compiling | |
// function declarations | |
// Insert: Function declarations | |
#endif |
'.'
with an underscore '_'
INCLUDED_
(see [Lakos])Filename | Include Guard Name | ||||||||
---|---|---|---|---|---|---|---|---|---|
XMLReader.hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
||||||||
xmlReader.hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
||||||||
xml_reader.hpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
// Sum of two integers | |
int add(int n1, int n2); | |
// Multiply two floating-point numbers | |
double multiply(double factor1, double factor2); | |
// Divide the numerator by the denominator | |
double divide(double numerator, double denominator); | |
// Determine if a character is a vowel | |
bool isVowel(char c); | |
// Reverse a string inplace | |
void reverse(std::string& s); | |
// Find the maximum value | |
int max(const std::vector<int>& numbers); | |
// Update the value at a pointer if the pointer is not null | |
void updateValue(int* valuePtr, int newValue); | |
// Add a value to each element | |
void addToElements(std::vector<int>& vec, int valueToAdd); | |
// Concatenate two strings | |
std::string concatenate(std::string_view s1, std::string_view s2); | |
// Determine if two strings are equal | |
bool equal(std::string_view s1, std::string_view s2); | |
// Print a message to the console a specific number of times | |
void printMessageNTimes(std::string_view message, int nTimes); | |
// Area of a rectangle | |
double rectangleArea(double length, double width); | |
// Determine if a number is even | |
bool isEven(int number); | |
// Swap the values of two integers | |
void swap(int& a, int& b); |
When performing a standard operation on a container, look for existing solutions
If a function is not doing very much, consider replacing the call with the calculation
inline
specifier, which often has no effect at all