| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
This document provides guidelines for designing and implementing basic tool classes in the ABACUS codebase, focusing on best practices for memory management, code style, and testing. These guidelines apply to all basic mathematical and utility classes, including but not limited to:
While this guide uses IntArray as an example for illustration purposes, the principles and practices described here are applicable to all basic tool classes in ABACUS.
Always use try-catch blocks when allocating memory to handle std::bad_alloc exceptions gracefully:
When reallocating memory (e.g., in create methods), use a two-stage approach to ensure that the original object remains valid if memory allocation fails.
Always check for null pointers before accessing memory, especially in methods that might be called on objects with failed memory allocation.
Implement a copy constructor to avoid shallow copy issues.
Implement move constructor and move assignment operator to improve performance.
Add boundary checks to prevent out-of-bounds access.
Use separate lines for braces, and always use braces for "if" and "for" statements, even if they contain one line of code
Use spaces instead of tabs for indentation (4 spaces per indent level).
Use English for comments and document important functionality. Follow Doxygen-style documentation for classes and methods.
Avoid using magic numbers. Instead, define named constants for numerical values:
Ensure all necessary header files are included, especially for functions like assert:
#include <cassert>Write comprehensive unit tests for all classes, including:
Use constructor initialization lists for test classes to improve compatibility:
class IntArrayTest : public testing::Test
{
protected:
ModuleBase::IntArray a2, a3, a4, a5, a6;
int aa;
int bb;
int count0;
int count1;
const int zero;
IntArrayTest() : aa(11), bb(1), zero(0)
{
}
};While this guide uses IntArray as an example, these principles apply to all basic tool classes in ABACUS. For example:
By following these guidelines, you can ensure that all basic tool classes in ABACUS are well-designed, robust, and maintainable.
| Back | FazBrowse Home | New Git URL |