Style Requirements
In CSCE146, you may have followed a simple formatting requirement, like right-clicking in the Eclipse IDE and selecting “Format” to tidy up your code before submission. However, CSCE240 introduces stricter style guidelines, requiring attention to details such as maximum column lengths, case sensitivity in naming, and more.
C++ Google Style Guide
Section titled “C++ Google Style Guide”This course follows the C++ Google Style Guide. As we cover each topic, you’ll be introduced to specific styling conventions relevant to that section. A Style Reference has been written to quickly find any style requirements you may have forgotten.
Cpplint Style Checker
Section titled “Cpplint Style Checker”You’ll be using a tool called cpplint to check your code for styling errors. While cpplint will catch most issues,
there are certain cases where it might not flag a styling error, even if one exists. A Cpplint Reference is available,
listing common issues, solutions, and other FAQs related to cpplint. The version of cpplint you download will depend on
your operating system. To check your files, use cpplint <filename> where <filename> is replaced by the name of the file and the
extension (for this course .h and .cc).
Coding Snippets
Section titled “Coding Snippets”The previous sections discussed some tedious micro-management, but don’t worry—coding snippets provided on this site are 😁 pre-styled and
already pass cpplint!
Code Editor
Section titled “Code Editor”Ready for some 🫣 great news? Editors like Visual Studio Code come with auto-formatting options that follow the 🫡 Google Style Guide! While it’s not perfect and won’t catch every style requirement, it will take care of most of the heavy lifting, such as indentation and column length.
Basic Style Requirements
Section titled “Basic Style Requirements”To get you started, here are a couple of style rules that you will always deal with:
Column Length
Section titled “Column Length”Do NOT go past column 80. Though the example looks awkward, it is styled correctly!
// Copyright 2024 CSCE240#include <iostream>
int main() { std::cout << "a very very long print statement that seems to never end and " "goes past the editor screen" << std::endl; return 0;}cpplint driver.ccLines should be <= 80 characters long [whitespace/line_length] [2]Commenting
Section titled “Commenting”If you are commenting in a line that contains code, you need to use at least two spaces between code and comment:
int counter = 0; // Keep track of ...cpplint driver.ccAt least two spaces is best between code and comments [whitespace/comments] [2]Copyright
Section titled “Copyright”Just like in CSCE145 and CSCE146, you are required to author each page through a comment at the top of your file. The syntax is Copyright <year> <name>. In coding snippets, CSCE240
will be used as the name.
// Copyright 2024 CSCE240int main() { return 0; }cpplint driver.ccNo copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5]Indentation
Section titled “Indentation”In the Google C++ Style Guide, 2-space indentation is a strict requirement per indentation level. If you like to use the Tab key
for coding, you may be able to set your Tab Length to 2. The Driver shows various levels of indentation done correctly.
// Copyright 2024 CSCE240#include <iostream>using std::cout;
int main() { bool quit = true; if (quit) { cout << "Goodbye!"; } return 0;}Main Function
Section titled “Main Function”You may have noticed from the code snippets that the opening brace of the main function is always placed on the same line as the associated statement. This is because it is a required style. If the statement is short enough, as seen in previous code snippets, the ending braces can also be in the same line. Braces placement will be expanded further in the guide.
A couple of extra requirements is that parenthesis () should be next to the function’s name, and you should have a single space between
the parenthesis and the opening bracket.
// Copyright 2024 CSCE240int main() { int num = 7; return 0;}cpplint driver.ccExtra space before ( in function call [whitespace/parens] [4]Missing space before { [whitespace/braces] [5]Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]{ should almost always be at the end of the previous line [whitespace/braces] [4]Newline Character
Section titled “Newline Character”Every document should end with a blank line at the end. Pretty simple.
cpplint driver.ccCould not find a newline character at the end of the file. [whitespace/ending_newline] [5]