Categories
Programming

My first `case` statement in 60 months.

The first time flipping through my O’Reilly Javascript Definitive Guide, I was pleasantly surprised to read that Javascript had a @case@ statement. I’m not sure why I was surprised, but I guess I forgot about it considering that I have been programming in Perl[1] and Python for so long.

The other day, I came across a @case@ statement while glancing through some Javascript code from another company. This time I made a mental note to use it the next time that I could.

Well, my chance arrived yesterday while doing a project for work. Our designer had wanted to place bounding corners at certain sections on the page. I was going to position a @

@ at the corner of each section and then turn off the appropriate borders to give it the look he wanted. I had named each @

@ corner using a suffix which described its location (eg, tl – top left) and wanted to think of a way to map that name to the borders which were to be showing.

Here is the incomplete code using a @case@ statement. I think it is more concise and easier to read than if it had been written using @if@ statements.

My First Javascript Case Statement:

this.corner_suffix = ['tl','tr','br','bl'];

for( i = 0; i < this.corner_suffix.length; i++ ) {

  // the first character takes care of top/bottom borders
  switch ( this.corner_suffix[i].charAt(0) ) {
    case 't':
      // set top location, turn off bottom border
      break;

    case 'b':
      // set bottom location, turn off top border
      break;
  }
  // the second character takes care of left/right borders
  switch ( this.corner_suffix[i].charAt(1) ) {
    case 'r':
      // set right location, turn off left border
      break;

    case 'l':
      // set left location, turn off right border
      break;
    }
}

I was happy to have rediscovered the @case@ statement and found that it was very natural to write it once I had remembered the rules for @break@ and @default@. It’s like meeting an old friend: you get along instantly and continue without missing a beat.

fn1. I stopped programming in Perl sometime in 2002. I see that there is now a module called Switch, released sometime in 2002, which emulates a @case@ construct.

Categories
Programming Python

Doing Python-style imports in JavaScript

Lately at work I’ve been doing a lot of programming in JavaScript. Among other things, I have been learning about using the prototype object to do object oriented programming. One thing that I missed was the feature in Python used to import modules directly into other code. For example:

from glorp import blah
import foo

I was happy to have found an open-source project which emulates that functionality in JavaScript! I haven’t been able to do more than skim the web site, but it looks promising.

h3. Resources

Categories
Design Patterns Programming

Design Patterns – The Decorator

Tonight was the second night of my design patterns meeting I hold with some friends from work. Most of us already had the book Head First Design Patterns by O’Reilly Publishing, but never made the time to go through it. Jennifer was the one who came up with the idea to start having a meeting every two weeks and we all agreed that it was a good idea.

We’re currently working on our format, but so far have settled on one person being the ‘teacher’ for the evening. This person is responsible for presenting the highlights of the chapter to everyone even though we have all read it. After they are finished presenting, we then go around the table and share our ideas on how the pattern could be used at work or on personal projects.

Tonight we discussed the Decorator Pattern. Here were my solutions:

Recipe card

In this design, the recipe card is the base object and the decorators are ingredients. As per the design, ingredients would be stacked up to create the ingredient list. Here’s a rough cut at the class diagram.

decorator_class.png

click to view a larger image

Some of the interesting characteristics of recipes is that they often have to be scaled (eg, from 4 to 12 persons) or translated to another type of measurement (eg, metric/english). This functionality can easily be added down at the ingredient level. The different ingredients could also have superclasses based on their type – liquid, solid, etc. This makes it easy to separate scaling and measurement translation as necessary.

Invoice discounts

In this example the base object is an invoice and the decorators would be different types of discounts or rebates. This would allow a sales team to be creative with the types of discounts given to a particular client.

Discount objects could be applied to different products, plus you have them expire at certain times (free for 3 months), also be percentages or flat dollar amounts.

Next time I’m presenting the Command Pattern.