Over the past couple of years, I've been trying to increasingly use Object-Oriented Programming (OOP) in my work/code. For the most part, I've done better and better but I know that I'm still nowhere near where a good OOP programmer would be.

One of the things that always tripped me up was getting the 30,000 foot view of OOP sorted out in my applications design and code. Sometimes I did it well, sometimes I didn't (well, more often than not, it's didn't).

I was working on refactoring (sooooo much more to learn on refactoring that it's actually kind of overwhelming) some Flex/ActionScript code in a PureMV-driven application I'm working on. Because I'm still new to PureMVC, I have these bouts of coding where I just throw everything in the component and sort it out into its appropriate location later. Today was one of those days...the sorting.

While sorting through some bad code to make it palatable code, I came across a situation where I had violated the DRY principle (Don't Repeat Yourself). I have several DataGrids in my application that have a date field. The dates returned from the Web Server are raw SQL Server dates (i.e., 12:15 00:23:59) that clearly need to be formatted! Since it's a DataGrid, I was creating a very basic Label Function for the appropriate column in the grid. As I skimmed the code, I realized that I wrote three functions (in three different views) that were nearly identical.

It was one of those, rather obvious, lightbulb moments. I extracted the common elements of the function, created a DataGrid date formatting class, and wrote a method (converting the unique elements of my three functions into parameters) to format the date and return a string.

My AS Class:
package *{
import mx.controls.dataGridClasses.DataGridColumn;
import mx.formatters.DateFormatter;

public class XmlDateFormatHelper
{
private var dateFormatString:String = "EEE MMM DD, YYYY";

public function XmlDateFormatHelper()
{

}

public function formatDateForGrid( item:Object, col:DataGridColumn, dateField:String, formatString:String = null ):String
{
var formattedDate:String = "N/A";
if( formatString != null )
{
dateFormatString = formatString;
}

if( item[ dateField ] != null && item[ dateField ] != "" )
{
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = dateFormatString;
formattedDate = dateFormatter.format(item.ScheduledRecallDate);
}

return formattedDate;
}

}
}

My component labelFunction:
private function formatDate( item:Object, col:DataGridColumn ):String
{
var xmlDateHelper:XmlDateFormatHelper = new XmlDateFormatHelper();
return xmlDateHelper.formatDateForGrid( item, col, "NameOfDateFieldInObjectOrXML" );
}

Where I previously had to maintain three separate labelFunctions on my DataGrids, I was able to create a single object that I could use across all three grids to format any kind of date.

I know this is an overly simplified example of a decent OOP practice but it was just so nice to finally see an OOP solution in the code and be able to execute it, if not ideally.

Comments