I know what you’re thinking “double the amount of code?! Who is this nutjob.” And “you’re the reason a clock app takes 20MB.”
But, let's get one thing straight.
Lines of code have never been a good metric for code quality. Ever.
Lots of developers want to skill up and abandon their old ways. Expanding your repertoire of approaches and techniques to eliminate branching is one quick way to do so. Often, your code also becomes cleaner in the process.
Crazy code like the above example needs to go. It’s not readable. It’s not maintainable or flexible. It’s just terrible…
I’ve applied many different programming styles, practices, and approaches to write more readable code throughout the years.
Some initially seemed great but turned out not to be. Others got outdated, and then there are the few practices that I’m still using.
I’ll just enumerate a list of those that have stuck with me and which, in my opinion, works well.
You do need documentation comments, but most comments are completely unnecessary.
There are many different “kinds” of comments that only increase the noise-to-signal ratio.
Here are some of those typical code comments that you might as well just delete on…
I heard about CRUD back when I started learning programming and web development. It was pretty much the approach to use. I’d be spending time thinking about entities as ‘resources’ and how to write awesome CRUD-based applications, where each resource was either created, read, updated, or deleted, and only that. Essentially what’s best described as ‘CRUD-thinking.’
Create, read, update, delete. Simple enough — and that’s exactly the problem.
When did you last think about your domain solely in terms of storage management?
But I can see why beginners like this approach. The CRUD idea translates well into existing programming concepts…
There comes a point when unit tests and mocking just won’t cut it.
Writing unit tests with a mocked database may be great when you want to verify that a piece of code runs correctly, given some expected input.
Using an in-memory provider is the natural next step. But while an in-memory provider is useful, it’s only a hollow shell compared to the real thing. For example, queries that succeed when testing with an in-memory database may fail hard when executed on the real database.
In-memory databases provide you with a false sense of confidence.
You’ll eventually need to be…
I often take jabs at curly braces app coders with no appreciation for reducing complexity, thinking any problem — without side-effects — is solved by piling onto already incomprehensible
But, I never really got around to explaining my underlying reasoning and guiding principles — besides that, traditional branching is equivalent to hardcoding and looks nasty.
Sidebar: I want to keep this practical. I won’t go all academic on you. I’ve provided links at the end if you’re more interested.
If easily comprehensible, extensible, maintainable, and working code is the pinnacle of software development, then why do…
Code often gives rise to “WTF”-moments whenever the reader doesn’t understand a piece of code’s purpose, goal, or the code is overly verbose.
I think you know exactly what I’m talking about.
Reading code, understanding the syntax, patterns applied, etc., is no issue. But, you still wonder, “wtf” is going on here? Why did he write it that way? What’s that conditional checking, and why? The list of questions goes on.
Clean code is all about communicating your intent as clearly as possible.
Sometimes that means writing more code and other times less code. But, most importantly, your code should…
One question lots of beginner developers often ask is “is unit testing worth it?”, “Why should I unit test”, or see them make claims like “testing is a waste of time”.
Guess what the very first thing any beginner developer does whenever they’ve completed a feature, say, created a repository class and used it in a web controller action.
You guessed it: they make a request to the endpoint using postman or thru the browser. They’re testing to see if the repository is actually returning what they’re expecting.
Primitive types don’t convey any meaningful domain knowledge — so don't build your domain classes solely with these types.
Obviously, you can’t avoid strings (though, technically not a primitive type), ints, doubles, and other primitive types entirely. They’re the fundamental building blocks for any piece of code and application. But, you can make an effort where it matters.
Neglecting to write proper domain classes and value objects is a recurring theme of many codebases.
Let’s take a look at how you can write better classes that properly capture domain-specific knowledge and business rules, resulting in safer, more flexible code.
You’ve noticed structuring code isn’t that easy. Starting from scratch often begs the question “where should this class go?”. Even the simple act of naming a solution, module, project, or what have you, can be mindboggling difficult.
It’s very likely you’ve already read tons of articles on this matter. You get different answers every time. This is great, as it broadens your repertoire of possible approaches, but, at the same time, it poses new questions. Also, having lots of options may even hinder your decisiveness.
Great, you’re in a hurry, I get it. Here’s how I typically structure my code…
Attending meeting-filled days wearing blue-light glasses, with a pizza slice in one hand and a dumbbell in the other, striking a perfect balance of endorphins and staying healthy, you wonder how the best developer ever gets any work done.
The secret is, he knows coding is only the last-mile to any solution.
We all know one engineer who seems to have the answers to all and everyone’s questions — or, more importantly, knows how to dig out answers.
The one who knows exactly how to deal with that difficult issue you’re facing, or, can elegantly model a complex domain and…