My coding style, detailed on my personal site at https://grvpanchal.me/my-coding-style/, emphasizes explicitness, readability, and simplicity over modern idioms like type inference, functional programming, and heavy OOP. This approach prioritizes code that remains clear without an IDE, favoring explicit types, detailed names, and procedural structures for long-term maintainability.[3]
Core Principles
I have outlined eight key rules that deviate from common practices in languages like TypeScript or Rust, focusing on human readability over tool-assisted brevity. These are derived directly from his philosophy of making code self-documenting and IDE-independent.
- No inferred types: Always declare types explicitly instead of relying on inference (e.g.,
let x: i32 = 5;overlet x = 5;). This ensures types are visible in plain text, like on GitHub, without grayed-out IDE hints.[3] - Use detailed names: Prefer long, descriptive identifiers (e.g.,
calculateTotalUserAccountBalanceovercalcTotal). This conveys intent without comments, though it trades brevity for clarity.[3] - No functional code: Avoid higher-order functions, lambdas, and chaining (e.g., no
map,filter). Stick to imperative loops for straightforward execution flow.[3] - Limited OOP: Use classes/structs sparingly for data grouping, not behavior. Avoid inheritance and heavy encapsulation; prefer simple structs with free functions.[3]
- No exceptions, use error values: Handle errors with explicit returns (e.g.,
Result<T, E>) rather than try-catch, making control flow predictable.[3] - Readability over conciseness: Prioritize verbose but clear code; avoid clever shortcuts that obscure logic.[3]
- Prefer early returns and extraction: Use guard clauses for early exits and extract functions to reduce nesting, improving flow.[3]
- Prefer simplicity over conciseness: Follow KISS (Keep It Simple, Stupid) and YAGNI (You Aren’t Gonna Need It), avoiding over-engineering.[1][3]
These align with broader clean code tenets like vertical and horizontal formatting—grouping related concepts, consistent indentation, short lines, and clear naming—while rejecting trends like functional paradigms.[1]
Why This Style Works
My preferences stem from practical pain points in collaborative and solo development, making his code resilient across environments.
- IDE independence: Explicit types and names ensure readability in any editor or plain text (e.g., GitHub), without relying on IDE tooltips that fade to gray or require hovering.[3]
- Predictable debugging: No exceptions or functional chaining means linear control flow; errors are explicit, and loops are easier to step through than nested callbacks.[3]
- Reduced cognitive load: Detailed names and early returns make intent obvious at a glance, suiting solo work or quick reviews. Limited OOP prevents over-abstraction pitfalls.[3]
- Simplicity scales: By shunning "fancy" features (e.g., interfaces, generics unless essential), code stays maintainable as projects grow, embodying DRY, SOLID (selectively), and Law of Demeter.[1][3]
- Personal productivity: my notes it's "just my preference," tailored to his workflow—e.g., double-clicking to insert types is faster for him than inference, and it aids copy-pasting.[3]
This style shines for small-to-medium projects or libraries where clarity trumps performance micro-optimizations, much like procedural C code that's battle-tested for decades.[2]
Comparison to Standard Practices
| Aspect | Standard Practice | my Style | Benefit for me |
|---|---|---|---|
| Types | Infer where possible (e.g., Rust let x = 5;) |
Always explicit | Visible without IDE[3] |
| Names | Concise (e.g., calcTotal) |
Highly descriptive | Self-documenting[3] |
| Paradigm | Functional/OOP mix | Mostly imperative, light structs | Easier debugging[3] |
| Error Handling | Exceptions/Optionals | Explicit Result types | Predictable flow[3] |
| Principles | Full SOLID, FP patterns | Selective (e.g., no interfaces) | Avoids YAGNI violations[1][3] |
Panchal acknowledges others' styles aren't "wrong," promoting style as personal choice.[3]
Practical Examples from Work
His GitHub gists reflect this: React components use explicit TypeScript files like ComponentName.component.tsx in dedicated folders, ensuring structured, readable organization without inference reliance.[4] For instance, he'd write a function as:
function calculateTotalUserAccountBalance(userAccounts: Vec<UserAccount>): Result<f64, CalculationError> {
let mut total: f64 = 0.0;
for account in userAccounts {
match account.balance {
Ok(value) => total += value,
Err(_) => return Err(CalculationError::InvalidBalance),
}
}
return Ok(total);
}
This explicitness aids maintenance.[3]
Broader Context and Applicability
Panchal's approach echoes clean code guides emphasizing formatting for readability (e.g., no long lines, grouped concepts) and principles like DRY, KISS, and SOLID.[1] Videos on coding styles reinforce consistency as key to better programming.[2] It works best for developers valuing explicitness over elegance, especially in teams with varying tools, but may feel verbose in FP-heavy ecosystems. Adopt selectively based on project needs.
No comments:
Post a Comment