Skip to content

Latest commit

 

History

History
301 lines (230 loc) · 6.68 KB

clean_code.md

File metadata and controls

301 lines (230 loc) · 6.68 KB

Variable

How to name a variable?

  • Use camelCase for variable name.
  • Variable name should be noun.

Example

let productPrice;
let customerName;
let bookTitle;
let carMake;
let employeeID;

Add Unit In Your Variable Name

  • The practice of adding units to your variable names is a good coding convention that helps improve code readability and maintainability.

Example

// Weight
let weightInKilograms;
let weightInPounds;

// Length
let lengthInMeters;
let lengthInCentimeters;
let lengthInInches;

// Temperature
let temperatureInCelsius;
let temperatureInFahrenheit;

// Volume
let volumeInLiters;
let volumeInGallons;

// Speed
let speedInKilometersPerHour;
let speedInMilesPerHour;

// Time
let timeInSeconds;
let timeInMinutes;
let timeInHours;

// Money
let moneyInDollars;
let moneyInEuros;

Global Variables

How to name a global variable?

  • Use prefix global at start.
  • Use camelCase for variable name.

Example

let globalProductPrice;
let globalCustomerName;
let globalBookTitle;
let globalCarMake;
let globalEmployeeID;

Private Variables

How to name a private variable?

  • Use prefix private at start.
  • Use camelCase for variable name.

Example

class MyClass {
  constructor() {
    this._privateProductPrice = 0;
    this._privateCustomerName = "";
    this._privateBookTitle = "";
    this._privateCarMake = "";
    this._privateEmployeeID = "";
  }
}

Constant

How to name a constant?

  • Use UPPER_CLASS for constant names.
  • Use _ Underscore for space between constant names.

Example

const MAX_VALUE = 100;
const PI = 3.14159;
const API_ENDPOINT = "https://api.example.com/data";
const DEFAULT_TIMEOUT = 5000;
const COPYRIGHT_YEAR = 2022;

Global Constant

How to name a Global Constant?

  • Use GLOBAL as prefix at start.
  • Use UPPER_CLASS for constant names.
  • Use _ Underscore for space between constant names.

Example

const GLOBAL_API_KEY = "your_api_key_here";
const GLOBAL_BASE_URL = "https://api.example.com";
const GLOBAL_TIMEOUT_DURATION = 5000;
const GLOBAL_MAX_RETRY_ATTEMPTS = 3;
const GLOBAL_APP_VERSION = "1.0.0";

Private Constant

How to name a private constant?

  • Use _PRIVATE as prefix at start.
  • Use UPPER_CLASS for constant names.
  • Use _ Underscore for space between constant names.

Example

const _PRIVATE_MAX_VALUE = 100;
const _PRIVATE_PI = 3.14159;
const _PRIVATE_API_ENDPOINT = "https://api.example.com/data";
const _PRIVATE_DEFAULT_TIMEOUT = 5000;
const _PRIVATE_MAX_RETRY_ATTEMPTS = 3;

Boolean

How to name a boolean?

  • Use a clear and concise name that describes the variable's purpose or meaning.
  • Start the variable name with a verb (e.g., "is", "has", "can", "should") to indicate its boolean nature.
  • Use camelCase for variable names.
  • Avoid using generic names like "flag" or "status" unless they provide additional context.
  • Avoid using names that are already used for other types of variables.

Example

// Example 1: Describing the variable's purpose
let isNumberEven;

// Example 2: Using "is" to indicate a condition or state
let isUserLoggedIn;

// Example 3: Using "has" to indicate the presence or existence of something
let hasValidEmail;

// Example 4: Using "can" to indicate the ability or possibility of something
let canEditProfile;

// Example 5: Using "should" to indicate a recommendation or expectation
let shouldDisplayWarning;

Function

Resource

How to write better function name?

  • Use nouns (plural): Arrays typically store collections of related data.
  • Start with a verb (optional): If the array undergoes transformations.

Array

How to write better array name?

Example

const shoppingCartItems = ["apple", "banana", "milk"];
const productPrices = [1.99, 2.5, 3.99];
const customerReviews = [
  { name: "Alice", rating: 5, comment: "Great product!" },
];
const monthlySalesData = [1000, 1200, 1500];
const filteredSearchResults = ["shirt", "jeans", "jacket"];

Object

How to write better object name?

  1. Context-Specific
  • Consider the object's usage within your application.
  • Example: const product = { id: 123, name: "T-Shirt", price: 19.99 };
  1. Singular Nouns (for Single Objects)
  • Use singular nouns to represent a single entity.
  • Example: const user = { username: "bob", email: "bob@example.com" };
  1. Plural Nouns (for Collections)
  • Use plural nouns for objects that represent collections of similar items.
  • Example: const customers = [{ name: "Alice", id: 1 }, { name: "Bob", id: 2 }];

Example

const shoppingCart = {
  items: [],
  totalPrice: 0,
  addItem(item) {
    this.items.push(item);
    this.totalPrice += item.price;
  },
  removeItem(itemId) {},
};

const product = {
  id: 123,
  name: "T-Shirt",
  price: 19.99,
  description: "A comfortable and stylish T-shirt",
  stock: 10,
  addReview(review) {},
};

const user = {
  username: "alice",
  email: "alice@example.com",
  firstName: "Alice",
  lastName: "Smith",
  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  },
};

const customer = {
  id: 456,
  userId: 123,
  shippingAddress: {},
  billingAddress: {},
};

const post = {
  id: 789,
  userId: 345,
  content: "This is a social media post!",
  createdAt: new Date(),
  comments: [],
  addComment(comment) {
    this.comments.push(comment);
  },
};