Skip to content

Reading Code vs Writing Code: Which Is Better for Learning Programming?

Purpose

This post explores the debate between reading code and writing code for learning programming. Based on real discussions from Reddit’s r/learnjava community, I’ll show you why neither method is inherently better - and how the most effective approach combines both strategically.

The Problem

When I started learning programming, I got conflicting advice from different sources:

Some people said “just write code, write code, write code” while others insisted “you must read open source projects to understand real code.” This confusion led me to waste time on approaches that didn’t work.

Here’s what happened:

I tried writing code first. I spent weeks building small projects but kept making the same mistakes over and over. My code was inefficient, and I didn’t know better patterns.

Then I tried reading open source code. I stared at Apache Commons Lang source for hours but felt like I wasn’t learning much. Passive reading didn’t translate to practical skills.

What the Community Says

I found a Reddit discussion on “Best open source Java projects for me to read?” that reveals the real consensus:

When someone asked “Which open source Java projects would you recommend for me to read?”, here’s what the community said:

  • Key insight from community: “Reading will not teach you much… modify it to do something a bit different” (practical approach)
  • Top-voted comment advocating balance: “Focus on writing code but as you go make sure to read a lot of code”
  • Downvoted comment (-6) claiming “don’t read code, write code” reveals extreme positions aren’t effective

The real-world consensus is clear: Reading provides context, writing builds skills.

The Solution: A Three-Phase Approach

Based on this community wisdom and my own experience, I developed a balanced approach that combines both methods:

Phase 1: Reading Phase - Study Patterns

I study well-structured open source code to understand patterns, conventions, and best practices. I don’t just read passively - I analyze:

  • How do experienced developers structure their code?
  • What naming conventions do they use?
  • How do they handle error cases?
  • What design patterns do they apply?

For example, here’s a method from Apache Commons Lang that taught me about null checks and character validation:

StringUtils.java
// Original code from Apache Commons Lang
public static boolean isNumeric(final CharSequence cs) {
if (cs == null) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}

Phase 2: Writing Phase - Apply Concepts

I implement small projects and exercises to apply concepts learned from reading. This phase is crucial for turning passive knowledge into active skills.

I start with simple exercises and gradually increase complexity. Each project builds on patterns I learned during the reading phase.

Phase 3: Integration Phase - Modify Existing Code

This is the most powerful phase. I modify existing code to add features or fix bugs, combining both methods for deeper learning.

When I studied the Apache Commons code above, I didn’t just understand it - I enhanced it:

StringUtilsEnhanced.java
// Enhanced version after studying the pattern
public static boolean isNumericWithDecimals(final CharSequence cs) {
if (cs == null) {
return false;
}
final int sz = cs.length();
boolean decimalPointFound = false;
for (int i = 0; i < sz; i++) {
char c = cs.charAt(i);
if (c == '.') {
if (decimalPointFound) return false; // Multiple decimal points
decimalPointFound = true;
} else if (!Character.isDigit(c)) {
return false;
}
}
return true;
}

Common Mistakes I Made

Reading Without Modifying Code

When I first tried reading open source code, I just read it passively. I didn’t make any changes. This created passive knowledge without practical skills. Now I always modify the code I read - even if it’s just adding comments or changing variable names.

Writing Code Without Studying Patterns

Early in my learning journey, I wrote code without studying established patterns first. This reinforced bad habits and led to inefficient solutions. Now I always study patterns before implementing new functionality.

Starting with Overly Complex Projects

I tried to work on complex projects that required extensive reading without basic skills. This led to frustration and overwhelm. Now I start with simple exercises and gradually increase complexity.

Applying the Patterns to New Problems

The real test is whether I can apply learned patterns to completely new problems. Here’s an example using the validation patterns I learned from reading:

EmailValidator.java
// Applying learned patterns to new problem
public class EmailValidator {
public static boolean isValidEmail(String email) {
if (email == null || email.isEmpty()) {
return false;
}
// Check for @ symbol
int atIndex = email.indexOf('@');
if (atIndex < 1 || atIndex >= email.length() - 1) {
return false;
}
// Split local and domain parts
String localPart = email.substring(0, atIndex);
String domainPart = email.substring(atIndex + 1);
// Apply patterns learned from reading code
return !localPart.isEmpty() &&
!domainPart.isEmpty() &&
containsOnlyValidChars(localPart) &&
containsOnlyValidChars(domainPart);
}
private static boolean containsOnlyValidChars(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isLetterOrDigit(c) && c != '.' && c != '-' && c != '_') {
return false;
}
}
return true;
}
}

Why This Works

Pure writing leads to inefficient trial-and-error, while pure reading creates passive knowledge without practical skills. The combined approach leverages the strengths of both methods:

  • Reading provides context and shows established patterns
  • Writing builds practical skills and muscle memory
  • Modifying existing code bridges theory and practice

Summary

In this post, I showed how reading code and writing code work best when combined in a balanced approach. The key point is that neither method is inherently better - the most effective programming education combines reading well-structured code to understand patterns, writing code to develop practical skills, and modifying existing projects to bridge theory and practice.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments