Abseil C++ Code Style Guidelines: Complete Reference for Contributors
Abseil C++ strictly follows the Google C++ Style Guide, requiring all contributions to use 2-space indentation, 80-character line limits, PascalCase for types, CamelCase for functions, and specific error handling via absl::Status and absl::StatusOr<T>.
The abseil/abseil-cpp repository maintains rigorous coding standards to ensure consistency across its open-source C++ libraries. All contributions must adhere to these Abseil C++ code style guidelines, which are formally defined by the Google C++ Style Guide and enforced through automated tooling and code review.
Abseil C++ Coding Standards Overview
According to the CONTRIBUTING.md file in the repository root, Abseil "uses a fairly rigid coding style, as defined by the [google-styleguide] project." All patches are expected to conform to the conventions outlined in the Google C++ Style Guide.
The repository includes a .clang-format file that encodes many of these formatting rules, allowing developers to run git clang-format to automatically align code with the style before submitting.
Key Style Conventions
Naming Conventions
Abseil enforces strict naming rules derived from the Google standard:
- Types:
PascalCase(e.g.,class MyClass,struct DataPoint) - Functions and methods:
CamelCase(e.g.,GetSize(),CalculateTotal()) - Variables:
snake_case(e.g.,buffer_size,temp_value) - Constants:
kprefix withPascalCase(e.g.,kMaxSize,kDefaultTimeout) - Private member variables:
snake_casewith trailing underscore (e.g.,size_,buffer_)
Formatting Rules
The .clang-format configuration enforces these mechanical requirements:
- 2-space indentation (no tabs)
- 80-character line length limit
- Spaces after commas
- No trailing whitespace
Header Guards and Include Structure
Abseil prefers #pragma once over traditional include guards, though both are acceptable. Headers should include only the minimal necessary dependencies. Real-world examples like absl/strings/str_format.h demonstrate this minimal include policy.
Error Handling and Memory Management
Code must use RAII patterns and smart pointers (std::unique_ptr, std::shared_ptr) rather than raw new and delete. For error propagation, Abseil requires absl::Status and absl::StatusOr<T> instead of exceptions or error codes.
Abseil-Specific Attributes
As shown in absl/base/attributes.h, Abseil uses specific attribute macros like ABSL_MUST_USE_RESULT that follow the style guide conventions. These should be applied to classes and functions where discarding the return value indicates a bug.
Testing Requirements
All new code must be covered by unit tests in files ending with *_test.cc. These use Abseil's testing framework with TEST() macros and EXPECT_* assertions.
Practical Code Examples
Header File Structure
#pragma once
#include <cstddef>
#include "absl/base/attributes.h"
namespace absl {
class ABSL_MUST_USE_RESULT Example {
public:
Example() = default;
~Example() = default;
// Returns the size of the internal buffer.
std::size_t GetSize() const;
private:
std::size_t size_ = 0;
};
} // namespace absl
This demonstrates PascalCase for the class Example, CamelCase for GetSize(), trailing underscore for size_, and proper use of ABSL_MUST_USE_RESULT from absl/base/attributes.h.
Implementation File
#include "absl/example.h"
namespace absl {
std::size_t Example::GetSize() const { return size_; }
} // namespace absl
The implementation maintains the same naming conventions with compact formatting suitable for 80-character limits.
Unit Test File
#include "absl/example.h"
#include "absl/status/statusor.h"
#include "absl/testing/testing.h"
TEST(ExampleTest, SizeIsZeroOnConstruction) {
absl::Example ex;
EXPECT_EQ(ex.GetSize(), 0);
}
Test files use the *_test.cc suffix and include the Abseil testing headers.
Summary
- Abseil C++ code style guidelines require strict adherence to the Google C++ Style Guide as documented in
CONTRIBUTING.md. - Formatting: 2-space indentation, 80-character line limits, and no trailing whitespace, enforced by the
.clang-formatfile. - Naming: Use
PascalCasefor types,CamelCasefor functions,snake_casefor variables,kprefix for constants, and trailing underscores for private members. - Error handling: Prefer
absl::Statusandabsl::StatusOr<T>over exceptions. - Memory: Use smart pointers and RAII; avoid raw memory operations.
- Testing: All code requires unit tests in
*_test.ccfiles using the Abseil testing framework.
Frequently Asked Questions
Does Abseil follow the Google C++ Style Guide?
Yes. According to the CONTRIBUTING.md file in the abseil/abseil-cpp repository, Abseil explicitly uses the Google C++ Style Guide. All contributions must conform to the standards defined at https://google.github.io/styleguide/cppguide.html.
What is the maximum line length for Abseil C++ code?
Abseil enforces a strict 80-character line length limit. This rule is encoded in the repository's .clang-format file and applies to all source files, ensuring consistent readability across different development environments.
How should I format my code before submitting to Abseil?
Run git clang-format before submitting your pull request. The repository includes a .clang-format configuration file that automatically handles 2-space indentation, line wrapping, and spacing rules to match the project requirements.
What naming convention does Abseil use for constants?
Constants must use a k prefix followed by PascalCase formatting, such as kMaxSize or kBufferLength. This convention is consistent across the codebase, as seen in headers like absl/strings/str_format.h.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →