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
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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
Name |
---|
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).
I generally use small and descriptive variable names like Czechoslovakia,Presbyterian,bureaucratic__ etc
That's not what your submissions say
I know sir,just lightening the mood :) Why so serious :) :)
there should always be some darkness
lightening not lightning :) :)
lightning = (thunder comes with it)
lightening = (making it weight lower) (making something lighter)
am i not right?
True sir. :)
yep then what i said was completely correct
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 areaint area;
at the end I had to print area so I just wrotecout<<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.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...
Besides, local variables use small letters, global variables have only the first letter in capital and constants/defines use all capital letters.
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()
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.