Naming Convention
A Crime in the programming language
Yes, if you named the variable as
x
or with any single alphabets. Then you did a crime
Naming variable sounds easy but it’s frustrating things to do.
Writing code is like directing a movie. Here naming is dialogues of that movie, so naming is much important than other parts of coding (don’t ask where are songs). Even if you have better at logic but poor at naming, you failed. Because poor naming will confuse your code while reading it (No one like the movie which can’t understand)
(Note cases like Pascal Case, lower camel case, and snake_case it differs according to the language you code. Here I code on swift and mentioned some cases at below for it)
Why Naming Convention is Important?
Even humans read our code(not only computer) at that time it should be understandable without your explanation and even you while referring after years.
Everyone has there own style in naming. This blog is not to disturb that, But to make it better (No no you can thank me later)
Some questions to remember while declaring a name for a variable, class or something you or your manager want
Why this variable?
Your naming should answer this question first.
Example
It has not answered anything.
variable a
not answering what that variable doing in code, while variable greeting
can do it (it's a greeting message)
What datatype it is?
It should answer whether datatype is int
or string
or what else is going to hold.
Again take the above example while looking variablea
we can't say it is int
or string
but again greeting
can, because no one going to greet you with int
okay.
And you can’t say element starts with "
and end with "
this is a string to your manager
And try to avoid mentioning datatype with the variable name
Im using SMS language?
And here you not texting with your crush and you’re not too busy to go with short-form messages
Example
It’s not Amt
it is Amount
both have more difference
Is it Meaningful?
Variable named as user
.This variable is cleared to you?. it can’t because it can be string
or int
. if it is named as userName
it would me correct one or userNumber
Same with inserting
or removing
. From this, we get questions like where you inserting, what you inserting. Yes your naming should not give way to raise a question
(NOTE: Declare a name, that should not leave your manager to raise any question from it)
Is it constant?
As I mentioned before coding should tell a story. While narrating the story we should not mess up and confuse the reader. so always go with constant words in a project.
Example
Above all are having the same meaning. if you pick fetchData
then use fetch
for your rest of code naming (where you need that meaning word)
Is it confusing?
In class A
adding the two numbers but in class B
adding an element into an array. Here the function's naming is correct when we understand deeply. But here manager will not look that much deeper. so addNumber
and insertElement
It will be better. But still not better because of parameters names (x, y)
and (x )
. Hope you know what to do with it.
Some free advice
- Use Pronounceable Names
- Don’t use naming to show your anger to the manager like keeping some offensive words like
killingTheElement
shitWithThis
and don't show your anger to the code it will kill you with bugs - Read your code after finished (it should tell you a story)
Some rules for swift developers while declaring the name
Naming for variable and function
while naming these types it should be in lowerCamelCase.
Naming for class, struct, protocol
and enum
while naming these types it should be in UpperCamelCase.
Yes after following these in your code, your code looks like an essay