HappyLittlePony's blog

By HappyLittlePony, history, 9 years ago, In English

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

  • Vote: I like it
  • +13
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it +7 Vote: I do not like it

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 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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

  • »
    »
    9 years ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    That's not what your submissions say

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +4 Vote: I do not like it

      I know sir,just lightening the mood :) Why so serious :) :)

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        there should always be some darkness

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          lightening not lightning :) :)

          • »
            »
            »
            »
            »
            »
            9 years ago, # ^ |
            Rev. 2   Vote: I like it -12 Vote: I do not like it

            lightning = (thunder comes with it)
            lightening = (making it weight lower) (making something lighter)
            am i not right?

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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 years ago, # |
  Vote: I like it +7 Vote: I do not like it

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 years ago, # |
Rev. 2   Vote: I like it +22 Vote: I do not like it

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 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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.