Naming conventions and style guides in programming
Published: 2023-06-01 14:35:46

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"
] 

```

**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):_

```java
int numberOfStudents;
String userName;
double averageScore;
```

_Method definition (Python) :_
```python
def calculateTotalAmount(paymentList):
    totalAmount = 0
    for payment in paymentList:
        totalAmount += payment
    return totalAmount
```

_Function call (JavaScript):_
```javascript
let formattedName = formatUserName(userName);
```

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#):_
```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; }
    }
}
```

_Method definition (Java):_
```Java
public void CalculateTotalAmount(List<decimal> paymentList)
{
    decimal totalAmount = 0;
    foreach (decimal payment in paymentList)
    {
        totalAmount += payment;
    }
    return totalAmount;
}
```

_Interface definition (TypeScript):_
```javascript
interface IEmployee {
    empCode: number;
    empName: string;
    getSalary: (number) => number; 
    getManagerName(number): string; 
}
```

_Enum declaration:_
```C#
public enum DaysOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
```

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):_

```python
number_of_students = 30
user_name = "johnsmith"
average_score = 85.5
```

_Function definition (Ruby):_

```ruby
def calculate_total_amount(payment_list)
    total_amount = 0
    payment_list.each do |payment|
        total_amount += payment
    end
    total_amount
end
```

_Database column names (SQL):_
```sql
CREATE TABLE users (
    id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email_address VARCHAR(255),
    date_of_birth DATE
);
```

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):_
```html
<div class="container-fluid"></div>
```

_CSS class (CSS):_
```css
.header-title {
    font-size: 20px;
    color: #333;
}
```
_Package name (Java):_
```java
com.example.my-project
```

_Command-line arguments (Shell script):_
```shell
./script.sh --verbose-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):_
```python
MAX_ATTEMPTS = 5
PI_VALUE = 3.14159
ERROR_MESSAGE = "An error occurred."
```

_Constant declaration (JavaScript):_

```javascript
const MAX_ATTEMPTS = 5;
const PI_VALUE = 3.14159;
const ERROR_MESSAGE = "An error occurred.";
```

_Global variables (C):_
```c
#define BUFFER_SIZE 1024
const int MAX_ITEMS = 10;
```

_Configuration variables (Shell script):_
```shell
DATABASE_HOST="localhost"
DATABASE_PORT=5432
USERNAME="admin"
PASSWORD="password123"
```

_Preprocessor directives (C++):_
```cpp
#define LOG_ENABLED
#define VERSION "1.0.0"
```

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 😃 !!!