DEV Community

Cover image for Naming conventions and style guides in programming
Samuel Egbajie
Samuel Egbajie

Posted on • Updated on

Naming conventions and style guides in programming

In this article, you will learn cases used for naming conventions while writing codes and also get references to some style guides that will enhance your naming convention depending on the language you work with.

It is important to know about the different naming conventions or cases used in coding for different reasons such as for best practices and proper codes maintainability, readability, codes consistency, tooling and automation, collaboration and more.

Different cases are used while writing codes to specify the naming convention for variables, functions, classes, files, folders and other code objects.

Depending on the programming language and accepted practices in the community, different case styles may be used. Following are some typical case types and examples in which they are employed:

[
"camelCase", 
"PascalCase", 
"snake_case",
"kebab-case",
"SCREAMING_SNAKE_CASE"
] 

Enter fullscreen mode Exit fullscreen mode

1. Camel Case (also known as lower camel case): By applying this naming case, there are no spaces or underscores between the words in this style, which capitalises the initial letter of every word except the first word. It is frequently used in naming variables, functions, and methods in programming languages such as JavaScript, Java, Python, C# etc.

Example:
Here are some examples of code snippets written in camelCase :

Variable declaration (Java):

int numberOfStudents;
String userName;
double averageScore;
Enter fullscreen mode Exit fullscreen mode

Method definition (Python) :

def calculateTotalAmount(paymentList):
    totalAmount = 0
    for payment in paymentList:
        totalAmount += payment
    return totalAmount
Enter fullscreen mode Exit fullscreen mode

Function call (JavaScript):

let formattedName = formatUserName(userName);
Enter fullscreen mode Exit fullscreen mode

Without only considering methods and variable names, these examples show how to utilise camelCase, where each word within an identifier begins with an uppercase letter except for the first. To increase readability and maintain a consistent coding style.

2. Pascal Case, often called upper camel case: This is a variant of camel case in which the initial letter of each word is capitalised. In programming languages like C#, C++, and Python, it is frequently used for naming classes and types.

Examples

Here are some examples of code snippets written in PascalCase:

Class definition (C#):

public class BankAccount
{
    private string accountNumber;
    private decimal currentBalance;

    public string AccountNumber
    {
        get { return accountNumber; }
        set { accountNumber = value; }
    }

    public decimal CurrentBalance
    {
        get { return currentBalance; }
        set { currentBalance = value; }
    }
}
Enter fullscreen mode Exit fullscreen mode

Method definition (Java):

public void CalculateTotalAmount(List<decimal> paymentList)
{
    decimal totalAmount = 0;
    foreach (decimal payment in paymentList)
    {
        totalAmount += payment;
    }
    return totalAmount;
}
Enter fullscreen mode Exit fullscreen mode

Interface definition (TypeScript):

interface IEmployee {
    empCode: number;
    empName: string;
    getSalary: (number) => number; 
    getManagerName(number): string; 
}
Enter fullscreen mode Exit fullscreen mode

Enum declaration:

public enum DaysOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
Enter fullscreen mode Exit fullscreen mode

These examples demonstrates name classes, methods, interfaces and enums, using PascalCase. Every word, even the first one, has its first letter capitalised when using PascalCase. It is frequently used to maintain a consistent naming convention and enhance code readability in languages like C#, Java,TypeScript, JavaScript, C++ etc.

3. Snake Case: This style often uses lowercase letters and underscores (_) between words. It is frequently used for naming variables and functions in languages like Python and Ruby and also for naming files and folders in frameworks like Reactjs.

Examples

Here are some examples of code snippets written in snake_case:

Variable declaration (Python):

number_of_students = 30
user_name = "johnsmith"
average_score = 85.5
Enter fullscreen mode Exit fullscreen mode

Function definition (Ruby):

def calculate_total_amount(payment_list)
    total_amount = 0
    payment_list.each do |payment|
        total_amount += payment
    end
    total_amount
end
Enter fullscreen mode Exit fullscreen mode

Database column names (SQL):

CREATE TABLE users (
    id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email_address VARCHAR(255),
    date_of_birth DATE
);
Enter fullscreen mode Exit fullscreen mode

These examples name variables, functions, constants, database columns, and configuration files using snake_case. snake_case separates words with underscores (_) and lowercase letters. It is frequently used to maintain a consistent naming convention and enhance code readability in languages like Python, Ruby, and SQL.

4. Kebab Case (also spelled dash case, spinal case): Between words in this style, hyphens (-) and lowercase letters are used. It is frequently used in HTML attributes, file names, and URLs. However, naming variables or functions using it is not as common in programming.

Examples
Here are some examples of code snippets written in kebab-case:

HTML classes (HTML):

<div class="container-fluid"></div>
Enter fullscreen mode Exit fullscreen mode

CSS class (CSS):

.header-title {
    font-size: 20px;
    color: #333;
}
Enter fullscreen mode Exit fullscreen mode

Package name (Java):

com.example.my-project
Enter fullscreen mode Exit fullscreen mode

Command-line arguments (Shell script):

./script.sh --verbose-mode
Enter fullscreen mode Exit fullscreen mode

These examples employ the kebab-case naming convention for command-line parameters, URLs, CSS classes, HTML elements, and CSS classes. Lowercase letters and hyphens (-) are used in kebab case to divide words. It is frequently used in languages like HTML, CSS, JavaScript, and others that permit the use of hyphens as word separators. For many usage scenarios, kebab-case enhances readability and guarantees consistency in naming standards.

5. Screaming Snake Case: This font uses uppercase letters and underscores (_) between words. It is also referred to as upper case snake case or screaming case. Usually, it is employed to name constants or global variables.

Examples

Here are some examples of code snippets written in SCREAMING_SNAKE_CASE:

Constant declaration (Python):

MAX_ATTEMPTS = 5
PI_VALUE = 3.14159
ERROR_MESSAGE = "An error occurred."
Enter fullscreen mode Exit fullscreen mode

Constant declaration (JavaScript):

const MAX_ATTEMPTS = 5;
const PI_VALUE = 3.14159;
const ERROR_MESSAGE = "An error occurred.";
Enter fullscreen mode Exit fullscreen mode

Global variables (C):

#define BUFFER_SIZE 1024
const int MAX_ITEMS = 10;
Enter fullscreen mode Exit fullscreen mode

Configuration variables (Shell script):

DATABASE_HOST="localhost"
DATABASE_PORT=5432
USERNAME="admin"
PASSWORD="password123"
Enter fullscreen mode Exit fullscreen mode

Preprocessor directives (C++):

#define LOG_ENABLED
#define VERSION "1.0.0"
Enter fullscreen mode Exit fullscreen mode

For naming constants, enum values, global variables, configuration variables, and preprocessor directives in these examples, SCREAMING_SNAKE_CASE is employed. Uppercase letters and underscores (_) are used in SCREAMING_SNAKE_CASE to divide words. It is frequently used to represent constants and global values that shouldn't be changed in languages like Python, C++, and shell scripts. SCREAMING_SNAKE_CASE is frequently used to increase code readability and maintain consistency and highlights that certain values are intended to be treated as immutable.

STYLE GUIDES FOR SOME LANGUAGE TO AID BETTER NAMING CONVENTIONS AND BEST PRACTICES

These style guides often define coding conventions, formatting standards, and best practices for writing clean and maintainable code. Here are a few examples:

HTML:
i. Google HTML/CSS Style Guide - https://google.github.io/styleguide/htmlcssguide.html
ii. Mozilla Developer Network (MDN) HTML Style Guide - https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/HTML
iii. Web Code Guidelines: HTML - https://webcodeguidelines.com/HTML/

CSS:
i. Airbnb CSS / Sass Style Guide - https://github.com/airbnb/css
ii. Google HTML/CSS Style Guide - https://google.github.io/styleguide/htmlcssguide.html
iii. Mozilla Developer Network (MDN) CSS Style Guide - https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/CSS

Python:

i. PEP 8: Style Guide for Python Code - https://www.python.org/dev/peps/pep-0008/
ii. Google Python Style Guide -https://google.github.io/styleguide/pyguide.html

JavaScript:

i. Airbnb JavaScript Style Guide - https://github.com/airbnb/javascript
ii. Google JavaScript Style Guide - https://google.github.io/styleguide/jsguide.html

Java:

i. Oracle Code Conventions for the Java Programming Language - https://www.oracle.com/java/technologies/javase/codeconventions-introduction.html
ii. Google Java Style Guide - https://google.github.io/styleguide/javaguide.html

C++:

i. Google C++ Style Guide - https://google.github.io/styleguide/cppguide.html
ii. LLVM Coding Standards - https://llvm.org/docs/CodingStandards.html

C#:

i. Microsoft C# Coding Conventions - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions
ii. C# Coding Standards and Naming Conventions - https://www.dofactory.com/reference/csharp-coding-standards

Ruby:

i. Ruby Style Guide - https://rubystyle.guide/
ii. GitHub Ruby Style Guide - https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md

PHP:

i. PHP-FIG PSR-12: Extended Coding Style Guide - https://www.php-fig.org/psr/psr-12/
ii. Laravel Coding Style Guide - https://laravel.com/docs/8.x/contributions#coding-style-guide

Swift:

i. The Swift Programming Language - API Design Guidelines - https://swift.org/documentation/api-design-guidelines/
ii. Raywenderlich.com Swift Style Guide - https://github.com/raywenderlich/swift-style-guide

These style guides provide recommendations on structuring and formatting code, as well as best practices for naming conventions, indentation, comments, and more.

Conclusion
The specific naming standards can vary based on the programming language and coding style used by a given development team; it's crucial to remember that these are merely conventions and suggestions.
Also note that coding style guides can be subjective and vary depending on the organisation, team, or individual preferences. It's always a good idea to follow the conventions used in your specific project or consult any guidelines provided by the language creators or major organisations in the respective communities.

If you like this, they is a chance you might want to read my other articles.
Thanks for reading and happy clean coding 😃 !!!

Top comments (0)