+ 1
Variable and Function names
What is the good practice to use case for variables and functions? I prefer camel case. Also want to know the best practices of naming convention. Sometimes it takes me hours to give reasonable names and sometimes it will be very long name like myTabHeadingClickable lol. Please share your thoughts. Much appreciated, Zaid
1 Respuesta
+ 2
The first tip I can give you is don't start your variable names with "my". tabHeadingClickable is just as descriptive but two letters shorter. Adding "my" to the start of a variable-name doesn't add any information and just increases length. That's a general rule: don't add anything to the variable name unless it conveys information to the reader.
Try not to stress out about casing. I believe that it is standard javascript procedure to use camelCase for functions/variables, but I personally like to use underscore_case because I find it easier to read (I come from a C++ background). But as long as you're are consistent throughout your code-base, it doesn't really matter which style you use; just use whatever you find most comfortable/readable.
With regards to the actual names of the variables, the best variables are short and descriptive. Of course, that isn't always a possibility and you will have to choose between a descriptive variable name (which is very long) or a short variable name (which is not as descriptive). When making that decision, that decision should really be made on a case-by-case basis. Here's a few things you should consider to help make the decision:
1) Scope
If the variable will be used only within a small scope (ex: a short utility function, a simple for-loop, a brief if-statement), then it's probably OK to give it a shorter name, since it will probably be obvious what the variable is doing within such a limited context. However, if the variable is used in a very large function or has a global scope, then you should probably use a longer name, since it will be less immediately obvious to a reader what the variable does.
2) Project Size
On a small project or a prototype it's totally OK to use short names. But on a larger project, you might be working on the code base over many days, and have forgotten what a variable does. In that situation it is more useful to have a more descriptive name.
3) Abbreviations
There are some good abbreviations but generally avoid them.