Simplify your JavaScript code with normalizer functions


Everyone loves a good if-else tower! What could be simpler than nesting a few conditionals?

if(stuff) {
  if(more stuff) {
  else {

Ok – that isn’t the most amazing thing in the world.

In a simple example like this, it isn’t too bad. But we all know real code is never easy to understand, especially after ten different people have changed it over the course of years!

But what can you do? Sometimes you have complicated logic, and implementing it without a leaning tower of if-elses can be nigh impossible.

Let me show you something called normalizer functions, and how they can help you get rid of some of that complexity.

Read More

4 non-JavaScript library topics to learn to take your skills to the next level


year200x Every new year there’s always a bunch of articles about “Learn this in year 200X!”

But the problem is.. they’re always about libraries. Learn PrototypeJS. Learn jQuery. Learn Backbone. Learn AngularJS. Now it’s Learn React (or some React-derivative)

This stuff comes and goes!

Chances are, your job mostly dictates the libraries you’d use. Even if you do get to pick one, you’ll figure it out with your coworkers based on factors other than “some guy on the internet said we should use this”.

I’m not saying those articles are necessarily bad. They can be a useful look at what’s popular and what are some new interesting up and comers.

What I am saying is there are more useful things to learn beyond the cool library of the year.

Read More

Best practices for JavaScript function parameters


From time to time, people ask things like “should I use objects to pass function arguments”. I’m sure you recognize the pattern, as it’s common with many libraries:

$.post({ a: 'lot', of: 'properties', live: 'in', here: 'yep' })

But I would say this is not actually such a good idea.

Let’s take a look at the best practices for defining function parameter lists in JavaScript.

Read More

5 step method to make test-driven development and unit testing easy


What’s the hardest thing in test-driven development or unit testing in general?

Writing tests!

The syntax or the tools aren’t the problem – you can learn those enough to get started in 15 minutes.

The problem is how to take some vague idea in your head about what you want to do, and turn it into something that verifies some function works… before you even wrote the damn thing!

People tell you to use TDD. But how can you possibly write a test for something that doesn’t exist? I don’t even know what the function is going to do yet – or if I actually want two functions instead of one – and instead you want me to think of a test for it? Are you crazy?

How do all those people who tell you to use TDD do it?

That’s the thing – test-driven development requires thinking of your code in a different way. And nobody ever tells you how to do that.

Until now.

Read More

What is property based testing (and how to do it in JavaScript)?


Ever wonder… what if you didn’t have to write specific tests with hardcoded test data?

Or have you ever written a function which can take many different values as input? How can you test something like that easily with different values?

These are the kinds of important questions in life that keep me up at night! (ok maybe not)

But did you know both of these questions can be solved with something that’s called “property based testing”? That’s right – property based testing can help you test the untestable. And while at it, you can even use them to test your other functions with automatically generated data.

All of this means property based tests are great for finding those pesky corner cases that are easy to forget. This means less debugging weird bugs that you find in production – you know, the really hard to reproduce ones, which occur for only one user in some weirdly specific combination of settings.

Sounds pretty cool huh? Let’s find out some more!

Read More

Staircase code – how short functions are not always the best solution


Have you ever seen code which at first look seems good.. but then as you try to figure it out (maybe to fix a bug), it just feels… wrong?

For some reason the code just feels really difficult to follow – for no obvious reason!

In this post, I’m going to explain one such occurrence that I ran into in a production codebase. The code looked ok, yet it was hard to understand. Why?

Let’s find out. We’ll also look at some examples of how you can avoid and discover problems like this more easily.

Read More

How many tests is too many?


Some time ago I stumbled upon some crazy stuff…

Specifically, I found out that SQLite has 787 times more tests than they have actual code!

It’s no joke, it’s documented right on their website. While they have about 116 300 lines of source code, they have 91 577 300 lines of test code.

That sounds completely insane.

I bet you’ve sometimes wondered what is the right amount of tests to write. In some cases, it’s easy to end up with more tests than code.

But 787 times more tests than code? What kind of crazy nonsense is that?

Read More

The best resources to learn about JavaScript Promises


Promises can be both a blessing and a curse. They were supposed to clean the “callback pyramid of doom”, but instead they can often end up confusing (and I wanted to call that “The callback Temple of Doom”. It doesn’t really make sense in this context, I just like Indiana Jones)

While promises do help, they are a lot more complicated than plain callbacks. With callbacks, you just pass a function and maybe stick another function inside that. But with promises, there’s a lot more.

The chaining can be confusing, as exceptions, normal JS values and promises all affect it differently. Setting more complex flows can also be difficult, as you need to juggle multiple promises and their result values around.

Then there’s the whole topic of error handling. The default behavior with promises is super convenient: Unhandled errors are silently ignored, because apparently the person behind promises hates everyone and wants you to have extra fun debugging times.

But promises are the future. New asynchronous APIs tend to be promise-based. New JavaScript features such as async/await are based on promises as well.

This means the better you know the ins and outs of promises, the better prepared you are for the future – not to mention that promises are very convenient when used well, despite the complexities.

Because of the importance of this, and the questions I sometimes receive which relate to promises, I’ve put together a list of the best promise-related material for you!

Below you’ll find a ton of info on promises, from the basics to super in-depth! 

Read More

Mongoose models and unit tests: The definitive guide


Mongoose is a great tool, as it helps you build Node apps which use MongoDB more easily. Instead of having to sprinkle the model-related logic all over the place, it’s easy to define the functionality as Mongoose models. Querying MongoDB for data also becomes quick and easy – and if you ever need some custom querying logic, that can be hidden away into the model as well.

But how do you test all that logic? When trying to test them, Mongoose models can be a bit daunting at first. Ideally, we’d like to test them without having to connect to a database – after all, a connection would make the tests slow and difficult to set up, because we need to manage database state. But how can we do that?

In fact, Mongoose related questions are some of the most common library-specific ones I get from my readers.

So, let’s find out how we can test Mongoose models. We’ll go in depth with examples for the most common testing situations you’d encounter with Mongoose.

Read More

How to unit test ES6 code?


ES6 is here today. Several browsers have almost all ES6 features available, and even Microsoft Edge is getting there. Tools like Babel help cover the parts which browsers don’t have yet.

If you want to start using ES6 for development, that’s easy: there’s a lot of detailed information available. But what about testing? How do you write unit tests for ES6 code? How do you even configure the testing tools to work with the new features?

In this article, I’ll show you how to configure the most popular testing tools – Mocha, Jasmine, Karma and Testem – to work with ES6. We’ll also go over some best practices for testing ES6 code and writing tests with ES6. You won’t have to neglect testing your ES6 code anymore!

Read the rest of this entry