The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Thursday, March 15, 2007

Hammers, screwdrivers, wrenches and paperclips

From
"Smirk" <{{charles@pickint.net}}>
To
spc@{{pick.net}} (email @ work)
Subject
ip address stuff
Date
Wed, 14 Mar 2007 16:41:49 -0400 (EDT)

Hi Sean,

I like the IP address layout you are using. Could you add some features?

The check box column should have one checkbox in the column header that would toggle all other checkboxes on or off.

-S

I spent the day working on what I thought would be something quick.

The first order of business was to get the webpage (mentioned above by Smirk) to send the information it had back to the server. And that's when I started running into some real issues (and let me just state for the record that so far I'm not a fan of JavaScript). Issues with tool use (specifically, software tools) and user expectations.

The IP files I edit look something like this:

192.168.1.0	00:00:00:00:00:00	Obnoxico & Co. network
192.168.1.1	DE:CA:FE:BA:D0:01	managed switch
192.168.1.2		|		their stuff ... 
192.168.1.3		|
192.168.1.4		|
192.168.1.5		|
192.168.1.6		|
192.168.1.7	FF:FF:FF:FF:FF:FF	Obnoxico & Co broadcast

Nice and easy to read. Vertical columns nicely arranged. But each column isn't nicely delineated in the actual “text.” Here, let me do a “reveal codes” and I'll show you:

192.168.1.0→00:00:00:00:00:00→Obnoxico & Co. network↵
192.168.1.1→DE:CA:FE:BA:D0:01→managed switch↵
192.168.1.2→→|→→their stuff ... ↵
192.168.1.3→→|↵
192.168.1.4→→|↵
192.168.1.5→→|↵
192.168.1.6→→|↵
192.168.1.7→FF:FF:FF:FF:FF:FF→Obnoxico & Co broadcast↵

The right arrows (→) represent the HT character, and the crooked arrow (↵) represents the end of line marker (under Unix, this is a single character LF whereas under Windows, this would be the two character sequence CR LF). And by using HT as a field separator, my columns don't line up logically (although visually, in my text editor, they do).

And that's an issue.

A hard issue.

Oh sure, most people can probably deal with a file that looks like:

192.168.1.0	00:00:00:00:00:00	Obnoxico & Co. network
192.168.1.1	DE:CA:FE:BA:D0:01	managed switch
192.168.1.2	|	their stuff ... 
192.168.1.3	|
192.168.1.4	|
192.168.1.5	|
192.168.1.6	|
192.168.1.7	FF:FF:FF:FF:FF:FF	Obnoxico & Co broadcast

But I can't. It would drive me up a wall. The solution would be to read in each line, expand the tabs to spaces, then pull out each field according to which character columns they're in. That would solve that problem, but it still leaves some other issues.

The last three features Smirk requested I woudn't say are necessarily hard to implement, but getting the user interface right is. Or at least, a user interface I would like (and that would be a minimum number of mouse clicks, because lord knows I don't want to have to check 32, 64 or even 128 individual check boxes to unassign a block of addresses—then again, I'd probably never use the web interface at all so that point might be moot). But what Smirk is asking for is an interface closer to how he works, which mimics a spread sheet (which I touched upon the other day).

And there's still the issue of just how much Smirk or P will update the IP list. My updating the lists hasn't been that much of an issue, and the only reason it's an issue now is the lack of updates to The Company Twiki. (what good is a knowledge base if no one bothers to update the knowledge?) And if the number of times it actually comes up that Smirk has to update the IP list is small, then perhaps writing this piece of software isn't really worth the effort (which is where I'm leaning).

On the other hand, this issue is deeper than just updating a list of IP addresses—at the core, it's a collaboration issue (even if right now it's just between Smirk, P and I) and the fact that Smirk uses hammers, I use screwdrivers and P uses a wrench. And all we have are paperclips (hmmm … that certainly came out of left field).


“undefined” vs. undefined

I had this bit of JavaScript code (never mind that I am compelled to add a semicolon to the end of each line when it isn't needed):

var lines = req.responseText.split("\n")
  
for (var i = 0 ; i < lines.length ; i++)
{
  var row = table.insertRow(-1)
  var fields = lines[i].split("\t")
  var cell;

  cell = row.insertCell(0)
  cell.textContent = fields[0]

  cell = row.insertCell(1)
  cell.textContent = fields[1]
  
  cell = row.insertCell(2)
  cell.textContent = fields[2]
}

I have a text file which I've just loaded. I break it into lines, and then each line into sub-fields, each separated by a tab (this is the format I keep my IP allocation files in), and then stuff the entire thing into a large table.

So, from a text file that looks like this (where I'm showing the actual tab and newline characters as arrows):

10.0.0.0→00:00:00:00:00:00→Backup network↵
10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵
10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵
10.0.0.3↵
10.0.0.4↵
10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵
10.0.0.6↵

and I end up with something that looks like:

Sample output
IP MAC Notes
10.0.0.0 00:00:00:00:00:00 Backup network
10.0.0.1 DE:CA:FB:AD:00:01 mobybackup
10.0.0.2 DE:CA:FB:AD:00:02 dnsserver
10.0.0.3 undefined undefined
10.0.0.4 undefined undefined
10.0.0.5 DE:CA:FB:AD:00:03 mailserver
10.0.0.6 undefined undefined

The undefines pop out because there's nothing else to the line except an IP address, so fields[1] and fields[2] literally have nothing in them, not even the empty string. They are devoid of any value whatsoever.

Okay, fine, whatever.

Now, I go save what I have in the table back to the server. So I write some code:

var table = document.getElementById('tdisplay')
var textfile = ""

for (var i = 0 ; i < table.rows.length ; i++)
{
  var ip   = table.rows[i].cells[0].textContent
  var mac  = table.rows[i].cells[1].textContent
  var note = table.rows[i].cells[2].textContent
  
  textfile = textfile + ip + "\t" + mac + "\t" + note + "\n"
}

And what do I get back?

10.0.0.0→00:00:00:00:00:00→Backup network↵
10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵
10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵
10.0.0.3→undefined→undefined↵
10.0.0.4→undefined→undefined↵
10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵
10.0.0.6→undefined→undefined↵

Oh wait! That's not what I wanted! Okay …

for (var i = 0 ; i < table.rows.length ; i++)
{
  var ip   = table.rows[i].cells[0].textContent
  var mac  = table.rows[i].cells[1].textContent
  var note = table.rows[i].cells[2].textContent

  if (!ip)   { ip   = "" }
  if (!mac)  { mac  = "" }
  if (!note) { note = "" }
  
  textfile = textfile + ip + "\t" + mac + "\t" + note + "\n"
}

And now let's see what I get:

10.0.0.0→00:00:00:00:00:00→Backup network↵
10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵
10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵
10.0.0.3→undefined→undefined↵
10.0.0.4→undefined→undefined↵
10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵
10.0.0.6→undefined→undefined↵

Whoah! Wait! It looks like that doesn't work. Do some reading, oh, I need to use the “===” equality operator instead of the “==” equality operator (shades of Lisp here).

for (var i = 0 ; i < table.rows.length ; i++)
{
  var ip   = table.rows[i].cells[0].textContent
  var mac  = table.rows[i].cells[1].textContent
  var note = table.rows[i].cells[2].textContent

  if (ip   === undefined) { ip   = "" }
  if (mac  === undefined) { mac  = "" }
  if (note === undefined) { note = "" }
  
  textfile = textfile + ip + "\t" + mac + "\t" + note + "\n"
}

And I get …

10.0.0.0→00:00:00:00:00:00→Backup network↵
10.0.0.1→DE:CA:FE:BA:D0:01→mobybackup↵
10.0.0.2→DE:CA:FE:BA:D0:02→dnsserver↵
10.0.0.3→undefined→undefined↵
10.0.0.4→undefined→undefined↵
10.0.0.5→DE:CA:FE:BA:D0:03→mailserver↵
10.0.0.6→undefined→undefined↵

That's not working? What's going on here?

Then a lightbulb goes on. The variable note isn't undefined, it's "undefined", as in, the string consisting of the letters u-n-d-e-f-i-n-e-d. I changed the code used to construct the table:

var lines = req.responseText.split("\n")
  
for (var i = 0 ; i < lines.length ; i++)
{
  var row = table.insertRow(-1)
  var fields = lines[i].split("\t")
  var cell;

  cell = row.insertCell(0)
  cell.textContent = (fields[0] !== undefined) ? fields[0] : ""

  cell = row.insertCell(1)
  cell.textContent = (fields[1] !== undefined) ? fields[1] : ""
  
  cell = row.insertCell(2)
  cell.textContent = (fields[2] !== undefined) ? fields[2] : ""
}

And I finally got what I was expecting:

10.0.0.0→00:00:00:00:00:00→Backup network↵
10.0.0.1→DE:CA:FE:BA:D0:01→mobybackup↵
10.0.0.2→DE:CA:FE:BA:D0:02→dnsserver↵
10.0.0.3→→↵
10.0.0.4→→↵
10.0.0.5→DE:CA:FE:BA:D0:03→mailserver↵
10.0.0.6→→↵

Now, I had originally written this as an indictment against dynamically typed languages, but really, it's not, now that I think about it. C can also have undefined variables, but they're usually pointers, and they usually contain NULL (less rarely, some other illegal address). I think my problem here was a mental mismatch with what was actually going on, which I'm finding is all too easy in a dynamically typed language like JavaScript. Was the variable undefined because it was actually undefined, or was it "undefined"?

Would using a statically typed language helped me here? Possibly. It might have forced the issue of “undefined” at compile time. Or possibly not (on my system, doing something like printf("%s\n",NULL) results in “(null)” being printed, which isn't catching the problem at compile time). But the hiding the issue (or in this case, not crashing) makes it harder to debug such things.

Obligatory Picture

[It's the most wonderful time of the year!]

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: http://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

http://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2019 by Sean Conner. All Rights Reserved.