Olympiad in Informatics

The Code Style of Yours Truly

A Few General Rules

  1. 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)
  2. Use the variable names from the problem statement whenever possible.
  3. Temporary variables (loop variables, say) can be named with a single letter or a two-letter abbreviation.
  4. Global variables and fields with a long lifecycle should be in lowerCamelCase whenever possible.
  5. Functions and function variables are named in lowerCamelCase.
  6. Constructors that assign values and initialization functions get done in one line (for example initializing an edge, or initializing a disjoint-set union)
  7. Use #define less, use const more.
  8. Inside loops or conditional statements, squash lines together with commas (not too long).
  9. 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

MeaningVariable name
Answerans
Stackst
Queueq
MaximumMAX_???
MinimumMIN_???
Arrayarr[]
Multiple arrays or sequencesseq?[]
Source data/start pointsrc
Target data/end pointdst
Distancedist

Comments 2

  1. lornd

    大括号永不换行!