Architecting Scalable and Usable Web Applications

Here are my notes from a Microsoft ArcReady seminar I went to in the middle of May. I went because I wanted to know more about building web applications that scale to many users. I definitely learned the basics of this, and found some references to other work as well.

At least one interesting thing that I learned was ProtoXAML, which is a way to take prototypes and make them seem a bit more flawed so that customers are more likely to give you good feedback. It was interesting to see this perspective, and might be helpful with some client interactions we have.

Java Anti-Pattern

I was doing some Java programming today, and I noticed an anti-pattern that was in an existing code base. It went something like the following:

public Class Klass {
    ...

    public Klass(int foo, boolean flag, String s1, String s2, String s3, String s4) {
        initialize(foo, flag, s1, s2, s3, s4);
    }

    public Klass(int foo, boolean flag, String s1, String s2, String s3) {
        initialize(foo, flag, s1, s2, s3, null);
    }

    public Klass(int foo, boolean flag, String s1, String s2) {
        initialize(foo, flag, s1, s2, null, null);
    }

    public Klass(int foo, boolean flag, String s1) {
        initialize(foo, flag, s1, null, null, null);
    }

    public Klass(int foo, boolean flag) {
        initialize(foo, flag, null, null, null, null);
    }

    private initialize(int foo, boolean flag, String s1, String s2, String s3, String s4) {
        _foo = foo;
        _flag = flag;
        _s1 = s1;
        _s2 = s2;
        _s3 = s3;
        _s4 = s4;
    }

    ...
}

Immediately, my Don’t Repeat Yourself sensor went off. It felt like the previous coder was essentially trying to use some default values for a method. You can do something like this in other languages, even other statically typed ones. In Ruby, it would be pretty simple:

class klass
  ...

  def initialize(foo, flag, s1 = nil, s2 = nil, s3 = nil, s4 = nil)
    ...
  end

  ...
end

However, I looked online and saw several instances of people saying “you can’t do that in Java.” So how else can we reduce this duplication?

One way that was suggested was to make better use of the object itself:

public Class Klass {
    ...

    public Klass(int foo, boolean flag, String s1, String s2, String s3, String s4) {
        _foo = foo;
        _flag = flag;
        _s1 = s1;
        _s2 = s2;
        _s3 = s3;
        _s4 = s4;
    }

    public Klass(int foo, boolean flag, String s1, String s2, String s3) {
        this(foo, flag, s1, s2, s3, null);
    }

    public Klass(int foo, boolean flag, String s1, String s2) {
        this(foo, flag, s1, s2, null, null);
    }

    public Klass(int foo, boolean flag, String s1) {
        this(foo, flag, s1, null, null, null);
    }

    public Klass(int foo, boolean flag) {
        this(foo, flag, null, null, null, null);
    }

    ...
}

This at least takes away the initialization method. I suppose you could make it shorter by having each constructor call the one immediately prior. You would remove a few nulls here and there. However, there is a deeper problem here, which was demonstrated in the code I was actually working with. What if we wanted to add even more strings? Why not just store the strings in an array? But if we have one string, do we need to store it in an array?

Ruby has a solution for this problem–the splat (*) operator:

  def initialize(foo, flag, *rest)
    foo = foo
    flag = flag
    strings = *rest
  end

If the rest parameter is nil, then strings will be nil. If the rest parameter is just a single value, like “hello”, then strings will be [“hello”]. It follows that if multiple values are passed in, then strings will contain an array of those values.

My first instinct was that Java did not have a construct that would allow us to do the same thing. However, a search revealed that there was an operator in Java that would allow us to do something like Ruby’s args or splat. Enter the ellipsis (…) feature, first released in Java 1.5:

public Class Klass {
    ...

    public Klass(int foo, boolean flag, String... strings) {
        _foo = foo;
        _flag = flag;
        _strings = strings;
    }

    ...
}

Ahh, that makes me happy. We went from about twenty lines to five with a single trick. Now, if there is some reason that only four strings can be allowed, we can do some additional parameter checking, but it seems like this is considerably more robust already. Like in Ruby, the varargs feature can only be used at the end of the method signature.

There were other parts of the codebase that had something like:

public Class Klass {
    ...
    public Klass(String string1, Shape shape1, String string2, Shape shape2, String string3, Shape shape3) {
        ...
    }
    ...
}

Where each String refers somehow to the associated Shape. In this case, you could just use a HashMap with generics, as in HashMap<String, Shape>.

You probably already knew all of this, but I had never used the varargs feature before in Java. Hope this helped.

22 idea street

I have a variety of interests and opinions, so as you might imagine, I struggled to come up with a name that would represent my true hopes for this blog. It seems like a permanent enough thing, being on the web and all. I took an unorthodox approach to generating the name that seems to have had significant positive effects. I took all of my personal writings for the last year or so, coded up a Ruby script that would calculate and sort the word frequencies, read through the list, and then took a nap.

My subconscious worked on it for awhile, and when I woke up, I asked myself what the correct name should be. A couple of strange hazy names came to mind, and then in literally a flash of insight, ‘22 idea street’ popped into my head. It immediately seemed correct, although I didn’t necessarily understand why. I excitedly woke up and wrote it down.

After thinking about it for a few minutes, each of the parts actually represents something that is deeply important to me.

Let’s start with 'idea.’ It represents novelty. Whether this means truly new and original ideas, the ability to apply newer practices to old problems, or to seeing a general solution to problems that others have been solving inefficiently, it is the essence of creating things that are truly valuable. Ideas have a great deal of energy, and are the driving force behind all great or inspired creation. When the going gets tough, you need to have the original vision of the idea clear to have the motivation to keep going.

'22’ is something that is specific. In this equation, it is the physical manifestation of the idea. It springs forth naturally from ideas that are truly founded with the end value in mind. No idea, no matter how interesting, has any value unless it is concretely implemented. The idea of fire doesn’t give you warmth or food. Building a fire does. But although it is one of the outputs in the process, it is not the end in itself.

The 'street’ represents people. A creation is worthless unless people desire to interact with it. Things must be appealing functionally as well as aesthetically. No one on the street is going to visit a booth if the presentation is unsightly, and few people will tolerate something that doesn’t resonate with them at a deeper level. A busy street captures a great deal of human interaction. It is inherently a human thing. If you walk along a street or visit a crowded street party, you can feel the energy and silent and verbal communication that takes place. The street is fundamentally about people interacting with their world and each other.

Both the '22 idea’ and 'idea street’ portions indicate that ideas are related to one another, and consistent thinking, analysis, and innovation spur other ideas. Hence, a multiplicity of ideas is positive, as long as the ideas that are given time are important in the long run. Every experience you have shapes you indelibly, so actively seek to cultivate interesting ones. Continuous learning and deliberation assist you in every area of your life, even when you don’t apprehend it at the time.

The entirety of the idea '22 idea street’ means creating value from ideas while never forgetting the people that produce and use that value. It’s about balance and taking the time for recreation (or as Thoreau would say, “re-creation.”) It’s about realizing that you only have one life to live, and then it’s over, so you must say “yes” to the right things, and an assertive but peaceful “no” to everything else. It’s about investing the time required to do the job excellently. It’s about taking pride in your life and managing your future with your heart as well as your brain. It’s about thinking outside of the box to come up with better approaches to solving the world’s problems without making new ones. It’s about having meaningful and open relationships with all of the people that you come into contact with, because they are the neighbors on your street in life. And finally, it’s realizing that by nurturing these relationships and placing trust in others, a whole greater than the sum of the parts can emerge.

By understanding these things that are important to me, I have a good guideline for living and growing. These beliefs are hardly immutable-I expect them to change. Nonetheless, writing them down is an important step of living with a purpose. It’s tough to live well before you enumerate what you are living for. Although some of my future posts might not have quite the same passion, gravity, or verbosity, I hope to get there with consistent effort. Hopefully you will join me in my journey. :)