Part I of the series “Code Makeover”

There are many reasons to refactor your code. It might be for better performance, more readability, improved maintenance and scalability, you name it. So, ready to check one of the easiest ways to start refactoring?

Today we’ll focus on what I’m calling the Chanel principle.

The Chanel Principle

Before you leave the house, look in the mirror and take one thing off.
— Coco Chanel

There are lots of trinkets that make our code hard to read. …

Image for post
Image for post
Photo by Sarah Kilian on Unsplash

Sometimes your code needs an array to run. No matter how long or empty the array is, you just need to keep running.

Image for post
Image for post

So far so good.
But let’s suppose your array comes from an external server and you have no control over it. Furthermore, both Malala and Abiy are busy on a Nobel laureate meetup and can’t come to see you. What will happen to your code?

Image for post
Image for post

Yeah, undefined. And if other functions depend on whoIsComing, your code breaks.

Failed with exception:  TypeError: Cannot read property 'XXX' of undefined

How can you fix this, so your code keeps running anyway? …

Part III of “ Weird questions from my first month of coding”

There are several ways to iterate through arrays in JavaScript. They all have one thing in common: they execute a given function in each item of your array; the difference is what you get in return.

Here I assume you know how to use each iterator. …

Part II of “ Weird questions from my first month of coding”

Image for post
Image for post
This is how you brew delicious coffee, folks.

You might know by now that methods are functions in disguise. Some methods like filter require another function to run. So when you use filter, you are actually using a function inside another function (a callback). In terms as broad as the Amazon Forest, a callback is a function inside another function. More precisely, though, a callback is a function that is passed as an argument, but this precise definition was way above my rookie understanding.

Well, read the documentation, duh. Yeah, good idea:

Image for post
Image for post
Hmmm… You sure?

But things don’t need to be that difficult. Let’s translate this to the real…

Part I of “ Weird questions from my first month of coding”

Image for post
Image for post

When I started learning JavaScript, I used to get confused with array iterations. It was quite unclear to me if I was dealing with array indexes, values or both. Do you feel the same? So tag along. I’m taking a bonkers approach because hey, if you’ve read the documentation and it didn’t work, you may well need some unusual stuff.

I assume you know what an array is, how to call an array value, which is the syntax for for loops, and what are JavaScript operators, even if you don’t know how to use them all properly yet.

You finally got your webpage looking exactly the way you want. But… you know people have different devices, right? I mean, your site will look great in any screen size, RIGHT?

TLDR; Section 1 explains how it works, Section 2 explains the syntax and Section 3 shows the code.

If your answer is anything other than a resounding yes, you’re in the right place. Learn more about media queries, a neat and easy way to make your website look precisely as you want for any screen size of your choice.

This is what we’ll create together in this example: as the screen changes sizes, the layout changes too. …

About

Manu Magalhães

Journalist turned techie. Talks about coding in real life | DevSecOps Engineer & School of Code alumna | Birmingham, UK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store