Is there a limit or restriction on variable name sizes?

Asked

Viewed 1,017 times

5

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

However, I have recently researched and seen that this concept has changed. And today in many tutorials the examples come with full names. Even if it 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:https://www.javascriptmasters.com.br/blog/teoria/clean-code-3-a-importancia-dos-nomes/

  • Maybe I’ll fall here: It is correct to prefix variable names with their type?. In the past, names were abbreviated by memory restriction.

  • This is exactly where I was going. I wanted to find out if there are any restrictions on name sizes due to some memory limit. This link you passed answers some things. But it doesn’t come to that point.

3 answers

4


There should probably be a restriction on the size of the variable names in javascript, I can’t tell you what the maximum allowed, but it’s probably a big number, which you shouldn’t 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 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) by Robert Martin. It will help you a lot in this and many other aspects!

  • 3

    I had not thought to do this. I created a variable following your example, but a little larger with 151800 characters :o! and it ran perfectly. Well now it is clear that from a distance I should not worry about it. Thanks! ;)

  • 2

    Oops, for nothing! Glad I could help! "a little bigger" hahahahahaha

3

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

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

  • Also, if you’re not in a good IDE/Text Editor, copy and paste, or at worst type manually, it gets tiresome/boring and even unviable for maintenance.

  • Yes. An example of this is programming an app with Android Studio. The method names are huge. I would never decorate them all. But by typing the first letter the IDE already suggests several options that start with it and filter as you type. But I still prefer to use bigger names than those abbreviations that only you created know what you mean later.

1

When you develop it yourself, you can create abbreviation patterns that for you will be easy 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 in the first letter of the variable what it represents, for example, if it is a parameter, use pNomeDaVariable, if it is a local variable use lnomeDaVariable and so on.

  • I have started using the full names. But I am still afraid and would like to know if there is any limit to sizes in the documentation.

  • I did not test, but I already asked this question to my java teacher, and according to him, it is the limit of a string, 255 characters.

  • 1

    Following @paulequilibrio’s example. I ran a test with a string of 151800 and ran perfectly! :)

  • 2

    His reply reminded me of this: http://desciclopedia.org/wiki/Gambi_Design_Patterns#Nmes_de_vars_abrv_cm_pouc_vgs

  • @Alan So far only one time I have needed a name greater than the language allowed me, in a language called "Formula", which you probably never have to work with (I hope). Write your code as clearly as possible and don’t worry about that sort of thing. The size of the script is, yes, a valid concern, but to solve this, just put a "mini-finisher" at the end of your construction process.

  • 1

    I see no advantage in these prefixes to identify a parameter and identify a local variable. In fact, disgust of 95% of Hungarian ratings, in addition to soiling the domain, in case of exchange of types it impairs refactoring. The only Hungarian notation I use is to identify regular expression (reXpto).

  • blz @Pablo Almeida! Thanks! ;)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.