The Code Style of Yours Truly
A Few General Rules
- Braces go on a new line (at least in C-family languages; in JavaScript, whatever, but I've never seen anyone use JavaScript in competitions)
- Use the variable names from the problem statement whenever possible.
- Temporary variables (loop variables, say) can be named with a single letter or a two-letter abbreviation.
- Global variables and fields with a long lifecycle should be in lowerCamelCase whenever possible.
- Functions and function variables are named in lowerCamelCase.
- Constructors that assign values and initialization functions get done in one line (for example initializing an edge, or initializing a disjoint-set union)
- Use #define less, use const more.
- Inside loops or conditional statements, squash lines together with commas (not too long).
- Use a modern editor for automatic formatting (strongly recommend VSCode, say goodbye to the F**K debugging method).
Sample code:
// snippet.cpp
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX_N = 100;
int n, seqa[MAX_N], seqb[MAX_N], ans;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &seqa[i]), scanf("%d", &seqb[i]);
for (int i = 1; i <= n; i++)
ans += seqa[i] - seqb[i];
printf("%d", ans);
return 0;
}
Variable Name Table
| Meaning | Variable name |
|---|---|
| Answer | ans |
| Stack | st |
| Queue | q |
| Maximum | MAX_??? |
| Minimum | MIN_??? |
| Array | arr[] |
| Multiple arrays or sequences | seq?[] |
| Source data/start point | src |
| Target data/end point | dst |
| Distance | dist |
菜死了
大括号永不换行!