Skip to content

Google Style Reference

This page outlines the correct styling practices for scenarios encountered in the course, based on the Google C++ Style Guide. Each section provides clear examples to ensure your code adheres to the required standards, helping you write clean, readable, and maintainable programs. Use this as a guide to meet the course’s styling expectations.

Access Specifier Indent Detected

Section titled “Access Specifier Indent ”

Access specifiers like public, protected, and private should be indented by 1-space. You should NOT have a blank line after an access specifier.

class MyClass {
public:
void Print() const;
private:
int data;
};

Reference to cpplint error

Class Blank Space Detected

Section titled “Class Blank Space ”

There should NOT be a blank line after the class name line or before the closing brace line.

class MyClass {
public:
MyClass(); // Not blank
~MyClass();
int GetData() const { return data; }
private:
int data;
void HelperFunction(); // Not blank
};

Reference to cpplint error

Class Data Member Not Detected

Section titled “Class Data Member ”

The name of the data member should ALWAYS end with an underscore (_).

int data_;
string name_;

Error not detected by cpplint

Class Name Not Detected

Section titled “Class Name ”

The name of your class should be capitalized, and should follow CamelCase rules.

class MyCamelClass {
// Members
};

Error not detected by cpplint

Your MAX column length is 80.

// 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;
}

Reference to cpplint error

If you use a comment in a line that contains code, you need to have at least two spaces between code and comment.

int counter = 0; // Counter for something...

Reference to cpplint error

Constant Not Detected

Section titled “Constant ”

Constants should be formatted with a lowercase k followed by CamelCasing the rest of the name.

const double kPie = 3.14;

Error not detected by cpplint

Control Structures Detected

Section titled “Control Structures ”
  • Blank space between the statement type and the parenthesis.

  • No blank spaces after the opening parenthesis and before the closing parenthesis.

  • Single space between the parenthesis and the opening brace.

  • Opening brace should be on the same line as the statement.

  • No blank lines between the braces and the code block.

do {
// Body of the loop
} while (condition);
for (initialization; condition; update) {
// Body of the loop
}
// Single Line OR Multi Line w/o omitting braces
if (true) {
// Do something
}
// Single Line w/ omitting braces
if (true) // Do something
// OR
if (true)
// Do something
switch (integer_expression) {
case value1:
// Case actions
break;
case value2:
// Case actions
break;
// ...
default:
}
// Braces REQUIRED
while (condition) {
// Body of the loop
}

Reference to cpplint errors

The first line of each file should be authored with a comment in the following format // Copyright <year> <name>.

// Copyright 2024 CSCE240

Reference to cpplint error

Function Mostly Detected

Section titled “Function ”
  • The name of the function should start with a capitalized letter and casing should follow CamelCase rules. This is not caught by cpplint.
  • Parenthesis should be next to the function’s name.

  • There should NOT be a space after the opening parenthesis, or before the ending parenthesis.

  • There should be a single space between the opening parenthesis, and the opening brace.

  • Opening braces should ALWAYS be in the same line as the associated statement. If the function implementation is only one small line, you can place the closing brace on the same line.

  • There should NOT be an empty blank line after the function declaration or before the closing brace of the function.

int GetData() const { return data_; }

Reference to cpplint error

Header Guards Falsely Detected

Section titled “Header Guards ”

You can disregard the error thrown as cpplint asks for a complete file path after #ifndef and after #endif. Complete filepaths should NOT be used in this course.

#ifndef FILE_NAME_
#define FILE_NAME_
// Prototypes
#endif

Reference to cpplint error

Include File Falsely Detected

Section titled “Include File ”

You can disregard the error thrown as cpplint asks for a complete file path after #include instead of the relative one that is used. Complete filepaths should NOT be used in this course.

#include "filename.file_extension"

Reference to cpplint error

Indentation Not Detected

Section titled “Indentation ”

2-space indent length per indentation level.

// Copyright 2024 CSCE240
#include <iostream>
using std::cout;
using std::endl;
int main() {
bool correct_indentation = true;
bool every_line = true;
if (correct_indentation) {
if (every_line) {
cout << "No points deducted!" << endl;
} else {
cout << "You almost had it!" << endl;
}
} else {
cout << "Sorry your indentation is incorrect!" << endl;
}
return 0;
}

Error not detected by cpplint

Your recipe commands should be indented with a tab not spaces. Copying and pasting code snippets may produce this error as some code editors don’t recognize a copied tab.

target: Prerequisite(s)
recipe command(s)
# As many as you need
target: Prerequisite(s)
recipe command(s)

Reference to cpplint error

Namespace Not Detected

Section titled “Namespace ”

You should use lowercase letters when naming a namespace and should follow snake_case notation. The ending brace should contain a comment stating namespace and the name of the namespace.

namespace csce240_program {
// Class, etc.
} // namespace csce240_program

Error not detected by cpplint

Newline Character Detected

Section titled “Newline Character ”

Every file should end with a blank line.

Reference to cpplint error

Variable Not Detected

Section titled “Variable ”

Variables should be named using snake_case.

int my_integer = 10;
double my_double = 10.0;

Error not detected by cpplint