Wednesday, January 31, 2007

Java Practice: When NOT to use enum's

We said goodbye to public final static int's when we upgraded to Java5 some time ago. In it's place, we are using Java's enum type. There are some really good tutorials on using Java enum's (see JavaWorld's tutorial for example), but here are some of the selling points:
  • type safety
  • code completion in Eclipse
  • self documenting
  • valueOf method makes it easy to convert from strings to enum values
  • enums can have internal methods
  • easy to use in collections
  • allows you to store additional attributes with each enumeration.
The last point two points are where enums depart from normal static ints. For example an enum for Shape could have an attribute 'sides'. (Why do developers love using shapes...).

Java's enum's are very much like classes. In fact, they are special classes that are inherited from the java.lang.Enum class. Therein lies the first limitation:

enum's can't extend other enum's since they all extend java.lang.Enum

So you can't create an enum FourSided that extends Shape.

Another peculiarity of enum's is that they are effectively singleton objects instantiated when they are first used. If you're not careful with your design and use of enums (e.g., enum abuse), this can get you in trouble. Specifically, if you use an enum like a full blown class. Here was the scenario (somewhat simplified)

enum Duration {
TwoWeek(dateFunctionTodayMinus(14)), FourWeek(dateFunctionTodayMinus(28)));

Date startDate;

Duration(Date d) { startDate = d);
}

Simplified.... dateFunctionTodayMinus(int X) returns a date of today minus X days.

We then had a class that used these enums. The developer assumed that these enums would get defined at runtime so that Duration.TwoWeek would always represent a date range starting two weeks from 'now'.

Wrong....

Duration.TwoWeek is instantiated at first use. All subsequent uses of Duration.TwoWeek will reflect the dates of when it was first used. Ouch.

The lesson here is that enums are not classes/objects. Enums are good to represent static/singleton objects but should never be used as value objects or have attributes that get set during usage. You can use an enum to 'type/label' a two week duration, but the actual start/end dates should be attributes of a class DateRange.
continue reading "Java Practice: When NOT to use enum's"

Thursday, January 25, 2007

Building Codes for Software?

If you go out and build a house, add a room to your house, upgrade your electrical panel, or pretty much anything in construction, their are building codes to follow, permits to file, and inspectors that review the work. Some of this can be overbearing, expensive, frustrating, and even political but it's all in place to maintain standards of security, safety, and sometimes even efficiency.

What would software and the software industry be like if similar codes, permits, and inspections were in place?

On one hand, it sounds scary. No doubt it would slow down productivity and make software development a lot more complicated, expensive, and probably a lot less fun.

It may also be unrealistic. The software industry is a young one compared to modern construction and the tools, building material (aka, software components), and development processes are still being improved and debated. When is CMM appropriate? TDD, Agile Development, EJB, .Net, ISO. Can you imagine the debate if anyone really tried to develop and enforce software development and coding standards?

But when you see the almost daily announcements on software security defects, data breaches, viruses, worms, privacy issues, performance issues.... One begins to wonder when and how real standards will emerge.
continue reading "Building Codes for Software?"

Friday, January 05, 2007

Jsp Practice: Use Objects As Request and Session Attributes

It's very tempting to set String and other simple data types (Date, Long, etc) as request or session attributes. It's easy to program and a method for passing information from your classes to view objects (e.g. Struts actions to jsp pages).

The problem is, this can become unruly very quickly. Some pages end up with a long list of jsp:useBean's making them long and hard to follow. It's hard to enforce naming conventions. As pages get upgraded, it's difficult to know what attributes have become obsolete. Finally, these attributes often get implemented with little or no documentation. Worse, if you have a chain of actions or multiple paths to a jsp view, it's hard to know what attributes are needed or are available.

There is a simple solution. Create value objects (VO's) and add these to the request (or session) contexts. Then, create a naming convention for these VO's. The VO classes can be used to document the definition and rules around these attributes and house any logic. If you're using Eclipse then the overhead for creating the VO's is very light. Create the VO, add your attributes, then let Eclipse generate the getters and setters.

Simple enough? Yes, but even my team at TripConnect has trouble keeping up with this standard. Why? That's for another blog...
continue reading "Jsp Practice: Use Objects As Request and Session Attributes"