How to Write Code Without Having to Read It

Do we need to read code before editing it? In order to safely fix a bug or update a feature, we may need to learn some things about the code. However, we’d prefer to learn only that information.

JM Neri
14 min readintermediate
--
View Original

Overview

The article discusses strategies for writing code with minimal reading, emphasizing the importance of focusing on critical areas while leveraging build errors and type systems. It provides practical tips for navigating codebases efficiently, particularly in the context of React Native and TypeScript.

What You'll Learn

1

How to identify the entry point of a feature in a codebase

2

Why leveraging build errors can streamline the debugging process

3

How to optimize code for better readability and maintainability

Prerequisites & Requirements

  • Familiarity with React Native and TypeScript concepts
  • Basic understanding of using git for version control(optional)

Key Questions Answered

How can I write code without reading it extensively?
You can minimize reading by focusing on user-visible strings and leveraging build errors to guide your edits. By identifying the entry point and using strategies like git bisect for regressions, you can navigate the codebase more efficiently without getting lost in unnecessary details.
What are tagged unions and how are they used?
Tagged unions, also known as discriminated unions or sum types, are a way to define types that can hold different structures. They help enforce type safety by ensuring that only valid combinations of values can be passed, thus reducing runtime errors and improving code clarity.
What should I do if I encounter build errors?
When facing build errors, you should climb up the callstack and edit the caller as if you will receive the correct input. This approach allows you to identify where the errors originate without needing to read through all the code, focusing instead on the errors themselves.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Focus on user-visible strings to find the relevant code quickly.
This method allows you to bypass unnecessary reading and directly locate the code that impacts user experience, which is crucial for efficient debugging and feature updates.
2
Utilize git bisect for regression tracking.
By using git bisect, you can identify which commit introduced a bug without needing to read through all the code changes, saving time and effort.
3
Implement tagged unions to enforce type safety.
Using tagged unions can prevent invalid data combinations at compile time, reducing potential bugs and making your codebase more robust.

Common Pitfalls

1
Relying too heavily on comments instead of tests can lead to stale information.
Comments may become outdated and not reflect the current state of the code, whereas tests provide real-time feedback on code functionality and correctness.

Related Concepts

Code Refactoring
Type Safety
Debugging Techniques