Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Murphy’s law - of database design

by jim Mar 10, 2012 12:37 PM

I think Murphy’s law is particularly relevant to software engineering, especially if we consider the full statement sometimes attributed to Murphy:

If there's more than one way to do a job, and one of those ways will result in disaster, then somebody will do it that way.

This is true of UI design; if we let the user enter data without validating it, then surely someone will do that. But I’m thinking particularly of database design:

If you design a database such that a programmer can put corrupt data into it, then eventually some programmer will do that.

And it will be mess, when you find out about that bad data later. The obvious rules apply here – use foreign keys to enforce relationships between tables (yes, there are people building databases on MS SQL Server with no foreign keys.)

But there are a lot of more subtle irregularities that can be prevented with a little planning, by applying constraints to columns. What does a NULL mean in that bit field? Nothing? Then make sure it’s NOT NULL. The reverse is true as well – are you using an empty string to indicate that no value has been entered into a field? It might be better to use a NULL for that. The ZipCodeLastFour field shouldn’t allow three digits, or five.

Some might argue that these things are validated in the UI or business layers, and don’t need to be enforced in the database. But Murphy’s law applies – someone is going to write the validation wrong, or the SQL statements that submit the data, or some import process. Figuring out how to fix your data is always a whole lot harder than preventing it from being corrupted in the first place.

Tags:

Code

Embedding the application background image in Flex

by jim Mar 01, 2012 1:34 PM

I was trying to switch to an embedded background image for my Flex application, and this syntax works:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                backgroundImage="@Embed(source='./images/soselec_bg.jpg')"  
                backgroundGradientAlphas="[1.0, 0.0]" 
                backgroundGradientColors="[#E7EEFA, #E7EEFA]"
                verticalScrollPolicy="off">

However, I had fiddler open at the time, and noticed that flash was getting a 404 Not Found, while trying to retrieve an image at http://mydomain/@Embed(source='./images/soselec_bg.jpg'). Fail.

I couldn’t figure out why this was happening, but did find a different way to embed the image, that doesn’t result in the failed request for a file that doesn’t exist. I used the standard embedding format:

[Bindable]
[Embed(source="./images/soselec_bg.jpg")]
protected var BackgroundImage:Class;

Then programmatically assigned the background in my preInitialize handler:

protected function preinitializeHandler(event:FlexEvent):void
{
    Application.application.setStyle("backgroundImage", BackgroundImage);
}

Tags:

Code

Checking for a ReCAPTCHA entry on the client

by Jim Feb 27, 2012 9:06 AM

ReCAPTCHA necessarily posts back to the server for validation of the entered text. However, if no text has been entered at all, then there's nothing to post back for. In ASP.NET web forms, we can use a CustomValidator control to check whether there's anything to submit, and if not, avoid the round trip to the server.

This example uses a ValidationSummary control (not shown.)

<asp:CustomValidator runat="server" ID="valCaptchaEntered" 
  ErrorMessage="Please complete the verification code (captcha)" 
  Display="None" ClientValidationFunction="validateCaptchaEntered">
</asp:CustomValidator> 
<div id="recaptcha_div"> 
<
recaptcha:RecaptchaControl ID="capLogin" runat="server"
PublicKey="[key]" PrivateKey="[key]"
Theme="white" CssClass="captcha" OverrideSecureMode="true" />
</
div>

Assuming you're using jQuery, the client side validation function is just a couple of lines, and looks like this:

// Make sure the user entered something into the captcha, so we don't round-trip to the 
// server if they've left it empty.
function validateCaptchaEntered(source, arguments) { var value = $.trim($("#recaptcha_response_field").val()); arguments.IsValid = (value != ""); }

JSON Parser in AS3

by Jim Jan 31, 2012 9:22 PM

In preparation for creating a JSON parser that could parse directly to model objects, I created this generic parser. I’m not going to use it, because it’s painfully slow (30 seconds to parse vs. a fraction of a second for the built-in parser.) So it goes on the blog  :-P

It’s somewhat simplified from the full algorithm, but it handles basic cases and could be up to the standard with a little more work.

package util
{
   import flash.utils.*;
   
   public class JSONParser
   {
      private var _data : String;
     
      private var _index : int = 0;
     
      public function JSONParser(data : String)
      {
         _data = data;
      }
     
      // get one character and advance
      private function take() : String
      {        
         return _data.charAt(_index++);
      }

      private function assert(condition : Boolean) : void
      {
         if (!condition)
            throw new Error("Unexpected character at position " + _index.toString());
      }
     
      // assign values to properties until we reach a matching end bracket
      private function assignProperties(target : Object) : void
      {
         // we've just entered an object, should be getting a field name first...
         var field : String;
         
         while (true)
         {
            field = "";
           
            assert(take() == "\"");    // field should start with quote
            var char : String = take();
            while(char != "\"")
            {
               field += char;
               char = take();
            }
           
            assert(take() == ":");
           
            target[field] = getValue();
           
            // having got the value, next char should be a comma or
            // the end bracket we're looking for
            char = take();
            if (char == "}")
               break;
            assert(char == ",");
         }
      }
     
      // Next thing in line is the value of a field; return that
      private function getValue() : Object
      {
         // might be a simple type, an object, or an array...
         var char : String = take();

         if (char == "\"")
            return readToQuote();

         if (char == "{")
         {
            // this value is itself an object
            var value : Object = new Object();
            assignProperties(value);
            return value;
         }
         else if(char == "[")
         {
            // We've got an array of objects
           
            var ar : Array = new Array();
            var element : Object;
            char = ",";          // fake to get in loop
            while (char == ",")
            {
               element = getValue();
               ar.push(element);
               char = take();
            }
            assert(char == "]")     // should have been end of the array
            return ar;
         }
         else
            assert(false);
         return null;
      }
   
      // read a string literal until we get to the next quote (eats the end quote)
      private function readToQuote() : String
      {
         var end : int = _data.indexOf("\"", _index);
         var s : String = _data.substring(_index, end);
         _index = end + 1;
         return s;
      }
     
      public function parse() : Object
      {
         var char : String;
         var root : Object;
         
         assert(take() == "{");
         
         root = new Object();
         
         assignProperties(root);
         
         return root;
      }
   }
}

2011 EOS Receiver Hitch

by jim Jan 24, 2012 8:31 PM

A few months ago I got a new VW Eos. I might have purchased the last manual transmission EOS in the U.S. - certainly in the southeast. It’s a shame they discontinued that option in the 2012 U.S. market.

There isn't a whole lot of information out there about bike racks on the American EOS, so I thought I would share my experience and some pictures, for anyone else that's wondering about this.

I got a Draw-Tite Sportframe receiver - this is rated at 200lb tongue weight and 2000lb towing.
http://www.etrailer.com/Trailer-Hitc...eid=2011208575.

For the rack I chose a Swagman XTC-2.

Some things about the install:
I had to jack up the passenger side a bit to get the receiver in place.

On my car, the passenger side bolt spacing is apparently non-standard. I couldn't put the exhaust hanger bracket bolts back into place. I called eTrailer, and they called Draw-Tite, and the spacing on the receiver is correct, at exactly the specified 4 inches. I guess there's some variability between cars, as to the exact spacing of the exhaust brackets. I expanded the rear passenger side bolt hole by about 1/4 inch with a dremel and grinder bit, to make everything fit.

I suggest checking this spacing before fishing the bolts into their holes with the provided wire tool. It’s not real hard to get the bolts into place, but it’s a real pain getting them back out again.

The receiver pushes up the rear fairing by about 3/4 inch. It's not really noticeable, because it's under the car, and it might be by design to keep it from vibrating.

The main bar of the receiver touches my trunk floor - in fact it was "in the way" by about 1/8 or 1/4 inch. I couldn't push the bolt flanges flush against the frame, and had to just torque the bolts down. Again I'm chalking that up to some variability in the car.

Some of the exhaust heat shield does need to be trimmed back, as the installation instructions say. The aluminum can be cut with a sturdy utility knife.

With the middle bar of the rack folded down, there's plenty of room for the top to open.

The bike closest to the car doesn't have a lot of pedal clearance, but the rack is very sturdy and I don't think it will flex enough for the pedal to hit the trunk. I'm going to put an sock over that anyway; I could bang it when putting the bike on the rack.

All in all, I'm very happy with the bike setup. The rack is sturdy, and easier to deal with than the roof rack I had on my Jetta.

Tags:

Tinkering