Linter to the Rescue

I’m currently using the Atom text editor.  I’ve been trying to stay text-editor agnostic, figuring that I will have plenty of time to optimize later.  But lately I’ve been hearing that Vim is worth learning.  This thoughtbot post pushed me over the edge, and so this weekend I gave it a shot.  I’m still nowhere near ready to use it all the time but working on it.

So tonight I started working on learning to use React with a Rails app.  I decided to start building in Atom at first, since that’s what I’m used to.  After a while though I figured I’d try Vim.

Of course, I jacked up the spacing on the file I was working on, and gave up (for tonight).  I went back into Atom and started working.   I edited the component I was working on, refreshed the browser, and:

ExecJS::RuntimeError  SyntaxError: [stdin]:5:10: unexpected . (ExecJS::RuntimeError)

The error was coming from layouts/application.html.erb, which was all auto generated code.  Wth?  I looked back over all my code, looking carefully at the indentations of the coffeescript.  Everything, line by line, was correct.

Luckily, Atom has a package manager and so it was easy to install coffee-lint. Sure enough:  I had added a line that somehow got pushed down below the code I was looking at.   Since the whole file was only a few lines long, I didn’t think to scroll down.  But the linter found the error immediately.

So… Atom 1, Vim 0.  I see that Vim has some linters available though so I’ll be giving it another shot.

Angular and Rails

This is a list of resources for building Angular frontends with a Rails Backend.

I’m hoping to do a hangout on codebuddies.org sometime in the next couple weeks to work through one of these tutorials.

I’m also interested in using React with Rails, so I’m bookmarking a few tutorials for that here as well:

Ruby Array.zip

Learned a neat little trick today:

>>arr1 = [1, 2, 3, 4]
>>arr2 = [5, 6, 7, 8]
>>arr1.zip(arr2)
=> [[1,5],[2,6],[3,7],[4,8]]

Made me smile. It turns out you can use this to iterate over two arrays at the same time and execute a block of code. In JavaScript I’ve gotten used to using a for loop and then indexing using the iterator:

for (i = 0; i < arr.length; i++) {
if arr1[i] == arr2[i]
//do something...
//close brackets etc

But Ruby doesn’t really have a for loop that works that way. Array.zip() to the rescue!

>>arr1.zip(arr2).each do |x, y|
>>if y == x+4
>>puts "awesome!"
>>end
>>end
"awesome"
"awesome"
"awesome"
"awesome"

Everything looks like a nail

I finished the code kata I blogged about yesterday.  I won’t share the solution since others might want to try it.  But it’s interesting how my thought process was shaped by the challenge to try not to use a nested loop.  Because of this, I didn’t think about the simplest solution, or the most efficient solution.  I thought about how I could do it without using a nested loop.  I approached it as a coding challenge rather than a math challenge, which is what it actually was.

And once I solved it as a math problem, rather than as a coding problem, the complexity dropped from exponential to linear.  So the code was improved by stepping outside of the coding domain.

Benchmarking my Code

I joined Codewars.com last week and I’m a little addicted.  The coding challenges are a good motivation to learn new methods and techniques.

I’ve been working on this kata today.  The challenge states “props if you do it without a nested loop,”  so I tried a few different ways.  After a little noodling around, I decided on the strategy of creating an array of arrays, passing a block to Array.new which would insert the proper values as the array was created.  Then Array.reduce would sum it up for me neat and clean.

I had trouble coming up with the code to initialize the arrays though.  I tried Array.new(weeks) { Array.new(days) } but couldn’t figure out how to increment properly as I went along.  So I just started hacking away, keeping the original idea of creating an array of arrays, inserting the values, and then using reduce at the end.  Here is my first function that passed the tests:

with a loopAs you can see I ended up with a nested loop.  But the tests passed, so I hit submit!

Apparently there’s a cutoff time.  It took more than 6 seconds to complete the tests to pass submission.  So I tried benchmarking the code:

benchmarking benchmarks with a loopHoly crap!  Look at the jump in time.  I know enough about Big O to know this is not good.

So I simplified a little bit:

Screenshot from 2015-11-01 18:44:17No need to create all those arrays, so I just pulled them out.  The results, with the same parameters passed to Benchmark, were:

Benchmarking the sumMuch better.  But although it’s passing the first round of tests, it’s still not passing the 6 second cutoff time for the submit tests, so I’ll keep trying.

Learning C the Hard Way

I’ve been kind of scattershot in my learning lately.  I’ve been unsure as to what direction I should go.  I’ve dabbled in Angular and Meteor.  I tried a C++ course through Coursera and downloaded Visual Studio but I was turned off by the massive download and new environment.

I went to Meteor Spacecamp this weekend.  I’ve been working on new features for the app I built for work.  I’ve been thinking through a new app for work and made a couple of tries at that.  I’ve also been trying to deploy an existing app as a volunteer project.  That one’s hard.

As far as learning goes, I’ve decided to settle on learning C for now.  I know it’s a language that will be replaced by Rust or some other language and there’s no real reason to write code in it, but it seems like a good opportunity to get a deeper understanding of how code works in general.  Ruby is written in C.  So is PHP.

So I’ve started Zed Shaw’s Learn C the Hard Way, and I’m really liking it!  I’m up to chapter 15 on pointers.  I plan on completing this before I move on to something else.

Why All the Doors in Star Wars Slide Open

Take a look at the poor robot trying to open the door in this gif:

poor robot

Obviously, this is why all the doors in Star Wars slide open, so you don’t have to teach robots to turn doorknobs.  Right?

This got me thinking about sliding doors.  Why are they so prevalent in science fiction?  Is it purely for the visual effect?

darthmaul

But there are real world examples of sliding doors.  Retail stores, especially grocery stores, pretty much always have sliding doors.  So do elevators. So there must be some purpose to it.

Some reasons why you might use a sliding door:

  • to control access, as with elevators
  • to regulate traffic – you can only enter as fast as the door opens
  • to facilitate traffic – people don’t have to slow down to open the door
  • to control temperature or moisture – a door can’t be left open to the outside
  • to allow people to pass through even with their hands full
  • because it looks cool

use the force

What does this have to do with web development?

I think there is some lesson in here about design, and about visual effect.  Something can look cool, and also be useful.  It could also be a little excessive.

It’s inspired me to look at things in the real world a little more though.  Before today I hadn’t thought about why we use sliding doors and why we don’t… but it turns out they can be pretty useful.

It makes me wonder what other things in the real world and on the web, have been so carefully designed that I don’t usually even think about them?  What do we put on our websites that actually increase functionality – and what is there just to look cool?

The MEAN Stack for Rubyists

I’ve been hearing about the MEAN stack for a long time now.  I heard what it stands for:  Mongodb, Express.js, Angular, and Node.  I also heard that it was roughly analogous to the LAMP stack (Linux, Apache, MySQL, and PHP).

Even after learning JavaScript and Angular, and using Mongodb a few times, I still didn’t quite understand the MEAN stack.  But this morning I read this excellent article that laid it out in a way I understood instantly.

Mongodb is the database

Express.js is a framework like  Sinatra

Angular is the templating framework (like erb)

Node is the server

NPM is like Bundler.

Easy enough!

Brakeman and Rails Security

I had the chance to listen to this talk today:

It’s very good.  On my way home from work, I listened to a Ruby Rogues podcast on Brakeman.  So, naturally when I got home I installed Brakeman and ran it on my reporting app.

I’m happy to report that the only vulnerabilities reported were due to the version of rails I used, and not my code.  But then, after watching the video, I wonder what that’s worth?