Approaching Iterativity

Initialization

I recently came across an article in which Scott Hanselman responded to a reader who wanted to know how to determine if they were actually a developer or just a good Googler.

Until I read this I hadn’t even thought about it. Not in those terms anyway. I think of myself as a successful developer, i.e. I’ve been gainfully employed as a developer for years. However, I don’t know if I’m actually a developer. I’ve certainly met and read the code of much better developers, people I consider actual developers, so the concept interests me. I’m certainly not a good Googler (mediocre at best) which means I should strive to determine if I’m actually a developer.

To that end, I’ve decided to take the advice Mr. Hanselman kindly offered his reader and work on one of the Code Katas while documenting the reference material I use. Since I’m currently polishing my proficiency[1] with JavaScript à la Node.js that’ll be the software context.

Condition

The kata I’ve chosen is transitive dependencies and, knowing that I want to do data entry via the command line, I hit up Google to see what I can get from Node.js. Stackoverflow, the result for most of my Googling, taught me[2] about the process object as well as some npm modules.

Since I’m just doing a kata, not developing a general, all-purpose solution, I opted to adopt the technique illustrated by this article in the nodejitsu documentation. As you’ll see in the code, I went with the complicated “simple example”.

I should mention that I tend (i.e. rarely fail) to reference the language documentation about everything. For JavaScript I use the MDN. I did go to stackoverflow for some of the object property stuff. Specifically, this question and this question.

Increment

Here’s my first attempt with the code kata. I don’t like the nested loops and I’m sure my JavaScript is sloppy so I’ll be coming back to make some improvements in a later article.

var app = {
  deps: {},
  concatDeps: function(key,a,b) {
    var arr = [];
    var i = 0;
    for(; i < a.length; ++i) { arr.push(a[i]); }
    for(i = 0; i < b.length; ++i) {
      if(key !== b[i] && !arr.includes(b[i])) {
        arr.push(b[i]);
      }
    }
    return arr;
  },

  processLine: function (line) {
    var depData = line.substring(0,line.length - 1);
    return depData.split(' ');
  },

  graphDependencies: function () {
    console.log('Original Chart:');
    console.log(app.deps);
    for(var aKey in app.deps) {
      for(var bKey in app.deps ) {
        if(aKey !== bKey && app.deps[aKey].includes(bKey)) {
          app.deps[aKey] = app.concatDeps(
              aKey,
              app.deps[aKey],
              app.deps[bKey]
          );
        }
      }
    }

    console.log('Dependency Graph:');
    console.log(app.deps);
    process.exit();
  },

  readDataFromCli: function() {
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', function(line) {
      var data;
      var i = 1;
      if (line === 'q\n') {
        app.graphDependencies();
      } else {
        data =  app.processLine(line);
        app.deps[data[0]] = [];
        for(; i < data.length; ++i) {
          app.deps[data[0]].push(data[i]);
        }
      }
    });
  }
};

app.readDataFromCli();

Statement

What I learned by both doing the code kata and tracking my resources is that my reference material didn’t tell me how to accomplish the task. I knew what I needed to look up by understanding the coding problem and my approach to its solution.

Maybe I misunderstood the initial question posed to Scott Hanselman. Was I supposed to use Google to find a solution? That option didn’t occur to me until just now. I’m going to have to work on my reading comprehension, seriously.

– Randy


  1. I’ve written JavaScript (poorly) since the 1990s. I’ve just never had much opportunity to really dig deep since I don’t use it professionally. ↩︎

  2. This question launched the deep dive that ultimately led to the nodejitsu article. ↩︎