Faultline Cognitive Encyclopedia

Misconception Codex

A curated compendium of cognitive faultlines, common programming traps, and the precise mental shifts required to master them.

🔍
Memory & Pointersc

Pointer Arithmetic Scaling Fallacy

🔴 The Cognitive Trap

Assuming pointer arithmetic adds raw bytes, so adding 2 to an int* moves forward 2 bytes instead of 2 * sizeof(int) bytes.

🟢 The Mental Shift

C compilers automatically multiply pointer arithmetic additions by the size of the referenced type (sizeof(T)).

✕ Flawed Code:
int *p = arr;
// Adding byte count manually overshoots by 4x!
int *bad = p + (2 * sizeof(int));
✓ Sound Pattern:
int *p = arr;
// Compiler auto-scales: moves forward exactly 2 integer elements
int *good = p + 2;
Memory & Pointerspython

Shallow Copy vs Deep Independence

🔴 The Cognitive Trap

Assuming that list slicing `arr[:]` or `Object.assign()` creates deep isolated duplicates of nested objects.

🟢 The Mental Shift

Shallow copies create new outer containers, but the inner nested objects remain shared memory references.

✕ Flawed Code:
nested = [[1, 2], [3, 4]]
shallow = nested[:]
shallow[0].append(99) # Modifies nested[0] too!
✓ Sound Pattern:
import copy
nested = [[1, 2], [3, 4]]
deep = copy.deepcopy(nested)
deep[0].append(99) # Safe!
Concurrencyjavascript

Async Microtask vs Macrotask Queueing

🔴 The Cognitive Trap

Assuming setTimeout(fn, 0) executes immediately before resolved Promises because it has a 0ms delay.

🟢 The Mental Shift

Microtasks (Promises, queueMicrotask) always drain completely before the event loop advances to the next Macrotask (setTimeout).

✕ Flawed Code:
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
// Trap: thinking Timeout runs first!
✓ Sound Pattern:
// Execution order:
// 1. Synchronous stack
// 2. Microtasks (Promises)
// 3. Macrotasks (setTimeout)
Data Structuresjavascript

Destructive Tree Subtree Assignment

🔴 The Cognitive Trap

Assigning `node.left = invert(node.right)` directly overwrites the left reference before it can be inverted and assigned to `node.right`.

🟢 The Mental Shift

When swapping references in place, either store the original reference in a temporary variable or perform parallel destructuring.

✕ Flawed Code:
node.left = invert(node.right);
node.right = invert(node.left); // Lost original left!
✓ Sound Pattern:
const tempLeft = node.left;
node.left = invert(node.right);
node.right = invert(tempLeft);
Language Semanticsjavascript

Closure Over Shared Loop Variable

🔴 The Cognitive Trap

Capturing a loop variable declared with `var` inside asynchronous callbacks captures the final mutated value rather than each iteration's value.

🟢 The Mental Shift

Block-scoped `let` binds a distinct lexical environment for every iteration, preserving the exact iteration state in closures.

✕ Flawed Code:
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100); // Prints: 3, 3, 3
}
✓ Sound Pattern:
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100); // Prints: 0, 1, 2
}
Data Structuresjavascript

Missing Guard Before Recursive Leap

🔴 The Cognitive Trap

Writing recursive transitions without handling empty/null base cases, causing CallStack Overflow or NullPointer exceptions.

🟢 The Mental Shift

Every recursive descent must begin with an exhaustive terminal guard condition returning a predictable baseline unit.

✕ Flawed Code:
function sum(node) {
  return node.val + sum(node.left) + sum(node.right);
}
✓ Sound Pattern:
function sum(node) {
  if (!node) return 0; // Explicit terminal anchor
  return node.val + sum(node.left) + sum(node.right);
}