Modular Code

One of my favorite things I've learned at Hack Reactor is the importance of breaking code into small units with clearly defined purposes, which can then interact with each other. This has several benefits.

  1. Easier to reason about your program. It's hard to wrap your mind around a function that does five different things. It's easier to track what's going on in your program when you can envision the flow from one small function to another. It's nice when you can evaluate one part of your program independently of the others.
  2. Code is more reusable. If you keep your functions small and with very clear, specific purposes, it's easy to reuse them elsewhere. A "getsAndFormatsAndSortsChatMessages" function is really specific to one task. A smaller function likes "sortsInputsReverseChronologically" can be used for many things.
  3. Easier debugging. You can quickly find out exactly which part of your program is throwing an error when each little part of the program is labelled with a descriptive function name like sortsMessages instead of being buried in a huge function like getsAndFormatsAndSortsChatMessages.
  4. Easier to split up work. It's better to assign each team member to work on a certain component than to have everyone simultaneously mucking around in one undivided chunk of code.

Backbone

I'm making a simple todo list app to brush up on BackboneJS a little. I had been building my individual task view by appending a string containing li tags and  specifying the class, like this:

<li class="task">this.model.get('text')</li>

Then, I saw that each of these list items was enclosed in a div, which I didn't want. I realized that if no tag name is specified, Backbone makes a div for the view, and that div is the el that you add things to with that view's render method.

It's cool that Backbone lets you follow the Backbone way as much as you want. Even if you don't take full advantage of all of its features, you can still take advantage of parts of it. Other frameworks require more adherence to their ways of doing things.