LogoTRUONG PHAM
Home
Projects
Blogs
YouTube
Contact

Newsletter

Stay updated with technical artifacts and engineering insights.

LogoTRUONG PHAM

Building scalable software and sharing insights on technology & life.

Sitemap

  • Home
  • Projects
  • Blogs
  • YouTube
  • Contact

Connect

  • GitHub
  • LinkedIn
  • Email
  • YouTube

© 2024 TRUONG PHAM. © All rights reserved.

Privacy PolicyTerms of Service
Back
Blog #40: Don't try to be smart – Write code for humans, not for machines
50 FRONTEND LESSONS – HARD-EARNED EXPERIENCES

Blog #40: Don't try to be smart – Write code for humans, not for machines

The story of a 'high-level' code snippet that solved a problem in one line and ended up being a roadblock for the whole team.

TP
Truong PhamSoftware Engineer
PublishedJuly 30, 2024

1. The Context

I was vacationing in Da Lat. The cool air coming into the room felt wonderful. Suddenly, my phone buzzed incessantly. A message in the team chat: "Ivan, the data filter function on the search page is glitching in a special case, and we just can't understand what you wrote to fix it."

I opened my laptop and looked at the function I had written six months ago with all the pride in the world. It was a snippet using "bitwise operators," combined with "reduce" and a mess of "nested ternary operators." At the time of writing, I thought I was... cool. I had used a single line of code to replace about 20 lines of simple code. I thought it was the pinnacle of programming.

But standing in the cool Da Lat air, looking at that very line, it took me 10 minutes to remember how I had "performed" that magic. If the author found it hard to understand, how could I blame the Juniors in the team for being stuck?

2. Foundational Knowledge

There is a classic saying in the industry: "Code is read more often than it is written". You only write code for one hour, but it will be read, edited, and maintained by dozens of others over thousands of hours after that.

"Cleverness" in programming often goes against "Clarity." Clever code is code that utilizes language tricks to make it as short as possible. Clear code is code that even a beginner can guess what it's doing without looking up too much documentation.

3. The Specific Problem

The system needed to filter a product list based on complex user permissions. Instead of writing explicit if/else statements, I performed a "code dance" as follows:

// The "clever" code that stuck the whole team
const filtered = data.reduce((acc, item) => (checkBitmask(item.perm) & user.role_mask) ? [...acc, item] : acc, []);

// And this is why it broke when the logic changed slightly
// A Junior tried to fix it and the result was a memory leak or completely wrong logic.

Juniors often have a tendency to COPY these snippets because they look "Senior." This is the seed of failure in large-scale codebase management.

4. How I Handled It

I had to swallow my ego. I sent back a new piece of code to the team. It was 25 lines long. It used a classic for...of loop, explicit if statements, and had extremely specific intermediate variables like hasPermission, isAuthorized.

I used console.assert to note my assumptions right in the code. Instead of bitwise, I used normal string arrays because they're much easier for humans to read, even if a machine processed them a few microseconds slower.

5. Trade-offs

Writing "for Juniors" has its drawbacks:

  • Length: Longer code, more scrolling time.
  • Aesthetics: It doesn't look "pro"; it looks like beginner code.
  • Performance: Sometimes theoretically slower than deep optimization tips.

Comparison:

  • The "high-level" way: Short, fast execution, but extremely high maintenance cost.
  • The "for humans" way: Long, easy to understand, almost immediate maintenance.

In an Agency or Product environment with constant pressure to change, maintenance time is ten thousand times more important than the "class" of the code snippet.

6. Lesson Learned

I now understand more clearly: The best code is code you can explain to a non-programmer in just 1 minute.

  • Advice: Never write code to prove how good you are. Write code to prove you are a considerate colleague.
  • KISS Principle: Keep It Simple, Stupid. Simplicity is the ultimate sophistication.
  • Self-reflection: Am I patronizing Juniors by writing overly simple code? No, I am respecting their time and my own time in the future.

A Senior isn't someone who knows the hardest functions. A Senior is someone who knows when to use the easiest functions so the whole team can progress together.


Notes on the day I learned to write normal lines of code.

Series • Part 40 of 50

50 FRONTEND LESSONS – HARD-EARNED EXPERIENCES

NextBlog #41: The 2 AM Panic and Infinite Question Marks
Blog #39: The 'Will Fix Later' Promise and the Compound Interest of Technical Debt
35Blog #35: Clean Architecture – The Dream and the Harsh Reality36Blog #36: Touching Legacy Code – When a refactor effort becomes a disaster37Blog #37: The Reusability Trap – When a 'multi-purpose' Component becomes a burden38Blog #38: Micro-frontend Reality Check – When theoretical glory meets harsh reality39Blog #39: The 'Will Fix Later' Promise and the Compound Interest of Technical Debt40Blog #40: Don't try to be smart – Write code for humans, not for machinesReading41Blog #41: The 2 AM Panic and Infinite Question Marks42Blog #42: When the Backend Changes the Schema and the Fragility of the Frontend43Blog #43: CORS Isn't Always the Backend's Fault44Blog #44: CI/CD and Environment Variables that 'Disappear' Without a Trace45Blog #45: The Brutal Difference Between Staging and Production
TP

Written by Truong Pham

Software Engineer passionate about building high-performance systems and meaningful experiences.

Read more articles