Key Expressions for Variables and Constants
In programming, declaration
refers to informing the compiler or interpreter about the name and type of something you intend to use later, such as a variable or function.
For example, in the C language, int a;
is a declaration that informs about creating an integer variable named a.
In TypeScript, you can declare a constant to store a boolean value, like const isDev: boolean;
, which informs about a constant named isDev
.
Naming
refers to the act of giving meaningful names to these variables, functions, etc.
For instance, names like userName and calculateTotal are self-explanatory about their purpose at just a glance.
The difference between a good developer and a less skilled one starts showing in their ability to name things effectively.
A great developer crafts understandable names and follows consistent naming conventions like camelCase
(e.g., isLoggedIn) or snake_case
(e.g., user_name) to enhance code readability and maintainability.
Naming Convention
Most programming languages do not allow spaces in variable names, so developers implicitly follow specific naming conventions
when naming files, variables, and constants.
The four main naming conventions are:
- Camel case (or Lower camel case)
- Pascal case (or Upper camel case)
- Snake case
- Kebab case
Let's compare each naming method.
1. Camel case
Generally refers to lower camel case, where the first word is in lowercase and the first letter of subsequent words is capitalized.
- Examples:
totalPurchaseCount
,setCurrentValue
- Features: The capitalized words look like the humps of a camel, hence the name.
- Usage: Used in languages like JavaScript, Java for variable and function names.
2. Pascal case
Also known as Upper camel case, where the first letter of every word is capitalized.
- Examples:
TotalPurchaseCount
,SetCurrentValue
- Features: Similar to camel case but starts with a capital letter in the first word as well.
- Usage: Used for class names, component names, and database table names in languages like Java, C#, TypeScript, etc.
3. Snake case
All letters are in lowercase, with words separated by underscores (_).
- Examples:
total_purchase_count
,set_current_value
- Features: Named for resembling a snake slithering along.
- Usage: Used for variable and function names in Python, constants in C, and certain database field names.
4. Kebab case
All letters are in lowercase, with words separated by hyphens (-).
- Examples:
total-purchase-count
,set-current-value
- Features: Originates from the resemblance to kebab skewers with words strung together.
- Usage: Used for CSS class names/IDs, URLs, package names, HTTP headers, etc.
Now let's explore key English expressions related to declaration, assignment, and naming together.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.