$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-review-codeReview MATLAB code for quality, performance, maintainability, and adherence to MathWorks coding standards. Uses check_matlab_code and matlab_coding_guidelines. Use when reviewing code, checking style, finding code smells, assessing quality, or preparing code for handoff or public
| 1 | # Code Review |
| 2 | |
| 3 | Systematically review MATLAB code for quality, correctness, performance, and adherence to MathWorks coding conventions using static analysis and manual inspection patterns. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - User asks to review, audit, or improve code quality |
| 8 | - User wants to check adherence to MathWorks coding standards |
| 9 | - Preparing code for handoff, publication, or open-source release |
| 10 | - After a significant implementation — verify before committing |
| 11 | - User reports "code smells" or asks for cleanup suggestions |
| 12 | |
| 13 | ## When NOT to Use |
| 14 | |
| 15 | - User wants to debug a runtime error — use `matlab-debugging` instead |
| 16 | - User wants to optimize performance — use performance profiling skills |
| 17 | - User wants to generate tests — use `matlab-testing` instead |
| 18 | |
| 19 | ## Workflow |
| 20 | |
| 21 | 1. **Run static analysis** — Use `check_matlab_code` MCP tool on all target files |
| 22 | 2. **Load coding standards** — Read the `matlab_coding_guidelines` MCP resource |
| 23 | 3. **Check naming** — Verify functions, classes, variables, and files follow conventions |
| 24 | 4. **Review function signatures** — Arguments blocks, input/output counts, name-value patterns |
| 25 | 5. **Assess structure** — Function length, nesting depth, complexity |
| 26 | 6. **Check patterns** — Vectorization, preallocation, modern API usage |
| 27 | 7. **Summarize** — Report findings by severity: errors > warnings > suggestions |
| 28 | |
| 29 | ## Step 1: Static Analysis |
| 30 | |
| 31 | Use the `check_matlab_code` MCP tool on each file. Then inspect results programmatically: |
| 32 | |
| 33 | ```matlab |
| 34 | info = checkcode("src/computeArea.m", "-struct"); |
| 35 | for k = 1:numel(info) |
| 36 | fprintf('Line %d (col %d-%d): %s\n', ... |
| 37 | info(k).line, info(k).column(1), info(k).column(end), info(k).message); |
| 38 | end |
| 39 | ``` |
| 40 | |
| 41 | For directory-wide analysis (R2022b+): |
| 42 | |
| 43 | ```matlab |
| 44 | issues = codeIssues("src"); |
| 45 | disp(issues.Issues); |
| 46 | ``` |
| 47 | |
| 48 | ## Step 2: Load Coding Standards |
| 49 | |
| 50 | Read the `matlab_coding_guidelines` MCP resource to get the authoritative MathWorks coding standards. Use these as the baseline for all naming, formatting, and structural checks. |
| 51 | |
| 52 | ## Review Checklist |
| 53 | |
| 54 | ### Naming |
| 55 | |
| 56 | | Element | Convention | Example | |
| 57 | |---------|-----------|---------| |
| 58 | | Functions | lowerCamelCase, verb phrase | `computeArea`, `loadData` | |
| 59 | | Classes | PascalCase | `SensorReader`, `DataProcessor` | |
| 60 | | Variables | lowerCamelCase, descriptive | `sampleRate` not `sr` | |
| 61 | | Constants | UPPER_SNAKE or `Constant` property | `MAX_ITERATIONS` | |
| 62 | | Test files | `t` prefix | `tComputeArea.m` | |
| 63 | | App files | PascalCase | `DashboardApp.m` | |
| 64 | | File = function | File name matches primary function | `computeArea.m` → `function computeArea` | |
| 65 | |
| 66 | ### Function Quality |
| 67 | |
| 68 | | Check | Standard | Severity | |
| 69 | |-------|----------|----------| |
| 70 | | Input count | Max 6 positional inputs | Warning | |
| 71 | | Output count | Max 4 outputs | Warning | |
| 72 | | Validation | `arguments` block present | Warning | |
| 73 | | Name-value args | `options.Name` pattern (not `varargin`) | Suggestion | |
| 74 | | Length | Flag if >50 lines | Suggestion | |
| 75 | | Nesting | Flag if >3 levels deep | Warning | |
| 76 | | `end` keyword | All functions terminated with `end` | Warning | |
| 77 | | Help text | H1 line present for public functions | Suggestion | |
| 78 | |
| 79 | ### Code Patterns |
| 80 | |
| 81 | | Check | Modern | Legacy (flag it) | |
| 82 | |-------|--------|-------------------| |
| 83 | | Multi-panel figures | `tiledlayout`/`nexttile` | `subplot` | |
| 84 | | Date/time | `datetime` | `datenum`/`datestr` | |
| 85 | | Strings | `string` type | char arrays for text | |
| 86 | | Vectorization | `.*`, `./`, logical indexing | Loops over elements | |
| 87 | | Preallocation | `zeros(n,1)` before loop | Growing arrays in loops | |
| 88 | | Data containers | `table`/`timetable` | Raw matrices for named data | |
| 89 | | Dynamic eval | Direct function calls | `eval`, `evalin`, `assignin` | |
| 90 | |
| 91 | ### High-Severity Flags |
| 92 | |
| 93 | These should always be reported as errors: |
| 94 | |
| 95 | - Use of `eval`, `assignin`, or `evalin` — security and maintainability risk |
| 96 | - Growing arrays inside loops without preallocation — performance |
| 97 | - Shadowing built-in functions — `sum = 5` shadows `sum()` |
| 98 | - Missing `arguments` block in public-facing functions |
| 99 | - Hardcoded file paths with backslashes |
| 100 | |
| 101 | ### What checkcode Misses |
| 102 | |
| 103 | `check_matlab_code` does NOT catch all issues. After running static analysis, **always scan the source code** for these common problems that require visual inspection: |
| 104 | |
| 105 | - **`subplot` usage** — not flagged by checkcode, but should use `tiledlayout`/`nexttile` |
| 106 | - **Shadowed builtin variables** — `sum = 0` shadows `sum()`, checkcode may not flag it |
| 107 | - **Deep nesting** (>3 levels) — checkcode does not measure nesting depth |
| 108 | - **Hardcoded backslash paths** — checkcode flags unused variables but not path style |
| 109 | - **Magic numbers** — unlabeled constants in code (e.g., `if length(x) |