Блог пользователя HappyLittlePony

Автор HappyLittlePony, история, 9 лет назад, По-английски

What is the best style of naming variables in competetive programming and why?. I use long names for variables but i think it is making my code longer and harder to read.

And also it takes often really long time to make a good variable name :D

  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

In programming contests, I try to use something short but descriptive, like str instead of s for a String. It pays off when you have a lot of variables since you can spend a lot of time reading your own code or even get the code wrong when the variables have no meaning at all.

When you are not in a hurry (not in a short programming contest) it's always better to use very descriptive variables and methods, e.g., addUndirectedEdge(int from, int to, int weight).

»
9 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

I generally use small and descriptive variable names like Czechoslovakia,Presbyterian,bureaucratic__ etc

»
9 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

I prefer to avoid simiar variable names. In a problem I denoted a 2-D array with int ar[100][100]; and I needed a variable to store area int area; at the end I had to print area so I just wrote cout<<ar<<endl; I was surprised by what was printed on the screen (address of the array I think), reading the source 3-4 times revealed the silly mistake.

»
9 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

I use very short variable names, usually only one character, but I always use the same variables, so I don't get confused. For example...

  • Number of elements/nodes is N
  • Number of edges is M
  • Number of queries is Q
  • Adjacency list is E
  • Array/matrix to store DP values is DP[] or DP[][]
  • In Segment Trees, the tree array is called T[]$ and the one used for lazy propagation is L[], the middle element is mid, the current range is [a, b], the sought range is [l, r] and the value to update is v
  • Etc.

Besides, local variables use small letters, global variables have only the first letter in capital and constants/defines use all capital letters.

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится +22 Проголосовать: не нравится

Always "speaking" variables names. Usually except for one-two sign names, with their default meanings:
a -- array, number
b -- balance, second array, second number
c -- char, count, capacity...
d -- dp, distance, double, second char
e -- edge, epsilon
f -- flag, float, function, first
g -- graph, next function
h -- height
i -- no comments :)
j -- no comments :)
k -- no comments :) + count of elements, more often number-boundary
l -- left, low...
m -- count of edges, count of second array and something else, map, median, matrix
n -- count of anything
o -- not used
p -- prime, prev, point
q -- query, queue, variable like placeholder, second point
r -- right, radius
s -- string, sum, set
t -- time, test, tree
u -- vertex from
v -- vertex to, vector, value
w -- width, weight, third vertex
x -- number, coordinate
y -- x
z -- y
And function names (except maybe f, g, but usually they are in problem statement and dfs0 dfs1 ... dfsN) areAlwaysTellingWhatTheyDoInJavaStyle()

»
9 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

I'm using Java Style usually if code is quite big and that's impossible to use such names as x, y, s, N, M and so on. It means fullDescriptionOfWhatFunctionDoes (type firstDescriptionOfFirstArgument, type fullDescriptionOfSecondArgument...) and some other things. It's quite comfortable for me cuz I'm typing with all 10 fingers and VS usually autocompletes it.