Early Returns

It was my very first code review. Three years of college had led up to this moment: my internship mentor would tell me his thoughts about my code. It would be the very first time a professional would judge what I had written. Sure, it might seem like a small thing not even worth mentioning in hindsight, but it felt big back then.

I was confident. I had considered every pattern and principle I had learned in class. I had taken care of code formatting, just like I had to do when writing code with pen and paper during my three-year study in college. There were more comments than code to explain my reasoning, making sure there was no room for confusion.

I started the assignment two days ago, and my mentor had deliberately given me freedom to code without looking over my shoulder. I was building a Google Analytics dashboard — an API integration, basically — for a custom-built CMS. Any of their websites would soon pull in the most relevant data from Google Analytics and display custom rendered graphs on the CMS' dashboard. It was a pretty high-impact feature, and I was honored to work on it.

I was far from done, of course, but the base layer was ready. My code could now synchronize the necessary data between the Google Analytics API and the project's database; the part that my mentor would be reviewing today.

"You know," he said, "I noticed something. Do you only write one return statement in a function?"

"Erm, yes," I replied, a bit surprised this was the thing he mentioned. "I learned you should only ever write one return statement per function to keep it clean," I continued, trying to defend my coding style.

"Yeah," he sighed, "you know, don't pay too much attention to what they tell you in school. That "one return statement rule" is so outdated and no one really follows it anymore. The rest seems good, carry on!"

And that was it, the code review was done. I have to admit, I was kind of taken aback; this was not how I expected it to go. I also didn't know whether I agreed with my mentor or not. I think I personally liked having only one return statement per function. Early returns seemed… ugly to me, in a way; but I couldn't really tell you why exactly.

Nevertheless, I went back to my desk without complaining and got to refactoring my functions to use early returns instead.

That's how I got confronted with one of the few certainties any programmer will really ever have: differences in code style opinions.

Naturally, there is a lot to say about early returns versus one return statement per function. You might even have thoughts about it right now. However, this book won't teach you the definitive answer to that question. Neither will it give any answer to any programming question you might have.

This book isn't about patterns, principles, or best practices; in fact, there's barely any code in it. It's about the many things I've learned along the way being a professional programmer, and about the many, many mistakes I made along that way as well.

It's what I wish someone would have told me years ago, and I hope it might inspire you.

How to read this book

This book is written as a story: my advice would be to sit down somewhere comfortably and read it through. Because it's a story, it's important that you read it chronologically without skipping chapters or jumping around.

It isn't just meant to be a past-time read, though. This book's goal is to make you think and reflect on your own programmer's journey. If you're reading the paperback version, you'll find one or two note-taking pages at the end of each chapter. These are meant for your own story. You could choose to read through the book once without taking notes and then go through a second time to write down your reflections; or do it all at once — up to you!

Yes, I hereby give you permission to scribble in a book.

If you're reading a digital version, my recommendation would be to have a note-taking app or paper at hand. My personal preference is to use pen and paper, but you choose what works best for you.

Enjoy!

Resisting The Urge

My internship marked the end of a three-year programming study in college. It should have been a joyous moment, celebrating the end of school and being ready to dive into real life; no more studying boring courses, no more programming on paper, ready for the real deal.

Instead, I felt confused. This three-month internship had painted such a different picture of what I thought programming was about, and instead of joy, I felt doubt. How sure was I this was actually what I wanted to do the rest of my life?

"Maybe we'll see you again soon?" my mentor asked on my very last day. Between the lines, I understood he meant that I should apply for a job with them, after graduation. And sure, my internship went pretty well: I did what I had to do and even more, and they were happy with me. But I wasn't happy with them — not them personally, but the job in general. I smiled, replied "who knows!", and walked out. I knew very well I'd never want to return. I'm sorry, Wim, for not telling you honestly back then; maybe I should have, and maybe you would have helped me, but I didn't. I didn't want to disappoint.

There wasn't one reason that stood out causing me to not like the job. It just… wasn't what I expected a programming job to be.

I loved programming. I loved the ability to solve problems with code. I loved programming itself. I loved "the art of clean code". Within a company, though, code wasn't what it was about. Code was a means to an end, rather than the goal itself.

In hindsight, that's a very sensible thing to say. But back then, I was on the verge of abandoning code altogether after that internship. Maybe I should study something entirely different. Maybe coding — real-world coding — wasn't my thing after all. One thing was for sure, three years of programming in college definitely hadn't prepared me for this. College was all about learning the right patterns and principles, writing code with pen and paper so that you really got to appreciate the art of writing code; it was algorithms and puzzle solving.

June came around, and graduation was there, and I went into the summer holidays confused. I was seriously contemplating pivoting to an entirely different sector. Maybe graphic design had my interest as well — it was still connected to computers somehow, and I had designed some websites in the past.

Regardless of what I wanted to do next, I had to earn some money first. I found a summer job as a programmer. It was close by, it was a small startup of five people, it paid well — very well, even; and it opened a whole new world. Granted, a startup of five cannot be compared to an agency with 50 programmers working for clients; but this felt so much better.

We were building an app — something related to planning and customer relations, the specifics don't really matter. I was in charge of building the backend REST API. This was my comfort zone, this is where I liked to be: modeling real-world processes with code, without worrying about any UX or UI. While others were building the frontend, I could focus on the very core of the problem, and it was great.

After weeks of designing models and processes, there was still one point that stood out. One part I struggled with, a part I found so boring and repetitive: making the actual endpoints that exposed my backend code to the frontend via an API. Controllers and routes. It was always the same, over and over again. Eventually, my urge for mental challenges got the better hand, and I decided to get rid of the problem once and for all.

That's when I came up with the CRUDController.

Continue reading…