Tuesday, March 13, 2018

Variable Constant And Naming Conventions

Variable
It is actually impossible to become a perfect developer without knowing about the basics of programming. And the most basic thing of any programming language is to use the variables, constants and its datatypes. Variable is actually the mathematical term but in programming we don’t use variable as we do in mathematics. In programming world, there we’ve some limitations to use the variables about its declaration, definition. Variable is actually a container in which we store the values. Variables, Constants, Methods, Interface, Class, Namespace these are the things we say Identifiers. Programmers name the identifiers their own and use them in the program.
Variables are declared with the help of datatypes. We’ve 2 terms

  • ·        Variable Declaration
  • ·        Variable Definition

Variable Declaration in which we just declare the variable using datatypes. But in our mostly development environments when we declare the variable it becomes automatically define into the memory like

 int x;  

Now in C#, it will automatically assigned with 0. Here we just declare the variable but it becomes defined automatically.
Variable Defintion we declare the variable and initialize it with the value. This is what we call variable definition.

 int x = 1;  

Each variable has its own datatype and this type is used to allocate the space for that variable into the memory, it also helps with its different function that we can apply on that type to manipulate different operations. We should use meaningful names for identifiers because this is the best programming approach.

 int x, int y;               // (really poor approach)  

Because if you name your identifiers properly then it would be helpful to understand the program for the other developer as well later on. We can’t name the variable which names are fixed in C# language. If you want to declare the variable in this way, then you need to use ( _ , @ ) these signs to declare the variable.

 int @int = 5;  
 int _int = 3;  

Variables are mutable (that can be changed) we can change the variable values in our application.

Strongly Typed
C# is the strongly typed programming language. It means that if we declare the variable for the specific type (int, float, long) then we can’t change the type of this variable later. But it is possible that we make another variable and cast the value of 1st variable into the 2nd variable, but it will be something lossy value conversion.

Constants
Constants are the variables that can’t be changed throughout running the application. Constants are Immutable (that can’t be changed). We use the constants when we need a specific value throughout the application like we initialize the constant and assign it file path, now we just use this constant in our entire application.

 const int distance = 500;  

When we declare the constant variable, we need to initialize at that time as well. Because on constant variable creation, program sets its value. And in later, we can’t change its value.

Naming Convention
It is quite difficult to name the meaningful names to the identifiers. Anyhow let’s discuss naming convention. Listen if you want to look like a good developer in the market then you must use the meaningful names and use the naming convention approaches in the programming.
Camel Case Notation
This notation is very common, we normally use this notation to name our variables and method arguments. In camel case notation, just 1st letter is lowercase and then each letter of the next word will be in uppercase e.g.
 string firstName;  
Pascal Notation
Pascal Notation is used when we name the class and methods. In pascal notation, each letter of each word is capital e.g.
 public class VehicleCategory {}  

Conclusion
Variables are open to change and constant values can’t change in the program.

0 comments:

Post a Comment