CONTRIBUTING.md 8.4 KB
Newer Older
1 2 3
# CONTRIBUTION GUIDELINES

## Before contributing
4
Welcome to [TheAlgorithms/C-Plus-Plus](https://github.com/TheAlgorithms/C-Plus-Plus)! Before submitting pull requests, please make sure that you have **read the whole guidelines**. If you have any doubts about this contribution guide, please open [an issue](https://github.com/TheAlgorithms/C-Plus-Plus/issues/new/choose) and clearly state your concerns.
5 6

## Contributing
7 8 9 10 11
### Contributor
We are very happy that you consider implementing algorithms and data structures for others! This repository is referred to and used by learners from around the globe. Being one of our contributors, you agree and confirm that:
- You did your own work.
    - No plagiarism allowed.  Any plagiarized work will not be merged.
- Your work will be distributed under [MIT License](License) once your pull request has been merged.
12 13
- You submitted work fulfils or mostly fulfils our styles and standards.

14
**New implementation** New implementation are welcome!
C
Christian Clauss 已提交
15

16
**Improving comments** and **adding tests** to existing algorithms are much appreciated.
C
Christian Clauss 已提交
17

18
**Issues** Please avoid opening issues asking to be "assigned” to a particular algorithm.  This merely creates unnecessary noise for maintainers.  Instead, please submit your implementation in a pull request and it will be evaluated by project maintainers.
19 20 21 22

### Making Changes

#### Code
23
- Please use the directory structure of the repository.
24
- File extension for code should be *.h *.cpp.
25
- Don't use **bits/stdc++.h** because this is quite Linux specific and slows down the compilation process.
26 27
- Organize your code using **`struct`**, **`class`** and/or **`namespace`** keywords
- If an implementation of the algorithm already exists, please refer to the [file-name section below](#new-file-name-guidelines).
28 29 30 31
- You can suggest reasonable changes to existing algorithms.
- Strictly use snake_case (underscore_separated) in filenames.
- If you have added or modified code, please make sure the code compiles before submitting.
- Our automated testing runs [__cpplint__](https://github.com/cpplint/cpplint) on all pull requests so please be sure that your code passes before submitting.
32
- Please conform to [doxygen](https://www.doxygen.nl/manual/docblocks.html) standard and document the code as much as possible. This not only facilitates the readers but also generates the correct info on website.
33
- **Be consistent in use of these guidelines.**
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#### Documentation
- Make sure you put useful comments in your code.  Do not comment things that are obvious.
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure. If you want to create a new directory, then please check if a similar category has been recently suggested or created by other pull requests.
- If you have modified/added documentation, please ensure that your language is concise and contains no grammar errors.
- Do not update README.md along with other changes, first create an issue and then link to that issue in your pull request to suggest specific changes required to README.md
- The repository follows [Doxygen](https://www.doxygen.nl/manual/docblocks.html) standards and auto-generates the [repo website](https://thealgorithms.github.io/C-Plus-Plus). Please ensure the code is documented in this structure. Sample implementation is given below.

#### Test
- Make sure to add examples and test cases in your main() function.
- If you find any algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes.
- Please try to add one or more `test()` functions that will invoke the algorithm implementation on random test data with expected output. Use `assert()` function to confirm that the tests will pass.

#### Typical structure of a program:
```cpp
/**
 * @file 
 * @brief Add one line description here
 * @details 
 * This is a multi line
 * description containing links, references,
 * math equations, etc
 * @author [Name](https://github.com/handle)
 * @see related_file.cpp, another_file.cpp
 */

#include 

62
/**
63 64 65 66 67 68 69
 * @namespace <check from other files in this repo>
 */
namespace name {

/**
 * Class documentation
 */
70
class class_name {
71
 private:
72 73 74
    int variable;  ///< short info of this variable
    char *message;  ///< short info

75
 public:
76
    // other members also documented as below
77 78 79
}

/**
80
 * Function documentation
81 82 83 84
 * @tparam T this is a one-line info about T
 * @param param1 on-line info about param1
 * @param param2 on-line info about param2
 * @returns `true` if ...
85
 * @returns `false` if ...
86 87 88
 */
template<class T>
bool func(int param1, T param2) {
89 90
    // function statements here
    if (/*something bad*/) {
91
        return false;
92
    }
93 94 95 96 97

    return true;
}

/** Test function */
98 99 100
static void test() {
    /* desciptions of the following test */
    assert(func(...) == ...); // this ensures that the algorithm works as expected
101

102
    // can have multiple checks
103 104 105 106
}

/** Main function */
int main(int argc, char *argv[]) {
107
    test(); // execute the tests
108 109 110 111 112
    // code here
    return 0;
}
```

B
bhaumikmistry 已提交
113 114
#### New File Name guidelines
- Use lowercase words with ``"_"`` as separator
115
- For instance
B
bhaumikmistry 已提交
116
```
B
bhaumikmistry 已提交
117
MyNewCppClass.CPP       is incorrect
B
bhaumikmistry 已提交
118 119 120 121
my_new_cpp_class.cpp    is correct format
```
- It will be used to dynamically create a directory of files and implementation.
- File name validation will run on docker to ensure the validity.
122
- If an implementation of the algorithm already exists and your version is different from that implemented, please use incremental numeric digit as a suffix. For example, if `median_search.cpp` already exists in the `search` folder and you are contributing a new implementation, the filename should be `median_search2.cpp` and for a third implementation, `median_search3.cpp`.
B
bhaumikmistry 已提交
123

124
#### New Directory guidelines
B
Bhaumik Mistry 已提交
125
- We recommend adding files to existing directories as much as possible.
B
Bhaumik Mistry 已提交
126
- Use lowercase words with ``"_"`` as separator ( no spaces or ```"-"``` allowed )
B
bhaumikmistry 已提交
127 128 129 130
- For instance
```
SomeNew Fancy-Category          is incorrect
some_new_fancy_category         is correct
C
Christian Clauss 已提交
131 132 133
```
- Filepaths will be used to dynamically create a directory of our algorithms.
- Filepath validation will run on GitHub Actions to ensure compliance.
B
bhaumikmistry 已提交
134

135
#### Commit Guidelines
C
Christian Clauss 已提交
136
- It is recommended to keep your changes grouped logically within individual commits. Maintainers find it easier to understand changes that are logically spilt across multiple commits.  Try to modify just one or two files in the same directory.  Pull requests that span multiple directories are often rejected.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
```
git add file_xyz.cpp
git commit -m "your message"
```
Examples of commit messages with semantic prefixes:
```
fix: xyz algorithm bug
feat: add xyx algorithm, class xyz
test: add test for xyz algorithm
docs: add comments and explanation to xyz algorithm
```
Common prefixes:
- fix: A bug fix
- feat: A new feature
- docs: Documentation changes
152
- test: Correct existing tests or add new ones
153 154

### Pull Requests
155
- Checkout our [pull request template](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.github/pull_request_template.md)
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#### Building Locally
Before submitting a pull request, build the code locally or using the convenient [![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus) service.
```
cmake -B build -S .
```

#### Static Code Analyzer
We use [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) as a static code analyzer with a configuration in [.clang-tidy](.clang-tidy).
```
clang-tidy --fix --quiet -p build subfolder/file_to_check.cpp --
```

#### Code Formatter
[__clang-format__](https://clang.llvm.org/docs/ClangFormat.html) is used for code forrmating.
* Installation (Only needs to be installed once.)
  * Mac (using home-brew): `brew install clang-format`
  * Mac (using macports): `sudo port install clang-10 +analyzer`
  * Windows (MSYS2 64-bit): `pacman -S mingw-w64-x86_64-clang-tools-extra`
  * Linux (Debian): `sudo apt-get install clang-format-10 clang-tidy-10`
* Running (all platforms): `clang-format -i -style="file" my_file.cpp`

#### GitHub Actions
Enable GitHub Actions on your fork of the repository.
After enabling it will execute `clang-tidy` and `clang-format` after every a push (not a commit).
The result can create another commit if the actions made any changes on your behalf.
Hence, it is better to wait and check the results of GitHub Actions after every push.
Run `git pull` in your local clone if these actions made many changes in order to avoid merge conflicts.
184

C
Christian Clauss 已提交
185 186
Most importantly,
- Happy coding!