Skip to content

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.

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.

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).

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!

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.

To get you started, here are a couple of style rules that you will always deal with:

Do NOT go past column 80. Though the example looks awkward, it is styled correctly!

driver.cc
// 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;
}

If you are commenting in a line that contains code, you need to use at least two spaces between code and comment:

driver.cc
int counter = 0; // Keep track of ...

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.

driver.cc
// Copyright 2024 CSCE240
int main() { return 0; }

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.

driver.cc
// Copyright 2024 CSCE240
#include <iostream>
using std::cout;
int main() {
bool quit = true;
if (quit) {
cout << "Goodbye!";
}
return 0;
}

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.

driver.cc
// Copyright 2024 CSCE240
int main() {
int num = 7;
return 0;
}

Every document should end with a blank line at the end. Pretty simple.

Terminal Error
cpplint driver.cc
Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]