Is there a limit or restriction on variable name sizes?

5

When I started programming it was said to be a good practice to abbreviate variable names to shorten their size.

However, I have been researching and seeing recently that this concept has changed. And today in many tutorials the examples come with full names. Even though this makes the variable size larger.

I would like to know if there is any problem or restriction in naming a variable with a big name like:

var pistasEmTextoSeparadasPorVirgula
// "Monaco,Singapore,Interlagos"
// "string"

Example taken from: link

    
asked by anonymous 22.08.2016 / 15:28

3 answers

4

Probably there should be a restriction on the size of variable names in javascript , I can not tell you what the maximum value allowed, but it should probably be a large number, which you should not worry about. You can test the example below (with 200 characters) in your browser right now and see that it works:

var fghfhjflkhflkhflshflhflhsfkjshadjfkhasjfhasljfhjsakhfjsadkhfkjsdhjfkhasdkjfhuiqewyruqweyriuyweqoriuhsafuihasdkjhfasjkdhfjashfjlhaslkjfhalskjdfhalsfjshdakjfhajksdlhfjkahsdlkfhlasfhasdjklfhalsjdkfhalkdq = 10;

console.log(fghfhjflkhflkhflshflhflhsfkjshadjfkhasjfhasljfhjsakhfjsadkhfkjsdhjfkhasdkjfhuiqewyruqweyriuyweqoriuhsafuihasdkjhfasjkdhfjashfjlhaslkjfhalskjdfhalsfjshdakjfhajksdlhfjkahsdlkfhlasfhasdjklfhalsjdkfhalkdq);

Regarding how to name the variables, the most important thing is that the name can describe well what the variable represents. I suggest reading the book Clean Code (Clean Code, in Portuguese) by Robert Martin. It will help you a lot in this and in many other aspects!

    
22.08.2016 / 16:02
3

The specification does not determine a limit , then the final decision is in charge of each implementation of the language. Of course there have to be limits, because computers are limited in terms of memory etc., but for practical purposes you may consider unlimited.

This does not mean that it is recommended to create very large variable names. But the limit is a little subjective, the idea is that the name be as short as possible that can communicate the function of the variable in the context of your code.

    
09.09.2017 / 00:22
1

When you develop yourself, you can create abbreviation patterns that will be easy for you to decipher in the future. In the case of teamwork, where other people will need to understand your code, it is much better to use them in full, and you can still use the first letter of the variable what it represents, for example if it is a parameter, use variableVariable, if it is a local variable use lnameVariable, and so on.

    
22.08.2016 / 15:43