Identifying complexity
Open live editorDiscover → Predict → Understand
What evidence shows that identifying complexity keeps state and application behavior understandable?
A heuristic can reveal review candidates but is not a universal code quality score. Inspect responsibilities and regression risk before refactoring.
- Explain the operation and its responsibility.
- Predict actual values before execution.
- Identify local calculation, actual DOM and external execution boundaries.
Conceptual application flow
- User intent
- Action / handler
- Business logic
- State
- Render / persistence
Five source examples
Working contract
A heuristic can reveal review candidates but is not a universal code quality score. Inspect responsibilities and regression risk before refactoring.
async function main(input) {
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}Input
if (a) run();
Fixed expected result
{
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
}Observe input preservation
The wrapper records the original input and compares it after the operation. Some lessons intentionally demonstrate mutation in a local copy.
async function calculate(input){
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}
async function main(input){const before=structuredClone(input);const result=await calculate(input);return {result,inputUnchanged:JSON.stringify(input)===JSON.stringify(before)};}Input
if (a) run();
Fixed expected result
{
"result": {
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
},
"inputUnchanged": true
}Investigate the discarded result
An early return bypasses the intended operation. Repair the first incorrect boundary rather than changing the expected result.
async function calculate(input){
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}
async function main(input){return null; // Deliberate early return discards calculation
return await calculate(input);}Input
if (a) run();
Fixed expected result
{
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
}Repeat the deterministic calculation
Invoke the same declared pure/model operation twice and compare both results. This is not an external idempotency guarantee.
async function calculate(input){
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}
async function main(input){return [await calculate(structuredClone(input)),await calculate(structuredClone(input))];}Input
if (a) run();
Fixed expected result
[
{
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
},
{
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
}
]Frozen regression contract
Execute the operation and compare with a fixed fixture result. Changing the calculation must not silently rewrite the expectation.
async function calculate(input){
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}
async function main(input){const actual=await calculate(input);const expected={"lines":1,"branches":1,"note":"Heuristic, not parser complexity"};if(JSON.stringify(actual)!==JSON.stringify(expected))throw Error("Regression contract mismatch");return {pass:true,actual};}Input
if (a) run();
Fixed expected result
{
"pass": true,
"actual": {
"lines": 1,
"branches": 1,
"note": "Heuristic, not parser complexity"
}
}Important operations
[
{
"line": 1,
"source": "async function main(input) {",
"explanation": "Declare the named operation or local value and follow its scope in the source."
},
{
"line": 2,
"source": "return {lines:input.split(\"\\n\").length,branches:(input.match(/\\bif\\b/g)||[]).length,note:\"Heuristic, not parser complexity\"};",
"explanation": "Return the observable result for the caller; rendering and external effects belong to explicit boundaries."
},
{
"line": 3,
"source": "}",
"explanation": "Declare the named operation or local value and follow its scope in the source."
}
]Guided mission
- Discover the problem and inspect source/input.
- Write a prediction. Run the actual source and inspect the returned value.
- Change one requirement and state the new expected result independently.
- Load the repair exercise, reproduce the failure and use hints.
- Restore the authored fixture and Check, then explain and extend.
Three hints
- Locate the input, transition and returned result before editing.
- Compare the first differing field and trace which expression produced it.
- Remove the deliberate early return and restore the named responsibility; compare with Finished Code only after trying.
Complete solution
async function main(input) {
return {lines:input.split("\n").length,branches:(input.match(/\bif\b/g)||[]).length,note:"Heuristic, not parser complexity"};
}Common mistake: Changing the expected result to match a defect instead of repairing the behavior.
Invalid JSON is rejected before execution. Syntax and thrown errors are shown. Model kernels for DOM, HTTP and Node teach contracts; use the actual browser and exported server projects for those execution boundaries.
Reflect and extend
Which returned value or actual DOM observation supports your explanation? Which side effect was outside this check?
Create a second normal, boundary and invalid case. Write exact expected values before execution and explain what the provided fixture does not prove.
AI prompt goals
- Explain the current source, input, actual output and architecture boundary.
- Help refactor one responsibility while preserving independently expected behavior.
- Investigate the current error and propose normal, boundary and invalid tests.