|
| 1 | +# List of things to check before submitting the code for review |
| 2 | + |
| 3 | +This document presents the common mistakes often seen during the review and |
| 4 | +also includes some general guidelines in code writing and testing. |
| 5 | + |
| 6 | +Please make sure as a PR (Pull-Request) submitter |
| 7 | +(or as a first-stage code reviewer) these are eliminated wherever possible. |
| 8 | + |
| 9 | +## General |
| 10 | + |
| 11 | +- Use PR / commit descriptions where possible to indicate what has |
| 12 | + changed, it helps your peer reviewer to understand basic differences |
| 13 | + among 20+ commits |
| 14 | + |
| 15 | +- Don't change the status of the associated Jira ticket(s to implemented unless merged and |
| 16 | + **agreed with your reviewer** |
| 17 | + |
| 18 | +- Include ticket number in the Pull-Request message (the PR template will |
| 19 | + have an entry for you to fill in) |
| 20 | + |
| 21 | +## Builds |
| 22 | + |
| 23 | +- Don't submit a branch for a review unless it passes all pre-submit CI |
| 24 | + jobs |
| 25 | + |
| 26 | +- Check that your code didn't add any new compiler warnings |
| 27 | + |
| 28 | +## Library code |
| 29 | + |
| 30 | +- Don't reinvent the *wheel*, reuse existing infrastructure |
| 31 | + |
| 32 | + Chance that you need something slightly different which tempts |
| 33 | + you to write a very similar function to an existing one is |
| 34 | + likely an indicator that you should think twice. |
| 35 | + |
| 36 | +- **Do NOT copy/paste code**. There might be rare occasions where it is |
| 37 | + useful but in general it denotes bad code design (particularly for |
| 38 | + bigger code blocks). Consider using a function or a template |
| 39 | + function with parameters. |
| 40 | + |
| 41 | +- Calls to functions (e.g., internal ones) check for their |
| 42 | + return `status` which is propagated down the call stack in transparent |
| 43 | + manner (use as appropiate `return da_error(...);` or `return status;`) |
| 44 | + |
| 45 | +- Make sure any memory allocation (`new`, `std::vector().resize()`, ...) is |
| 46 | + `try`-`catch`ed for errors, in case of an error, the leftover memory is |
| 47 | + deallocated |
| 48 | + |
| 49 | +- For internal workspace, typically use `std::vector` rather than type |
| 50 | + `*array = new ...` to eliminate clean-up on error |
| 51 | + |
| 52 | +- Use self-explaining (i.e., non-ambiguous) variable names, |
| 53 | + particularly for booleans |
| 54 | + |
| 55 | + `bool isLower` has clear meaning, while `bool LUflag` has not |
| 56 | + |
| 57 | +- Use variable names matching their common interpretation and ideally |
| 58 | + only in that context |
| 59 | + |
| 60 | + e.g., `i` ~ row index, `j` ~ column index |
| 61 | + |
| 62 | +- Used `const` keyword on pointers and references not being changed by the |
| 63 | + function (including the internal functions) but don't use them for |
| 64 | + scalars passed-by-value. |
| 65 | + |
| 66 | + Use `const` also when casting constant pointers (otherwise you might wipe |
| 67 | + this protection off). |
| 68 | + |
| 69 | +- Document your code, particularly pay extra attention to |
| 70 | + |
| 71 | + - New data structures, enums, etc. |
| 72 | + |
| 73 | + - Internal functions with many parameters: is it obvious how all |
| 74 | + parameters are connected? Will it ever get reused? |
| 75 | + |
| 76 | +- Test all aspects of your input early in your user-facing API |
| 77 | + |
| 78 | + - Never dereference a pointer without checking earlier that it is |
| 79 | + not `nullptr` (or `NULL`) |
| 80 | + |
| 81 | +- Be aware that `std::vector` of size 0 has member `.data() = nullptr`, that might |
| 82 | + cause issues on empty matrices, thus always test your code for |
| 83 | + $A \in R^{m\times{}n}$ with $0<\{m,n\} \And \text{nnz}(A)=0$. |
| 84 | + |
| 85 | +## Documentation |
| 86 | + |
| 87 | +- Build the documentation and read it from there if the formatting is |
| 88 | + correct |
| 89 | + |
| 90 | + - Is the API listed in `.rst` files that the documentation appears |
| 91 | + at all? |
| 92 | + |
| 93 | +- Imagine that you read the documentation without prior knowledge of |
| 94 | + the API, would you be able to use it? Write examples? Understand |
| 95 | + what needs to be on input and how to process output? |
| 96 | + |
| 97 | +- Are all errors the API can return listed? |
| 98 | + |
| 99 | +- Follow the common patterns how to refer to things, examples: |
| 100 | + |
| 101 | + - If your API has an example, include it in the documentation via *collapsed |
| 102 | + file loading* |
| 103 | + |
| 104 | +- Typography: don't place **spaces** before commas, semicolons, |
| 105 | + colons etc. (e.g., not *~~word1 , word2 : word3~~* but *word1, |
| 106 | + word2: word3*) |
| 107 | + |
| 108 | +## Example |
| 109 | + |
| 110 | +- Check returned status of all public APIs called |
| 111 | + |
| 112 | +- Include a basic check if the API works and return non-zero code if |
| 113 | + it didn't, this helps to use the example as a test in the CTest framework |
| 114 | + |
| 115 | +## Testing |
| 116 | + |
| 117 | +- Good practice is to run only your new unit tests and check the |
| 118 | + coverage. Do you see what you expected? Ideally coverage LOC percentage would |
| 119 | + not decrease |
| 120 | + |
| 121 | +- When creating wrong input cases, make sure that exactly one thing is |
| 122 | + wrong (e.g., if matrix is supposed to be symmetric and of matching |
| 123 | + dimensions, there should be multiple test cases such as the |
| 124 | + dimension is right but it is not symmetric, it is symmetric but the |
| 125 | + dimension is wrong) |
| 126 | + |
| 127 | + - Also make it just slightly wrong, not by miles (e.g., if we |
| 128 | + expect $n=m$, choosing 5 & 6 might be more likely to fail than 5 |
| 129 | + & 234891) |
| 130 | + |
| 131 | +- Choose such input that it is likely to detect error, examples what |
| 132 | + not to do: |
| 133 | + |
| 134 | + - square matrix $m=n$ so making a mistake by swapping $n$ & $m$ is not |
| 135 | + discovered |
| 136 | + |
| 137 | + - input vector or matrix of all ones doesn't test that the correct indices are |
| 138 | + accessed |
| 139 | + |
| 140 | + - Complex numbers with same real and imaginary parts |
| 141 | + |
| 142 | +- Test on edge case input as well as typical |
| 143 | + |
| 144 | + - e.g., 0-sizes, 0-nnz, 1 row matrix, ... |
| 145 | + |
| 146 | +- Set correct tolerances for the expected results, use tolerance relative to machine epsilon |
| 147 | + |
| 148 | +- If using `if constexpr()` make sure that there is `else` branch (even if |
| 149 | + for just reporting error that no code should go there), it is easy to make |
| 150 | + mistakes in these, that code section is never visited but it is |
| 151 | + assumed all is fine and tested |
| 152 | + |
| 153 | +- If a function (which can fail) is called in multiple places within |
| 154 | + the same unit test, add Gtest's `SCOPED_TRACE()` to distinguish which call |
| 155 | + failed |
| 156 | + |
| 157 | +- Use standardized facilities to check if your results match with the |
| 158 | + reference, **don't write comparison functions that already exist** |
| 159 | + |
| 160 | +- If using radnomly generated data, initialize the random seed to allow for |
| 161 | + reproducibility |
| 162 | + |
| 163 | +- In positive quick exit tests, check the validity of the output |
| 164 | + (e.g., that the matrix generated which is supposed to be empty is |
| 165 | + indeed valid, of the right dimension and empty) |
| 166 | + |
| 167 | +- It might not be necessary to test a complete combination of all |
| 168 | + parameters with the same input, although sometimes it might be desirable. It |
| 169 | + might make more sense to vary the input for different types to get |
| 170 | + better coverage with smaller number of tests. |
0 commit comments