Ben Nelson

Software Engineer at Hack Reactor

Read this first

AngularJS Transclusion Explained

tl;dr: Transclusion is easy to learn and easy to implement.

See key points at end of post.

When I was first learning how to create custom directives in AngularJS I was initially intimidated by the term “transclusion”. Just from hearing the word I could make no assumptions about its meaning and I was preparing myself to dig in and spend a significant amount of time learning and mastering a complex topic. To my complete surprise transclusion turned out to be a very simple topic that I learned in no time. For anyone new to AngularJS who has heard this term hopefully my explanation will prove useful.

Consider the following “Hello World” example:

app.js

var myApp = angular.module('myApp', []);

myApp.directive('myDirective', function(){

  return {
    restrict: 'E',
    transclude: true,
    template: '<div>' +
                '<p>Hello World!</p>' +
...

Continue reading →


Closure and For Loops

Note: If you don’t understand Javascript closures then I recommend you first check out this blog post.

When I was interviewing for a job in San Francisco I was asked to fix the following code:

for (var i = 0; i < 5; i++) {
  setTimeout(function(){
    console.log(i);
  }, 100);
}

If you run the above code the number 5 will be printed to the console five times. The problem here is that the closure scope for each anonymous function at each iteration of the loop points to the same variable. They all point to the same “i”. (In fact, the “i” that the anonymous functions point to has a value that wouldn’t normally be printed if this code functioned as it was intended to. The 5 that is printed is the “i” value that caused the for loop’s condition-expression to evaluate to false.)

By using an IIFE, Immediately Invoked Function Expression, you create a “block scope” of sorts that is...

Continue reading →


Javascript Hoisting Explained

A common question that people have when learning Javascript is what the difference is between a function expression and a function declaration.
For example:

Function Expression

var x = function(){
  return true;
};

vs

Function Declaration

function x(){
  return true;
};

The key difference between these two patterns has to do with variable hoisting. Variable hoisting is something that happens when your Javascript is “compiled” by the browser. The browser reads your code and grabs all of your function and variable declarations and moves them to the top of their scope.

Like this:

function(){

  var a = true;
  var b = true;

  var c = function(){
    return true;
  };

  function d(){
    return true;
  };

}();

Gets “compiled” and becomes this for when the code is executed:

function(){

  function d(){
    return true;
  };
  var a;
  var b;
  var c;

  a = true;
  b = true;
...

Continue reading →


Why does everyone do such a terrible job of explaining JS Closures?

While I was preparing for an interview with Hack Reactor I received an email that listed several concepts that I should be comfortable with before interviewing. Some of the topics listed included: callback functions, higher order-functions, and how and when to use anonymous functions. Another topic that was given to me as suggested reading material was an article on Javascript Closures. Because of my inexperience I assumed that the concept of closures was something very complicated that only advanced engineers would understand. It didn’t help that each time I googled for help I consistently came across explanations of closure that would contradict each other.

Here is the simplest explanation of closure that I can manage

A function’s scope remains intact even if it is called after its parent function and scope have closed.

Here’s an example:

var foo = function(){
  var x = "Hello
...

Continue reading →


Create Custom Terminal Commands

I spent some time today building custom terminal commands and I thought I would write about two simple ways to create your own custom commands. Custom bash scripts are a great way to automate redundant tasks. The examples below will give you a simple starting point for developing your own custom bash scripts.

1 - Create a custom command inside of your bash profile

Open your terminal and type:

$ open ~/.bash_profile

This command will open up your bash profile. Inside of your bash profile you can write custom terminal commands using the syntax below.
ex:

to(){
  touch $1  
  open $1
}

Now in any directory you are in you can type the command:

$ to example.sh

This will create (touch) a new file and immediately open it.

2 - Add a custom shell script to your PATH

Open your bash profile. (See above.) If the following line isn’t present inside of your bash profile then add it...

Continue reading →


JS to Swift

For my thesis project at Hack Reactor my group elected to build a native iPhone app using Swift. I was excited to learn Swift because I felt that learning another language and developing for a different platform would be a great way to become a better software engineer. I believe that it has and here’s why:

  • It deepened my understanding of pointers and memory. These concepts are largely ignored in Javascript.

  • It has made me painfully aware of convenient layers of abstraction in Javascript. For example, Javascript arrays can hold any type, they are by default mutable, and they automatically resize when changed and they automatically track their size. Javascript arrays certainly feel luxurious.

  • My understanding of object oriented programming has deepened. In Swift you actually need to declare class values as public, whereas in Javascript you merely add an underscore before values...

Continue reading →


Binary Search Explained

For the uninitiated there are a lot of intimidating buzz words that get thrown around when you listen in on programming discussions. Recursion, hash tables, bitwise operators, and bash commands may seem complicated and mysterious at first but ultimately learning to program is no different than learning any other skill. It starts out difficult and confusing but gradually you begin to understand and eventually master each skill. This blog post is about one of those buzz words that might seem intimidating at first but in reality is a simple and powerful concept.

What Is Binary Search

A binary search is an effective method for searching for a piece of information inside of a huge dataset.

A binary search goes something like this:

Imagine that you are a machine and you are looking for someone’s name in the phonebook. The name you are looking for is “Scott” and for the sake of this...

Continue reading →


Simple Recursion

A significant chunk of Hack Reactor’s pre-course work involves rebuilding several javascript functions that utilize recursion. The first time I saw recursion I had a very difficult time mentally wrapping my mind around it. My brain exceeded its call stack size. After receiving valuable instruction from teachers here at Hack Reactor and having practiced recursion a ton I now understand why recursion is so difficult to grasp. I have also learned how easy it could have been to learn recursion. For any who are confused I hope this helps.

Why is recursion initially so difficult to understand

The reason why so many people have a hard time understanding recursion is because they approach it the wrong way. People will often try to mentally follow their code through each line and through each iteration of the recursive function. This approach often leads to confusion and frustration...

Continue reading →


Hack Reactor - How I Became a Hacker

“Hey Ben, what classes are you taking next semester?”

“Actually, I’m not taking any classes. I’m taking the semester off to go live in San Francisco to go to a coding bootcamp. All of my money will be spent on tuition, I’ll be leaving behind my pregnant wife, and I am going by myself. I also might drop out of college. Oh, and the school I am going to is called ‘Hack Reactor’.”

“…”

It’s 9:30 in the evening and I am at the end of a long day of learning to code. This is my third week here at Hack Reactor and each day preceding today has been spent in a similar fashion. Every day class begins at 9:00am and students routinely stay until after 10:00pm. So far my experience has been one of complete immersion into computer science and programming.

Sometimes a feeling of shock comes over me when I remember how less than a year ago I was hoping to become a doctor. I was at BYU taking...

Continue reading →