Metadata

ID: javascript-code-style/max-class-lines

Language: JavaScript

Severity: Warning

Category: Code Style

Description

A class must stay short (less than 100 lines) to be easy to understand. If your class or function is more than 100 lines, you should refactor your code and ensure that your class is less than 100 lines.

Non-Compliant Code Examples

class DummyClass {
  constructor() {
    this.name = "John";
    this.age = 30;
    this.email = "john@example.com";
    this.address = "123 Main St";
    this.phone = "555-1234";
    this.salary = 50000;
    this.skills = ["JavaScript", "HTML", "CSS"];
    this.projects = [
      { name: "Project 1", duration: 6 },
      { name: "Project 2", duration: 4 },
      { name: "Project 3", duration: 8 }
    ];
    this.status = "active";
    
    // Initialize more properties...
    // ...
    // ...
  }
  
  sayHello() {
    console.log(`Hello, my name is ${this.name}.`);
  }
  
  calculateSalary() {
    let total = this.salary;
    for (let project of this.projects) {
      total += project.duration * 1000;
    }
    return total;
  }
  
  updateEmail(newEmail) {
    if (newEmail.includes("@")) {
      this.email = newEmail;
      console.log("Email updated successfully.");
    } else {
      console.log("Invalid email format.");
    }
  }
  
  addSkill(skill) {
    this.skills.push(skill);
    console.log("Skill added.");
  }
  
  removeSkill(skill) {
    const index = this.skills.indexOf(skill);
    if (index !== -1) {
      this.skills.splice(index, 1);
      console.log("Skill removed.");
    } else {
      console.log("Skill not found.");
    }
  }
  
  promote() {
    if (this.status === "active") {
      this.salary *= 1.1;
      console.log("Promoted successfully.");
    } else {
      console.log("Cannot promote an inactive employee.");
    }
  }
  
  resign() {
    if (this.status === "active") {
      this.status = "inactive";
      console.log("Resigned successfully.");
    } else {
      console.log("Already inactive.");
    }
  }
  
  // More methods...
  // ...
  // ...
  
  // Long method 1
  longMethod1() {
    // Implementation goes here...
    // ...
    // ...
  }
  
  // Long method 2
  longMethod2() {
    // Implementation goes here...
    // ...
    // ...
  }
  
  // Long method 3
  longMethod3() {
    // Implementation goes here...
    // ...
    // ...
  }
  
  // Long method 4
  longMethod4() {
    // Implementation goes here...
    // ...
    // ...
  }
  
  // Long method 5
  longMethod5() {
    // Implementation goes here...
    // ...
    // ...
  }
  
  // More long methods...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  // ...
  
  // Final method
  finalMethod() {
    // Implementation goes here...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
    // ...
  }
}

Compliant Code Examples

class DummyClass {
  constructor() {
    this.name = "John";
    this.age = 30;
    this.email = "john@example.com";
    this.address = "123 Main St";
    this.phone = "555-1234";
    this.salary = 50000;
    this.skills = ["JavaScript", "HTML", "CSS"];
    this.projects = [
      { name: "Project 1", duration: 6 },
      { name: "Project 2", duration: 4 },
      { name: "Project 3", duration: 8 }
    ];
    this.status = "active";
  }
  
  sayHello() {
    console.log(`Hello, my name is ${this.name}.`);
  }
  
  calculateSalary() {
    let total = this.salary;
    for (let project of this.projects) {
      total += project.duration * 1000;
    }
    return total;
  }
}