This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/1_Intro/Code_Conventions.mdx
Benjamin Qi 80cbf96234 misc
2020-07-16 18:39:07 -04:00

93 lines
No EOL
3.2 KiB
Text

---
id: code-con
title: Code Conventions
author: Nathan Wang, Benjamin Qi, Michael Cao
description: "?"
prerequisites:
- expected
---
The code we provide should follow the conventions below. Please note that you do _not_ have to copy our conventions; there is no "right" convention for coding!
Everything should compile assuming that the templates below are included. If any code does not compile or is hard to read, submit a complaint using the "Contact Us" button.
## General
<resources>
<resource source = "CF" title = "Swift - Competitive C++ Manifesto" url="blog/entry/64218"> some material directly from this blog post</resource>
</resources>
- Indenting with either tabs or 4 spaces is fine.
- Binary operators should be spaced on both sides. For example, `a+b` should be written as `a + b` and `x=a+b` should be written as `x = a + b`.
- Unary operators should only be spaced on the left side, ex. `x = -y`.
- Semicolons and commas should be spaced on the right side, ex. `f(a, b, c)`.
- Braces that open on one line should close on another. Always include braces, even if the code inside them is only one line.
- Parentheses should be spaced from the outside. For example, we would write `if (a == b) {` or `for (int i = 0; i < n; ++i) {`.
- Use `++x` rather than `x++` to increment a variable (ie. in a `for` loop).
- Start your `else` statement on the same line as the closing brace of your `if` statement, like so:
```cpp
if (a == b) {
++a;
} else {
++b;
}
```
- Use `true / false` for boolean values, not `1 / 0`.
- Explain the use of any not well known standard library functions with comments that haven't been introduced before like `__builtin_ffs()` or `tie(a, b, c)` in C++.
## Template
<LanguageSection>
<CPPSection>
See [C++ Tips & Tricks](./cpp-tips).
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
void setIO(string name = "") { // name is nonempty for USACO file I/O
ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
freopen((name+".in").c_str(), "r", stdin); // see Input & Output
freopen((name+".out").c_str(), "w", stdout);
}
// the code that we provide requires everything above this line to compile
int main() {
setIO();
}
```
<!-- Any preferences? Also, this doesn't apply to the custom sorting module, make sure to include all 3! :) -->
- Avoid the use of macros (`#define`) other than those in the template above.
- Use `const` in C++ for variables that don't change through the code.
- Use `make_pair` rather than `{}` to store a pair.
- Use `auto` to increase readability (ex. when dealing with iterators).
- Use `struct` instead of `class`. <!-- Even if you are copy pasting a `struct`, space it out rather than squeezing it into one line. -->
- Use UpperCamelCase for custom structs (`SegTree, Point`) and lowerCamelCase for functions (`solve(), binarySearch()`). Constants should be fully capitalized (`INF, MOD, SOME_CONST`).
</CPPSection>
<JavaSection />
<PySection />
</LanguageSection>