Skip to content

Commit 348e76a

Browse files
authored
Update CONTRIBUTING.md (#59)
- Nim v2 is out - Add result return style guide
1 parent 9c7501c commit 348e76a

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

CONTRIBUTING.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We are very happy that you are considering working on algorithm and data structu
1414
- [Readability and naming conventions](#readability-and-naming-conventions)
1515
- [Compilation](#compilation)
1616
- [Types](#types)
17+
- [Result return](#result-return)
1718
- [Exceptions and side-effects](#exceptions-and-side-effects)
1819
- [Documentation, examples and tests](#documentation-examples-and-tests)
1920
- [Other](#other)
@@ -40,28 +41,28 @@ Being one of our contributors, you agree and confirm that:
4041

4142
We appreciate any contribution, from fixing a grammar mistake in a comment to implementing complex algorithms. Please check the [directory](DIRECTORY.md) and [issues](https://github.com/TheAlgorithms/Nim/issues/) for an existing (or declined) implementation of your algorithm and relevant discussions.
4243

43-
**New implementations** are welcome! This includes: new solutions for a problem, different representations for a data structure, algorithm design with a different complexity or features.
44+
**New implementations** are welcome! This includes new solutions for a problem, different representations for a data structure, and algorithm design with different complexity or features.
4445

4546
**Improving documentation and comments** and **adding tests** is also highly welcome.
4647

4748
**Identical implementations** are not allowed.
4849

4950
### Implementation requirements
5051

51-
- The unit of an implementation we expect is a [**Nim module**](https://nim-lang.org/docs/manual.html#modules). Although the main goals of this repository are educational, the module form mirrors a real world scenario and makes it easy to use the code from this repository in other projects.
52-
- First line must contain the canonical title of the module prefixed by double hashes (`## Title Of The Module`). This title is used in this repository's automation for populating the [Directory](DIRECTORY.md).
52+
- The unit of an implementation we expect is a [**Nim module**](https://nim-lang.org/docs/manual.html#modules). Although the main goals of this repository are educational, the module form mirrors a real-world scenario and makes it easy to use the code from this repository in other projects.
53+
- The first line must contain the canonical title of the module prefixed by double hashes (`## Title Of The Module`). This title is used in this repository's automation for populating the [Directory](DIRECTORY.md).
5354
- The module should be thoroughly documented with doc comments. Follow the [Nim documentation style](https://nim-lang.org/docs/docstyle.html).
54-
- The file begins with the module-level documentation with the general description and explanation of the algorithm/data-structure. If possible, please include:
55+
- The file begins with the module-level documentation with the general description and explanation of the algorithm/data structure. If possible, please include:
5556
* Any restrictions of the implementation and any constraints for the input data.
56-
* An overview of the use-cases.
57+
* An overview of use cases.
5758
* Recommendations for when to use or avoid using it.
5859
* Comparison with the alternatives.
5960
* Links to source materials and further reading.
6061
- Use intuitive and descriptive names for objects, functions, and variables.
6162
- Return all calculation results instead of printing or plotting them.
6263
- This repository is not simply a compilation of *how-to* examples for existing Nim packages and routines. Each algorithm implementation should add unique value. It is fine to leverage the standard library or third-party packages as long as it doesn't substitute writing the algorithm itself. In other words, you don't need to reimplement a basic hash table ([`std/tables`](https://nim-lang.org/docs/tables.html)) each time you need to use it, unless it is the goal of the module.
63-
- Avoid importing third-party libraries. Only use those for complicated algorithms and only if the alternatives of relying on the standard library or including a short amount of the appropriately-licensed external code are not feasible.
64-
- The module has to include tests that check valid and erroneous input values and the appropriate edge-cases. See [Documentation, examples and tests](#documentation-examples-and-tests) below.
64+
- Avoid importing third-party libraries. Only use those for complicated algorithms and only if the alternatives of relying on the standard library or including a short amount of the appropriately licensed external code are not feasible.
65+
- Include tests that cover valid and erroneous input values and the appropriate edge-cases. See [Documentation, examples and tests](#documentation-examples-and-tests) below.
6566

6667
### Nim Coding Style
6768

@@ -70,39 +71,44 @@ We appreciate any contribution, from fixing a grammar mistake in a comment to im
7071
We want your work to be readable by others; therefore, we encourage you to follow the official [Nim Coding Style](https://nim-lang.org/docs/nep1.html).
7172

7273
- Help your readers by using **descriptive names** that eliminate the need for redundant comments.
73-
- Follow Nim conventions for naming: camelCase for variables and functions, PascalCase for types and objects, PascalCase or UPPERCASE for constants.
74-
- Avoid single-letter variable names, unless it has a minimal lifespan. If your variable comes from a mathematical context or no confusion is possible with another variable, you may use single-letter variables. Generally, single-letter variables stop being OK if there's more than just a couple of them in a scope. Some examples:
74+
- Follow Nim naming conventions: camelCase for variables and functions, PascalCase for types and objects, PascalCase or UPPERCASE for constants.
75+
- Avoid single-letter variable names, unless their lifespan is minimal. If your variable comes from a mathematical context or no confusion is possible with another variable, you may use single-letter variables. Generally, single-letter variables stop being OK if there's more than just a couple of them in a scope. Some examples:
7576
* Prefer `index` or `idx` to `i` for loops.
7677
* Prefer `src` and `dst` to `a` and `b`.
7778
* Prefer `remainder` to `r` and `prefix` to `p`.
78-
- Expand acronyms. Prefer `greatestCommonDivisor()` to `gcd()`, as the former is easier to understand than the latter, especially for non-native English speakers.
79+
- Expand acronyms. For instance, use `greatestCommonDivisor()` rather than `gcd()` for better clarity, especially for non-native English speakers.
7980

8081
#### Compilation
8182

82-
- Compile using the stable version of the Nim compiler. The 2.0 version is not out yet, but it is a good idea to compile against the development version of the compiler to check your implementation is reasonably future-proof.
83+
- The code should successfully compile using the stable version of the Nim compiler. It's a good idea to check compilation against the development version as well for future-proofing.
8384

8485
#### Types
8586

86-
- Use the strictest types possible for the input, output and object fields. Prefer `Natural`, `Positive` or custom [subrange types](https://nim-lang.org/docs/manual.html#types-subrange-types) to unconstrained `int` where applicable, use `Natural` for indexing.
87+
- Use the strictest suitable types for input, output and object fields. Prefer `Natural`, `Positive` or custom [subrange types](https://nim-lang.org/docs/manual.html#types-subrange-types) over unconstrained `int` where applicable, use `Natural` for indexing.
8788
- On the other hand, write generic code where appropriate. Do not impose arbitrary limitations if the code can work on a wider range of data types.
8889
- Don't use unsigned numerical types (`uint` and its sized variants), unless wrapping behaviour or binary manipulation is required for the algorithm.
8990
- Prefer the [`Option[T]`](https://nim-lang.org/docs/options.html) to encode an [optional value](https://en.wikipedia.org/wiki/Option_type) instead of using an invalid value (like the `-1` or an empty string `""`), unless it is critical for the algorithm. It may be also fitting if you are looking for the equivalent of "NULL" (default value for pointers)[^null].
9091

92+
#### Result return
93+
94+
- Prefer the expression-based return over using the implicitly declared `result` variable[^result].
95+
- Use `return` keyword only for changing the control-flow of a function. Minimize such cases.
96+
9197
#### Exceptions and side-effects
9298

9399
- Raise Nim exceptions (`ValueError`, etc.) on erroneous input values.
94-
- Use [exception tracking](https://nim-lang.org/docs/manual.html#effect-system-exception-tracking). Right after the module-level documentation, add a `{.push raises: [].}` module pragma. This enforces that all `func`s don't raise any exceptions. If they do raise at least one, list them all with the `raises` pragma after the return type and before the `=` sign like this: `func foo(bar: int) {.raises: [IOError].} =`.
100+
- Use [exception tracking](https://nim-lang.org/docs/manual.html#effect-system-exception-tracking). Right after the module-level documentation, add a `{.push raises: [].}` module pragma. This enforces that all `func`s do not raise any exceptions. If they do raise at least one, list them all with the `raises` pragma after the return type and before the `=` sign like this: `func foo(bar: int) {.raises: [IOError].} =`.
95101

96102
#### Documentation, examples and tests
97103

98-
- It is incited to give a usage example after the module documentation and the `push raises` pragma in the top-level `runnableExamples` block.
99-
- Use the [`std/unittest` module](https://nim-lang.org/docs/unittest.html) to test your program.
104+
- Consider including a usage example after the module documentation and the `push raises` pragma in the top-level `runnableExamples` block.
105+
- Use the [`std/unittest` module](https://nim-lang.org/docs/unittest.html) to test your code.
100106
- We recommend using a `when isMainModule:` block to run tests. This block runs when the module is compiled as an executable. See the [minimal example](#minimal-example).
101107

102108
#### Other
103109

104-
- If you need a third-party module that is not in the file [third_party_modules.md](https://github.com/TheAlgorithms/Nim/blob/master/third_party_modules.md), please add it to that file as part of your submission.
105-
- Use the export marker `*` to distinguish the functions of your user-facing [application programming interface (API)](https://en.wikipedia.org/wiki/API) from the internal helper functions.
110+
- If you need a third-party module not listed in [third_party_modules.md](https://github.com/TheAlgorithms/Nim/blob/master/third_party_modules.md), please add it to that file as part of your submission.
111+
- Use the export marker `*` to distinguish the functions of your user-facing [application programming interface (API)](https://en.wikipedia.org/wiki/API) from internal helper functions.
106112

107113
### Minimal example
108114

@@ -134,14 +140,14 @@ when isMainModule:
134140

135141
### Submissions Requirements
136142

137-
- Make sure the code compiles before submitting.
138-
- Look up the name of your algorithm in other active repositories of [TheAlgorithms](https://github.com/TheAlgorithms/), like [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python). By reusing the same name, your implementation will be appropriately grouped alongside other implementations on the [project's web site](https://the-algorithms.com/).
139-
- Please help us keep our issue list small by adding fixes: Add the number of the issue you solved — even if only partially — to the commit message of your pull request.
143+
- Make sure the code [compiles](#compilation) before submitting.
144+
- Look up the name of your algorithm in other active repositories of [TheAlgorithms](https://github.com/TheAlgorithms/), like [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python). By reusing the same name, your implementation will be appropriately grouped alongside other implementations on the [project's website](https://the-algorithms.com/).
145+
- Please help us keep our issue list small by adding fixes: Reference the number of the issue you solved — even if only partially — ino the commit message of your pull request.
140146
- Use *snake_case* (words separated with an underscore `_`) for the filename.
141-
- Try to fit your work into the existing directory structure as much as possible. Please open an issue first if you want to create a new subdirectory.
142-
- Writing documentation, be concise and check your spelling and grammar.
143-
- Add a corresponding explanation to [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation) (optional but recommended).
144-
- Most importantly, **be consistent in the use of these guidelines**.
147+
- Try to fit your work into the existing directory structure as much as possible. If you want to create a new subdirectory, please open an issue first.
148+
- Writing documentation, be concise and verify your spelling and grammar.
149+
- Optionally but recommended, provide an explanation in [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation).
150+
- Most importantly, **be consistent in adhering to these guidelines**.
145151

146152
**Happy coding!**
147153

@@ -150,3 +156,4 @@ when isMainModule:
150156
Authors: [@dlesnoff](https://github.com/dlesnoff), [@Zoom](https://github.com/ZoomRmc).
151157

152158
[^null]: If you are wondering why it's preferable to avoid Null references, you should check [Tony Hoare's report at the QCon 2009 conference](https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/).
159+
[^result]: Refer to comparison of different ways of returning results in [The Status Nim style guide](https://status-im.github.io/nim-style-guide/language.result.html).

0 commit comments

Comments
 (0)