Cultural Complexity Squashes Methodology

In a previous post, I theorized that an organization’s culture is the largest obstacle to successfully completing a software development project. This topic really intrigues me for a few reasons. One is that it goes above the methodology argument. Where there are certain camps advocating one approach over another, there are more people sitting in between watching none of them work. In fact, the first “Extreme Programming” project (C3) “failed” due to communication failures between three stakeholder groups; developers, sponsor and users. So although they were able to revamp a failed development project and pare themselves down to an agile group, they still were unable to truly satisfy both the sponsor and the users. But, was it ever going to be possible to satisfy these people? Think about how much inertia must be overcome to make even a slight change in a huge organization such as Chrysler, much less replacing the entire paycheck system.

At the heart of this failure is the simple fact that an organization, much like people, do not always behave rationally (in a utility driven sense). In this case, after four years, C3 came in over budget and behind schedule. Also, after the Y2K scare (turns out that I didn’t need that bunker and canned food after all) Chrysler just went back to using their mainframe solution. The problem here is that at this point, C3 was more than likely in the home stretch and simply waiting for further adoption throughout Chrysler. But ignoring sunk costs, Chrysler cut the program.

Some cultural notes here: if you look at studies of new products, only about 15% of individuals are willing to put themselves on the cutting edge. Imagine how this would work throughout the HR groups of Chrysler. Is the HR director at the Twinsburg stamping plant going to eagerly replace the system that they have been using for years with a new one? Probably not willingly.

Also, knowing that Daimler was looking to purchase Chrysler, I’m sure it was a priority to trim down departmental budgets. In this case, it probably made sense to swallow the sunk costs in expectation of a better financial result.

Finally, apparently the C3 project is underway again, using SAP. Culturally, this probably makes sense. With SAP, your scope is already defined and your options are finite. With a custom-built system, stakeholders can dream up wonderful, contradictory and insane requirements and you can literally implement them all. With SAP, you cannot. Instead they have spent millions of hours accumulating requirements from your industry and have implemented the most commonly needed solutions. I am in no way saying custom development is a waste, but it is not always the best solution given the environment and context.

Leave a Comment

Setting Compiler Level For Maven

For some reason Maven 2 does not have JDK 1.5 set as its default compiler level.  Since I’ve run into this problem a few times, I figured I would document the solution.

This will need to be added to the root POM of your Maven project:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

You can also add this into an “active by default” profile in your settings.xml, located in either your .m2 directory or your install directory.

Comments (2)

What Really Makes Enterprise Software Development Complex

The early phases of an enterprise software development effort typically focuses on limiting, defining and availing the potential technical complexities that obstruct a successful outcome. After the scope and requirements are defined, the architects or leads get together and come up with possible architectural solutions (hopefully). This is generally where the risk-laden components are identified; an RMI component or use of a third-party web-service. And it is generally assumed that this components present the most complexity. Unfortunately, this is only partly true.

After ten years, and hours of conversation with other experienced software people I have come to the conclusion that what makes enterprise level software development so complex is the culture. This cultural complexity comes from all levels of the enterprise and manifests itself in all levels of granularity.

From a high-level, coarse grained perspective, compare software development at Google and at a large bank. Imagine developing software at Google for the past two years (I pick Google because their workspace and culture has received a good amount of press recently) and then making a horizontal shift to developing software for a large bank. Your world would be in turmoil to say the least; your hours, your clothes, your professional acquaintances, your attitude. Where Google’s atmosphere promotes creativity but demands challenging work hours; your bank job would probably cringe at new approaches, but you would never feel crushed by deadlines.

This is not to say that Google is better than your bank, but Google is better at developing software than your bank; they have to be. Google is fundamentally a software company; that is what they do. Your bank makes money. So right there, the culture of the bank is negatively affecting its software development effort.

To counteract this, organizations should reflect their software products. To continue with the bank example, if I work within the “Retail Banking and Branding” department as a developer, chances are I will be working for people that I am certain know nothing about what I do. Furthermore, any decision I see that seems counterproductive to me or my work will assuredly be a sign of my management’s ignorance. However, if I work in a software department, I will give my manager’s more of a benefit of the doubt concerning their competence.

Another, more finely-grained point of complexity is the developer’s role, or more importantly, perceived role in the organization. One of the strengths (although it can quickly turn to a weakness) of agile methodology is the amount of autonomy given to developers. In order to develop rapidly, much of the oversight and pre-conceived detailed design is handed off to the developer. In my estimation, this is a much better approach for two reasons. First, this responsibility allows the developer to become creative, test themselves and advance their skills much better than simply transcribing a designer’s UML diagrams. Second, those UML diagrams usually fly out the window after three hundred lines of code.

Of course, the drawback to this approach is that your newly energized developer is coding in circles, recreating the wheel, or simply failing to provide an adequate solution. These risks are very real and extremely likely even with experienced developers. By letting a developer go sit at their desk for days with a goal and an open, green field in front of them will almost always ensure that you get a very creative, elegant solution to a problem that you don’t need to solve. Even if you catch this divergence early, it is unlikely that the existing code will be deleted. Rather, at least some will remain, influencing the future design and elegance. The resulting code will be like a house built on slanted stilts.

How can this cultural complexity be overcome; how can you harness your developer’s drive to succeed and become a better developer without unleashing a bull in a china shop? Code and design reviews early and often. Have them propose their solution to other developers and see what happens. Once the early design is defensible, it will be harder to stray of course. But as you can probably imagine, this is much easier written than practiced. Again, culture plays an enormous role. These reviews can be confrontational, developer’s do not appreciate their code, design, intelligence being questioned. One way to tamp down the tension is through leadership; perhaps you show your design first. Also, be sure to moderate fairly but forcefully. Comments should be strictly constructive, but pay attention to body language as well. Another pitfall could be cliques within the team; they may seem positive during the presentation but start whispering and snickering afterwards. Finally, make sure that everyone presents their design, so everyone is on both sides of table.

I will write more about this later. If you have read this, you can probably think of dozens of ways in which the culture of a project eventually caused some form of failure or otherwise. Please share them. It seems more obvious everyday that people are what complicates enterprise-level software development to the point of failure, not technology.

Comments (1)

Visitor Design Pattern with Reflection

The GoF introduced core design patterns that are used throughout OOP implementations everyday. In fact, a good amount of code that I have seen over the years uses (or almost uses) a design pattern without the developer realizing. However, a good amount of code I have seen is a mess because the developer did not use a design pattern.

By definition, a design pattern offers a tried and true solution to a requirement that occurs throughout most applications. However, inexperienced developers fail to recognize the similarities, or patterns, of the requirements and attack virtually the same problem from different perspectives. This happens because the details of the problem are usually very different, making the similarity of the problem structure elusive. As a result, the developer will wrestle with the problem in a different manner than they did any previous situations, sometimes reaching different conclusions. Also, this prevents the developer from identifying common functionality, forcing them to have multiple code components that solve the same problem.

One example of this occurs when an interface or superclass definition needs to be narrowed at runtime. A simple example is an Animal hierarchy:


public class Animal {
public void walk() { //do something };
public void sit() { // do something };
}
public class Dog extends Animal {
public void chaseSquirrel() { //do something };
}
public class Cat extends Animal {
public void scratchPost() { // do something };
}

As you can see, although Dogs and Cats have similar methods, they also have different public methods. In our example, we receive a list of Animals; both Dogs and Cats. However, we need to invoke a method that is unique to each animal as we iterate through the list. One way to handle the problem is to alter our Animal definitions as follows and iterate through the list in the following manner:


public interface Visitable {
void accept(Visitor visitor);
}
public interface Visitor {
void visit(Animal animal);
void visit(Dog dog);
void visit(Cat cat);
}
public class Animal implements Visitable {
public void walk() { //do something };
public void sit() { // do something };
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public class Dog extends Animal {
public void chaseSquirrel() { //do something };
}
public class Cat extends Animal {
public void scratchPost() { // do something };
}
public class Demo implements Visitor {
public void visit(Animal animal) { // do something };
public void visit(Dog dog) { dog.chaseSquirrel() };
public void visit(Cat cat) { cat.scratchPost() };
public void iterateAnimals(List<Visitor> animals) {
for (Visitor animal : animals) {
animal.accept();
}
}

As you can see, this will work by calling the accept method, which in turn call the visit method with the narrowed instance. However, what if the hierarchy held a dozen unique animals? Then we would need a rather large switch statement, and our iterator method would be rather large.

However, this solution seems a bit limiting to me; what if we need multiple accept methods or Visitor types. Also, there is a good amount of extra code and definitions needed here.

It is important to note that the JRE will not narrow in the list iteration automatically. For instance, this will produce an output of “OBJECT” over and over, even if the list held Strings.

void handle(String s) {
System.out.println("STRING");
}
void handle(Object o) {
System.out.println("OBJECT");
}
void iterate(List<Object> list) {
for (Object o : list) {
handle(o);
}

This would be the ideal solution, however for obvious reasons Java does not offer this option.

Instead, I suggest altering the pattern to use reflection. This removes the need for extra interface definitions and implementations and keeps the implementation cleaner. Here is an example using our original (non-Visitor) Dogs and Cats:


void handle(Animal animal) { // do something };
void handle(Dog dog) { dog.chaseSquirrel() };
void handle(Cat cat) { cat.scratchPost() };
public void iterateAnimals(List<Visitor> animals) {
for (Visitor animal : animals) {
Method m = Util.getMethod(this, "handle", animal);
m.invoke();
}

First, assume the “getMethod” takes the object with the method implementation, the method name and object (or it could be the class). To provide more functionality, we could also provide some extra parameters and have the Util actually invoke the method.

Leave a Comment

Pure and Simple Java Aspects with Proxies (Roll Your Own) – Part 2

This is the second part of my series on creating your own AOP solution. The first can be found here.

The last post left off having proxied our interfaces so that method calls could be intercepted; pre and post-processing could be performed. Some examples of this processing would be logging, security, caching, etc. At this point it is fairly obvious that the current solution has a significant drawback; there is only one chance to intercept the call. Ideally, the invocation handlers could be chained dynamically. However, the current solution requires an implementation for each possible chain. This is unacceptable.

To resolve this issue I defined two “interceptors”, a pre and post method interceptor. Here is the interface definition:


public interface PreMethodInterceptor {
void preMethod(Object proxy, Method method, Object[] params, Object target, InterceptorAdvice advice);
}
public interface PostMethodInterceptor {
void postMethod(Object proxy, Method method, Object[] params, Object target, InterceptorAdvice advice);
}

Now an invocation handler can be populated with any combination of interceptors, and the code will look like this:


public abstract class ProxyInvocationHandler implements InvocationHandler {

private static final Logger log = Logger.getLogger(ProxyInvocationHandler.class);
private List<PreMethodInterceptor> preInterceptors;
private List<PostMethodInterceptor> postInterceptors;
private Object target;


public ProxyInvocationHandler(Object target, List<PreMethodInterceptor> pre, List<PostMethodInterceptor> post) {
this.target = target;
this.preInterceptors = pre;
this.postInterceptors = post;
}
public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable {
HandlerAdvice advice = new UniversalAdvice();
Object object = null;
for (PreMethodInterceptor pre : this.preInterceptors) {
if (advice.continueIntercepting()) {
pre.preMethod(proxy, method, parameters, this.target, advice.getInterceptorAdvice());
}
}
if (advice.invokeTargetMethod()) {
object = method.invoke(this.target, parameters);
advice.getInterceptorAdvice().addInterceptionResult(target, object);
} else {
object = advice.getInterceptorAdvice().getLastResult(target, method.getReturnType());
}
advice.reset();
for (PostMethodInterceptor post : this.postInterceptors) {
if (advice.continueIntercepting()) {
post.postMethod(proxy, method, parameters, this.target, advice.getInterceptorAdvice());
}
}
return object;
}
}

Now, each desired interceptor is processed before and after the method call.

The next obvious improvement will be how to handle exceptions, failures and other behavior altering logic. Take this case of an interceptor chain, inspired by my cache/performance task defined in part one.

Log -> Performance -> Cache -> Method -> Cache -> Performance

Obviously, if the cache holds the object graph we are interested in, we do not want to execute the method or perform the post-cache interception. In that case we would ideally want this:

Log -> Performance -> Cache -> Performance

Yet it is vital that each interceptor is ignorant of other interceptors. We cannot expect to share state between the interceptors (although that is completely possible, its an assumption that we should not make). So stay tuned for part three, where we tackle this problem.

Leave a Comment

Introduction to Burn Down Charts

Burn down charts are a fairly new method of tracking and managing software projects. Originally part of the Scrum methodology, they are being adopted more widely now. I am intrigued.

A burn down chart shows a graph of how much effort a project will require before it is completed, relative to it’s deadline. A great primer on burn down (and it’s inverse, burn up) charts can be found on here.

To try it out, I figured I would use it on the next phase (or sprint) of my project. Currently, I do not really do project planning, I simply list out the tasks and my estimates in “man” days. If you have ever gone through this task, you pretty much guess. Also, there is another period for “testing” and “integration” testing, which always comes at the end of the phase. If you have ever waited until the end of a phase to begin testing all of you code, you probably quickly realized that you are sunk. Allotting two days to test twelve days of development strikes me as illogical. Besides, who doesn’t iteratively test and develop?

So, for this phase, I have decided to try something new. Using excel, I started with the same task list exercise, however, this time i broke down the tasks into smaller efforts and estimated the time it would take to develop, test and integrate.

(This is very important; the task is not complete until it is completely coded, unit tested and integrated into the overall system. What a novel concept… don’t mark things as done until they are actually done, not when you are pretty sure they are done.)

The estimates are provided in hours. I also added who was responsible for the task. Next, I added all the hours; this is my initial development effort estimate. The estimates will change as the project moves forward because the requirements and other factors outside of my control will change.

Next I needed to pick my scale for time, the x-axis. Since my project is relatively short, I have chosen every workday. I then listed out every unit (workday) in a column. For Day 1, I used my total effort estimate in hours. I then setup a formula for Day 2 that will subtract how many hours I expect to accomplish in a day from Day 1. In my case, since I am working on this part-time with someone else, I chose 12 hours per day. So my table looks like this:

Days Hours Remaining
Day 1 196
Day 2 184
.
.

Hopefully, my Hours Remaining column will be at 0 on my final day, otherwise I will have some time to make up.

Next, I need to track my actual progress. So I create another column that will show how many hours I have remaining; the estimated effort minus the hours burned. This becomes the actual progress.

To produce this chart in excel was very simple; insert -> chart. I selected the 3-D line, although a standard line will work just as well. I selected my 3 column table (I added column headers), and I am now well aware that I am behind schedule. Maybe I should get to work.

Burn Down Example

Leave a Comment

Pure and Simple Java Aspects with Proxies (Roll Your Own) – Part 1

I recently went about creating my own “interceptor” solution, outside of the normal AspectJ, AspectWerkz, or Spring AOP solution. I realize that it sounds like maybe I am suffering from the “Not written here” complex, but I did not want to have to introduce a whole new package/technology into our codebase. We are not using Spring, and probably won’t any time soon. Also, I really did not feel that I needed to bring in AspectJ, etc. just to solve a simple problem. Besides, I just wanted to go about designing something.

It started because recently I have been tackling a few issues with queries in our application. It started with some queries taking a long time, close to 30 (yes, thirty) seconds to complete. The code that I am looking through uses Hibernate, but basically as a way to not use JDBC. Nearly every query is written as a prepared statement in the code, and there are hundreds of queries. Each query typically has its own method, which handles all of the transaction management and caching.

Quickly, let me point out that I realize this is foolish at best, with terrible being a more realistic adjective. Firstly, all that transaction code is repeated throughout the entire service level, it’s not even refactored out into a method. Secondly, the caching/retrieving is done manually in some methods rather than use Hibernate’s caching. Even more interesting, EHCache is used as the cache. So to be clear, the code basically uses Hibernate to avoid ResultSet manipulation.

Initially, my task was to find the slow queries. However, the poor response times could only be reproduce in the production environment; not an ideal place for debugging. I figured that slow queries would still be slow in dev or in QA, just on a much smaller magnitude. If I could time every query in dev and QA, I could identify the relatively slow queries. These would be the queries that I would optimize first, most likely with some caching. Furthermore, this would provide an audit of every query call in the system.

Ideally, I would just wrap up every service-level object with a performance tracking interceptor in my Spring configuration, but Spring was not an option. So, I could go through the code and add timestamps before and after each query call, adding to the transaction and caching code. Or, I could figure out a way to intercept each method invocation and perform the timestamp logic. As you can imagine, intercepting made the most sense to me.

It is interesting to note here that this seems to me the only solution, but I realize that others may not see it this way. They may see this as overkill and a waste of time. Why not simply add the necessary code through copy/paste, throw it into QA and roll it back when the results are obtained. And I have to admit, this probably would save time and be just as effective. However, it also seems sloppy and error-prone. Also, I was thinking bigger-picture; adding the transaction management and cache management into the interceptor.

Back to aspects. Java 1.3 included the Proxy class in the reflection package. This class provides a way to dynamically create proxies of interfaces with an InvocationHandler. At runtime, rather than call the actual method of the proxied class, the “invoke” method of the handler is called. In essence, this allows the developer to “intercept” the method invocation and do as they please. The only condition is that the class to be proxied must implement at least one interface, and only the methods defined in the interface can be “intercepted”. Fortunately for me, the code I was working with had the service level properly defined through interfaces.

So the solution was beginning to take shape. I simply need to create a proxy for each service level object with a handler for tracking the elapsed time between the method invocation and the method return. So, the handler would look something like this:

public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable {
Object o = null;
try {
long start = System.currentTimeMillis();
o = method.invoke(target, parameters);
System.out.println(method.getName() + " - " + (System.currentTimeMillis() - start));
} catch (InvocationTargetException ite) {
throw ite.getCause();
}
return o;
}
}
This is obviously a very simple example (and I apologize for the lack of formatting). The invoke method is called in place of the method on the proxied object; "target". In reality, we would not want to write out to the System, but this is just a simple example.

To set this up, the following code is needed when the “target” is instantiated.


Foo target = new FooImpl();
MyInvocationHandler handler = new MyInvocationHandler(target);
Foo proxy = (Foo) Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { Foo.class }, handler);

Simple enough, isn’t it? Now, the target is actually an instance of “FooImpl” and the proxy is actually an instance of “Proxy”. However, the proxy instance will actually be treated by the runtime environment as an instance of “Foo”. Any instanceof, casts and other type comparisons will properly evaluate just as if a non-proxied instance of “Foo” was used. However, it is important to note that the proxy is not viewed as an instance of “FooImpl”.

The handler code can be improved significantly; for instance rather than writing to System.out, the handler could write to a file or database. The results could be filtered to ignore results that fall under a specified threshold. I actually did both of these and found the problem queries.

Another thing to keep in mind is that the entire call stack can be examined and reported. Not only do you have the immediate method call and target, but the class and method (and line number) of the caller can be determined with:

new Throwable().fillInStackTrace().getStackTrace();

I am going to end things here for now. As you can see, with very little code we have provided ourselves with some pretty useful information without incorporating any new technologies or cutting and pasting. However, we can still do more. We can link multiple interceptors and specify which methods we wish to intercept. That will be part 2.

Also, please feel free to comment or ask questions.

Comments (2)

How to access Environment Variables in the Spring IoC Configuration

UPDATE: For a more modern version, please check here.

So for the past few hours, I have spent some time attempting to figure out how my Spring applicationContext file can resolve environment variables. First off, let me start by saying that I HATE using environment variables, especially when Java provides you with a simple method to define your own variables. However, I had no control over this requirement, so off to work…

THE PROBLEM: We have a simple properties file used to dynamically define the Data Source. Here is the Data Source configuration:

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>
${db.connectionType}://${db.url}:${db.port}/${db.name}
</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>

I had a PropertyPlaceholderConfigurer that pointed to the location of my properties file, which was located in the classpath. (<property name="location" value="classpath:context.properties" />). My application, built using Maven, would create an executable jar containing the dependencies and everything ran perfectly… in my development environment.

I realized that this would not be ideal for other environments, but I kind of pushed this out of my mind during the initial development phase. However, it was now time to support multiple environments dynamically.

SOLUTIONS:

Manually editing the properties file was not an option, it was locked in the executable jar. So, it is clear that the file would need to be placed somewhere else in the file system.

My first solution was to simply change the location to: <property name="location" value="file://${ctx.props}" />. This would allow us to start the application with whichever properties we wished.

Unfortunately, we want to avoid using System properties when starting applications, instead we would like to use environment variables. Aside from being lazy, this can lead to enormous headaches trying to manage several environment variables throughout several environments. I would much rather create a simple startup script.

Here is where things get a bit confusing. Java 1.4 deprecated the System.getenv() method, which gave a Java application access to the environment variables. Therefore Spring did not support accessing environment variables. So, when I googled “spring environment variables”, I found several posting indicating that it was not supported and not possible.

It wasn’t until dug into the API (which is where I should have started), that I found that Spring does in fact support this out of the box. So, now the environment variable problem was solved as follows:
<property name="location" value="file://${CONFIG_DIR}/context.properties" />

Now, however, I (and all other developers) were stuck having to create an environment variable locally for a deployment requirement. So, after some quick research, I ended up with this solution:
<bean id="dataConfigPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file://${CONFIG_DIR}/batchContext.properties</value>
<value>file://${ctx.props}</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="searchSystemEnvironment" value="true" />
</bean>

Now, I can run this locally (assuming that I have not set the environment variable) with the ctx.props set as a System property or I can run it with the environment variable set (and no System variable).

ONE MORE THING:

With this setup, I would have a properties file in my jar that I never use (because I am using a file resource rather than a classpath resource). So, I had to update my Maven POM to exclude the properties file in the build.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>executable.Startup</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>install</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
</build>

And I am done.

Comments (3)

introduction

this is the first post on the site.  i am still working on coming up with a concise mission statement, but for now i am planning to use this site to kick around some ideas and display concepts and tutorials as they become clear to me.  for the past 10 years i have been using the internet to help me overcome my everyday programming obstacles, and now i am planning to return the favor.

Leave a Comment

Follow

Get every new post delivered to your Inbox.