Answer the question
In order to leave comments, you need to log in
How to name variables?
Hello, how do pros usually name variables?
For some reason, when it comes time to come up with a name for a variable, a stupor begins.
Let's say you need to get likes from a vkontakte post
such a normal name:
get_like_from_vk_post
Almost all the code in such long names, is this absolutely normal?
1) or too long?
2) And if so, how would you call such a function?
3) Does the length of the variable affect the speed? Significantly or you can come up with as many long names as you like
Answer the question
In order to leave comments, you need to log in
Specifically in this case, I would just call: likes_in_vkpost().
Let me explain:
1.
If a function is used on the right side and assigns a value to a variable on the left side, then what is it if not a getter function? Why once again sharpen the reader of the code "No dude, I'm getting 100 pounds of likes from the post." He just needs to say "Like from the post". If he doesn't believe, he will go to the function and read its code or look into the code of unit tests.
If an http request is sent in the get-function code, then an http response is obtained. Then something counts. This means a violation of the "one action one function" principle.
* Send a request to one function
* Receive a response to another
* Parse another one
* But to get what has already been received from the network and stored in an object or some storage in memory, this is the get-function. Therefore, it is made read-only so that it does not destroy the object and then it is obtained as a "property of the object" and for this reason it makes no sense to use 'get' in the function name
2. It makes sense to use the verb in the function only when you want to EXPLICITLY say which needs to be looked at very carefully.
2.1. You have a complex computational process for counting likes and this code can be "stupid", i.e. long work then you need to say compute_likes(). The reader of the code will think with the verbs: compute, analyze, etc
2.2. Updating values in an object can also be dangerous, and for this reason, you need to add 'set' . If the dude's object collapses, then he will ask the question: "And who changes the object?" and then it will look for verbs: set, update, push, pop, etc
3. Use English Grammar
Let's take the sentences "Is this string empty?", "Is this data encrypted?". How would you say this in English?
'Is string Empty?', 'Is data encrypted'?. Then in the code we will read as:
if ( str1.empty() )
if ( data1.encrypted() )
If you use the scheme:
if ( str1.is_empty() )
if ( data1.is_encrypted() )
Then you violate the grammar of English. The verb 'to be' in interrogative sentences is placed BEFORE the subject! Take the C++ standard library documentation for example, and read the description of std::string.
Disclaimer: This does not apply when you write in a procedural style. Example:
empty(driver_data_request)
it's hard to understand what the author meant and therefore 'is_empty' makes perfect sense!
The length of the name according to the scope.
If the function is small, then the length does not need to be large.
Context must be taken into account. If it is clear that the social network is a contact, then there is no need to write it in the name.
If it is clear that the likes are only for the post, then there is no need to write a post.
i.e. getLikes or getLikesCount
is probably better
.
But everything is decided by the context and the specific case.
It can be so. You come up with a lot of names. Then you realize that the names have something in common. For example, the word likes. And you dance from this word. i.e. number of likes, give number of likes and so on
1. Normal, it even happens, the longer the better.
2. getLikeFromVkPost - exclusively from experience, the name of the variable camel case. With underscore - values of constants
3. For JS, it only affects if the size of the .js file has a weight.
a function to name a getVkPostLike()
variable is better to namevk_post_likes
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question