Adopting Meaningful Function Naming Conventions

Implement a strategy for naming functions that accurately reflects their purpose to improve team collaboration and code understanding.

0 likes
49 views

Rule Content

{
  "title": "Adopting Meaningful Function Naming Conventions",
  "description": "Implement a strategy for naming functions that accurately reflects their purpose to improve team collaboration and code understanding.",
  "category": "JavaScript Cursor Rules",
  "rules": [
    {
      "name": "Function Naming Conventions",
      "description": "Ensure function names are descriptive and follow established naming conventions to enhance code readability and maintainability.",
      "severity": "warning",
      "pattern": [
        {
          "regex": "function\\s+([a-zA-Z0-9_]+)\\s*\\(",
          "message": "Function names should be in camelCase and start with a verb that clearly describes the action performed."
        }
      ],
      "examples": {
        "incorrect": [
          "function dataProcessing() { /* ... */ }",
          "function UserData() { /* ... */ }"
        ],
        "correct": [
          "function processData() { /* ... */ }",
          "function getUserData() { /* ... */ }"
        ]
      }
    },
    {
      "name": "Boolean Function Naming",
      "description": "Boolean-returning functions should be prefixed with 'is', 'has', or 'can' to clearly indicate their return type.",
      "severity": "warning",
      "pattern": [
        {
          "regex": "function\\s+([a-zA-Z0-9_]+)\\s*\\(.*\\)\\s*\\{\\s*return\\s+(true|false|!|!!|Boolean\\()",
          "message": "Boolean functions should be prefixed with 'is', 'has', or 'can' to indicate their return type."
        }
      ],
      "examples": {
        "incorrect": [
          "function validUser() { return true; }",
          "function checkPermission() { return false; }"
        ],
        "correct": [
          "function isValidUser() { return true; }",
          "function hasPermission() { return false; }"
        ]
      }
    },
    {
      "name": "Avoid Generic Function Names",
      "description": "Function names should be specific and descriptive to avoid ambiguity and improve code clarity.",
      "severity": "warning",
      "pattern": [
        {
          "regex": "function\\s+(doSomething|handleData|process)\\s*\\(",
          "message": "Avoid using generic function names like 'doSomething', 'handleData', or 'process'. Use specific names that describe the function's purpose."
        }
      ],
      "examples": {
        "incorrect": [
          "function doSomething() { /* ... */ }",
          "function handleData() { /* ... */ }"
        ],
        "correct": [
          "function calculateTotalPrice() { /* ... */ }",
          "function fetchUserData() { /* ... */ }"
        ]
      }
    }
  ]
}