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.
Camel case is a naming convention where the first letter of every word is capitalized.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Key Expressions for Declaration and Naming
declare a (variable/function/class)
declare a (variable/function/class)
declare
is a verb meaning 'to declare.' The noun form is declaration
. Since variable
, function
, and class
are countable nouns, an article is needed.
define
define
define
is used when a variable is both declared and assigned a value (initialized). Unlike declare
, it assigns memory.
assign a value to a variable
assign a value to a variable
assign
is a verb used to assign a value to a variable. Since value
and variable
are countable nouns, an article is needed.
initialize a variable
initialize a variable
initialize
is the verb used when declaring a variable and setting its initial value. This is the American spelling; the British spelling is initialise
.
reassign a value
reassign a value
reassign
is used to overwrite a new value onto a variable that already has a value.
unassign a value
unassign a value
unassign
is used to remove or clear a value that has been assigned to a variable.
clear the value of a variable
clear the value of a variable
clear
is used to empty a stored value in a variable or field, returning it to its initial state.
allocate memory to a variable
allocate memory to a variable
allocate
is used to assign computing resources or memory to a specific target, commonly used with the preposition to
.
deallocate or release resources
deallocate or release resources
deallocate
or release
is used to return memory or resources that are no longer in use.
name a (variable/function/class)
name a (variable/function/class)
name
as a noun means 'name', and as a verb, it means 'to give a name.' When mentioning naming conventions, the expression in [case]
is used.
rename
rename
rename
is a verb used when assigning a new name to an element that already has an existing name.