Sparc Refinement Example 4 Complexity Reduction

Sub-skill of sparc-refinement: Example 4: Complexity Reduction.

vamseeachanta Updated

File contents

Example 4: Complexity Reduction

Example 4: Complexity Reduction

// Bad: Cyclomatic complexity = 7
function processUser(user: User): void {
  if (user.age > 18) {
    if (user.country === 'US') {
      if (user.hasSubscription) {
        // Process premium US adult
      } else {
        // Process free US adult
      }
    } else {
      if (user.hasSubscription) {
        // Process premium international adult
      } else {
        // Process free international adult
      }
    }
  } else {
    // Process minor
  }
}

// Good: Cyclomatic complexity = 2
function processUser(user: User): void {
  const processor = getUserProcessor(user);
  processor.process(user);
}

function getUserProcessor(user: User): UserProcessor {
  const type = getUserType(user);
  return ProcessorFactory.create(type);
}

vamseeachanta/workspace-hub/tree/main/.agents/skills/_archive/development/sparc/sparc-refinement/example-4-complexity-reduction commit 4e68fa5cfb

Frequently asked questions

npx skillmds@latest add vamseeachanta/sparc-refinement-example-4-complexity-reduction