summaryrefslogtreecommitdiff
path: root/.cursor/rules/writing-scannable-code.mdc
blob: 97ed87e9482ad2b1db532a4e3d651edafb68e29a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---
alwaysApply: true
---

# Scannable Code Guidelines/Separating Concerns.

## Core Principle

Code should be scannable through proper abstraction, not comments. Use variables and helper functions to make intent clear at a glance.

## Good Scannable Code

- Extract complex logic into well-named variables
- Create helper functions for multi-step operations
- Use descriptive names that explain intent

Examples:

```javascript
// ✅ Scannable: Intent is clear from variable names
const isValidUser = user.isActive && user.hasPermissions;
const shouldProcessPayment = amount > 0 && !order.isPaid;

// ✅ Scannable: Complex logic extracted to helper
const searchParams = buildFreesoundSearchParams({ query, filters, pagination });
const transformedResults = transformFreesoundResults({ rawResults });
```

## Bad Unscannable Code

Avoid:

```javascript
// ❌ Hard to scan: What does this condition mean?
if (type === "effects" || !type) {
  params.append("filter", "duration:[* TO 30.0]");
  params.append("filter", `avg_rating:[${min_rating} TO *]`);
  if (commercial_only) {
    params.append("filter", 'license:("Attribution" OR "Creative Commons 0")');
  }
}

// ❌ Hard to scan: Complex ternary
const sortParam = query
  ? sort === "score"
    ? "score"
    : `${sort}_desc`
  : `${sort}_desc`;
```

## Rule

Make code scannable by extracting intent into variables and helper functions. If you need to think about what code does, extract it. The reader should understand the flow without diving into implementation details.