Respond in blue ink: Requiring Tools in Computer Science

This semester I’m taking several electives to finish up my degree. One of the courses I’m taking is Object Oriented Programming Systems, formerly Object Oriented Analysis and Design. I have yet to find a suitable explanation as to why OOaD was cut and reformed at the graduate level, but I’m assuming it was because it was a bit too advanced for some many of the people I find in my major. In its stead, we now have a much more aptly abbreviated course.

In any case, my professor is a big proponent of Java, the Waterfall [[Software Development Life Cycle SDLC]] (which he dubs “the generic SDLC.” I wasn’t aware wet sand was considered “the generic foundation”) and, of course, all the misery that comes with it.
For one of our more recent homework assignments, we were given the trivial task of converting a four Java classes from a previous assignment into UML diagrams. We were obviously applying what we learned in class by putting the cart in front of the horse. Our assignment was to be done in [[Jude (UML Tool) JUDE]] of all things. In the interests of full disclosure, I am completely spoiled in terms of tools to use. Microsoft, through its godsend of [[MSDNAA]] makes Christmas just about every day of the year for me. Sure I know it’s a form of product marketing – analogous to giving crack to kids – but have you used their stuff? It’s head and shoulders above everything else, and in many cases is the paradigm to which the industry only seeks to make parity with. With that being said, I’m hoping you’ll understand my… disposition against Fischer Price’s JUDE product. Instead of trudging through the installation of a clearly inferior product, ripping my hair out trying to understand the UI, and eventually producing a diagram that my 5-year-old cousin could have drawn better, I decided that I would use Visio instead. I made my diagrams down to the letter and they looked beautiful. I spent extra time making sure that the positioning of each class reduced the nmber of times connections intersected and made especially sure that everything was clear. I saved it out as a PDF, so as to be platform agnostic, and submitted it.

I wasn’t nearly as surprised as I was livid when I found out that I had been deducted 50% credit on the sole basis of, “it’s not a .jude file.” That is the kind of arrant pedantry up with which I will not put. To me, it’s really the equivalent of me asking you to write a paper on Shakespeare in blue ink. I’d suppose a lot of people would remark, “well, I write in blue ink anyway, so it’s not a big deal,” and continue on. Still more would say, “normally I write in black ink, but I’ll comply because it seems like a trivial request, and not worth the fight.” This would probably leave me by myself stating emphatically that it is unreasonable to be concerned with the color of the ink that I write in juxtaposed against the magnitude of writing a paper on Shakespeare. Of course, I’d comply if the reasoning for the requirement of blue ink was enumerated, so long as it wasn’t arbitrary.

Perhaps even more important than the freedom aspect of the scenario is the abject insanity that revolves around diagrams. Who cares if your diagram was written in Visio, JUDE, Rhapsody, or on a whiteboard? It’s a diagram. The concept of [[WYSIWYG]] is almost an understatement. It might be argued that JUDE is an “enterprise application” and my good professor is trying to do me a favor by showing me a tool that’s used in the “real world.” I don’t think I would ever want to work for an employer that says, “We see  UML on your résumé, but while we think you’re smart enough to work on our codebase, we don’t think you’re smart enough to learn to use the UML tool we have.”

I think it’s more important to state the objectives of any assignment, be it professional or educational, and let the person doing the work choose the best way to accomplish it. Nobody likes a micromanager. Especially me.

New Logo

Marlene just finished the next iteration of my logo. I really like it!

My old logo
My old logo
My new logo
My new logo

Browser Compatibility and the Sanity of Web Developers

Since I’ve decided to get this blog rolling, one of my primary objectives was to ensure a consistent, high quality experience for any platform visitors come from. Obviously, it’s impossible to test the myriad of browsers out there, but I think it’s useful to at least test on the top three engines, namely Trident (Internet Explorer), Gecko (Firefox), and WebKit (Safari / Chrome). I also test on Opera, even though it has little market share. Hopefully I’ll get my traffic statistics going again soon, so  I can prove more definitively that sixty something percent of the ten people who come here use Firefox anyway.

So that leave three or so of you guys using IE. Well, I hate to break it to you, but your browser sucks. It really does. Web developers everywhere have been rejecting Microsoft’s browser for some time now, which has given rise to the healthy market share Firefox has had.

How does this affect you and me, you may be wondering. Well, let’s do a case study on the site you’re looking at right now, since I’m fixing it up anyway. For those of you not using a W3C compliant browser (read: IE), the website is supposed to have some blocks of color of varying height below each of the navigation links, which highlight to orange and a main body text that wraps around the sidebar on the right.

northhorizon-ff<figcaption class="wp-caption-text">My post about hashing as seen in Firefox.</figcaption></figure>

Beautiful. Everything looks just like it should in Firefox, Safari, Chrome, and Opera. But what happens when we look at it in IE?

northhorizon-ie7<figcaption class="wp-caption-text">The same post as seen by Internet Explorer 7</figcaption></figure>

Oh wow. Not what I was going for at all. This is quite common for web developers to experience: write a website and make it perfect for the W3C compliant browsers and then try and get it to work in IE, because in all honesty, IE makes very little sense in terms of expected results. On another project, I instructed the browser to move an object 6 pixels left and 10 pixels down. Firefox was happy to oblige, but IE only moved it 3 pixels left and 8 pixels down.

So what’s going on here at the root, so we can fix it? Well, if you inspect my CSS, you’ll find this tidbit:

#sidebar {
	float: right;
	width: 250px;
	list-style: none outside;
	background: white;
	margin: 0 0 0 10px;
	padding: 5px;
	padding-left: 20px;
}

Most notably, I’m using CSS floats to put the Sidebar on the right. By doing so, I’m instructing the browser to make some space for it and to wrap text around it. IE obviously doesn’t care about my floated div, so it just disregards the instruction. That leaves me in a precarious position. Should I abandon my floats, which lead to nicer, cleaner XHTML in favor of tables because of one non-compliant browser? Of course not! Instead I have a little function that tells me what browser you’re using called get_browser(). The code looks like this:

$browscap = get_browser();
define('IE', ($browscap->browser == 'IE'));
[northhorizon-ie7-fixed](http://northhorizon.net/wp-content/uploads/2009/02/northhorizon-ie7-fixed.png)
The same post in Internet Explorer 7 with my fixes.

This gives me a global constant, IE that allows me to change how the website looks if you’re running IE. With this in mind, I actually insert in a table in the middle of the page to force the sidebar to stay on the right. Problem solved.

As I was doing all this, I wondered if IE8 would give me any relief. I was somewhat surprised to find that it does, in fact, respect floating divs, and even respects the :after pseudo classes that allow me to add those blocks under the navigation links. Just not the hovering. Sigh. Anyway, all I needed to do was modify my IE code to check the major version number of the browser to say that IE8+ was really mostly W3C compliant.

$browscap = get_browser();
define('IE', ($browscap->browser == 'IE' && $browscap->majorver < 8));

So why is IE this way fundamentally? I don’t have any real code or facts to base these assumptions on, but my speculation is based on working with both XHTML and CSS as well as C# / WinForms / WPF. I assert to you that Microsoft has misunderstood the Web since its inception. With things like ActiveX (now, thankfully, deprecated) it seems like Microsoft has always wanted to bring Windows to the web – literally. In Windows Forms (the battleship gray programs we all used to know), each visual element is a very unique item. Buttons don’t behave very much like Panels which don’t behave very much like CheckBoxes. It follows that you can’t really apply properties that relate to a Button to a Panel. Sensible enough. Unlees you’re dealing with XHTML and CSS. Back when XHTML went 1.0, I was somewhat surprised to see that they had actually removed elements and attributes from the specification. Usually, moving to a new product means that you get more, not less. It took some time, but I eventually realized that the Web isn’t like WinForms at all. Each element on the web (with the exception of form controls like ComboBoxes and CheckBoxes) is really the same. Almost everything can be broken down into a div or a span, and really if you change the display attribute on them, you end up with one amorphous panel.

This is the fundamental difference between IE and the W3C triad. Microsoft insists that floats and hovers really shouldn’t apply to things like divs, because they come from two seperate branches in the object tree. Really, until Microsoft realizes this, throws the existing codebase out, and tries again, IE will never be able to keep up with the rest. It’s a matter of understanding the nature of the Web and using an appropriate architecture to interpret it.

Variable-length Integers

Wow. So it definitely took me long enough to get this blog going. A lot has happened since I got this site up and running, so hopefully there will be no shortage of thoughts to write about.

This semester I’ve finally qualified to take the CS Senior Design Project, which, this semester, is to design a Chord client. At the heart of the system is the identifier, which is essentially a glorified hash. We decided to go with SHA-256, partially because the instructor mentioned it was in the class of his favorite hashes. It didn’t really matter to me, since C# supports it just as easily as anything else.

Identifiers have two essential purposes, comparison and addition, from which every method can be derived. I wasn’t particularly intrigued by either of these topics, since almost without exception, you can rely on existing structures in the FCL to do whatever you need to do, and it’s always better to do so. Now, I said “almost without exception,” namely because I stumbled upon the fact that these identifiers unequivocally are exceptions. The thing about using a SHA-256 hash – or any hash for that matter – is that it’s an enormous value – 256 bits to be precise. There aren’t any real 32 or even 64 bit hashes available (I doubt they’d be useful), so relying on the good ol’ FCL goes out the window.

Other students, stuck in the abyss of Java, were having difficulty getting their socket implementations going, so mucking around in massive integer arithmetic was right out. I believe the professor advised them to take the 32 MSB and just rely on an unsigned integer. (Side note: does Java even have unsigned integers?) I considered a few similar options: taking the 64 LSB or separating the hash into several equal groups of 64-bits each and getting the xor result of the segments. Arrogantly I proceeded on, demanding that whatever I wrote work not only for SHA-256 but also SHA-512 or anything else using the full bit-length.

Comparison wasn’t difficult. Essentially, you start at the MSB in your array (which happens to be at Length - 1) and compare until you find a comparison != 0:

public int CompareTo(Identifier other)
{
	for (int i = _hash.Length - 1; i >= 0; i--)
	{
		int compare = _hash[i].CompareTo(other._hash[i]);

		if (compare != 0)
			return compare;
	}

	return 0;
}

Now comes the fun part. First, I wrote a little method called Pad(byte[] list, int size) which pads the MSB of an array with 0’s until it’s the proper length. After several iterations, I finally ended up with this:

public static byte[] Add(byte[] a, byte[] b)
{
	if (a.Length > b.Length)
		b = Pad(b, a.Length);
	else if (a.Length < b.Length)
		a = Pad(a, b.Length);

	byte[] result = new byte[a.Length];

	int carry = 0;
	for (int i = 0; i < a.Length; i++)
	{
		int value = carry + a[i] + b[i];

		// get the bottom part of the bits
		result[i] = (byte)value;

		// knock off the bottom bits that we already got and
		// put the rest into carry.
		carry = (value - result[i]) / byte.MaxValue;
	}

	return result;
}

The data holder for carry has to be large enough for the worst case scenario 0xFF + 0xFF + carry 1, which is why I chose an int rather than a byte (although a short could have worked just as well, I suppose).

Subtraction was more difficult. Consider the way you subtract, with carrying, etc. Carrying on its own is a recursive process, and I can’t imagine writing it out. It was at that time that I remembered how the ALU does subtraction- by taking the two’s compliment of the second operand and doing addition. So I followed suit:

public static byte[] Subtract(byte[] a, byte[] b)
{
    if (a.Length > b.Length)
        b = Pad(b, a.Length);
    else if (a.Length < b.Length)
        a = Pad(a, b.Length);

	byte[] twoscomp = new byte[b.Length];

	// get the 1's compliment
	for (int i = 0; i < b.Length; i++)
		twoscomp[i] = (byte)(byte.MaxValue - b[i]);

	// add 1
	twoscomp = Add(twoscomp, new byte[] { 1 });

	// NOTE: twoscomp is now the two's compliment of b.

	return Add(a, twoscomp);
}

I also needed multiplication, but I’m still working on the solution. I’ve got a working draft, but I’m not entirely sure I’m happy with it just yet. I’ll keep you notified.

Up and Running

So I got tired of the old site and decided to push ahead plans to get v2 of NorthHorizon up and running. Work will be commencing soon to get a fresh, original theme for the site up, hopefully from a friend of mine.

I’m going to get a post up soon with some really interesting ideas and technologies Chris and I have been working on in WPF and .NET 3.5 and get down to the fun stuff.