Tuesday, November 13, 2007

Some Architectural Notes:

"Practical Guide to Enterprise Architecture" book published by Prentce Hall has very readable contents. I enjoyed reading it. Here are some highlights

Architecture Is Business-Driven. This is the first and most important habit of an architect. All architectural undertakings should be done for the sole reason of solving business problems. Architecture planning is a continuous process that requires full alignment with business goals.

Communication Is King. Enterprise architecture must be communicated to all parties across all IT organizations and lines of business on an ongoing basis. This should include strategy, goals, and objectives. Its purpose is to increase awareness throughout an enterprise. It must also include a statement of shared values that are endorsed and supported by the executive team and incorporated into the IT governance model.

Unification Is Queen. For enterprise architecture to succeed, it must be unified across the enterprise. Success and failure of all forms of architecture depend upon the joint efforts of all business and IT areas, their support of the architecture, and consistent implementation.

The Frog Is a Prince. Support from the executive team is essential for the practice of architecture, but a successful architecture starts almost at a grass-roots level by seeking support from line managers and their direct reports where the sell is the hardest. Getting buy-in from the troops (Seek first to understand, then be understood) will shield the architecture from passive resistance.

K.I.S.S.: Keep Infrastructure Simple. Information technology assets and the elimination of the endless combinations of platforms that use disparate technologies are the main inhibitors of change and result in an inflexible architecture. A goal of sound enterprise architecture is the destruction of incompatible technologies. If infrastructure is kept simple and consistent, enterprise architecture can leverage existing skill sets, training, support, and services in a cost-effective and agile manner.

Agility Is Key. Sometimes the best solution for a particular business problem requires adoption of a technology and/or approaches that are not mainstream. Agile enterprise architecture will allow the modification of standards to support business requirements, and it will seek out alternate ways to integrate the technology with the current architectural model.
Crawl Before You Walk. Expectations should be managed. The best architecture for an enterprise is one that evolves versus one that is created. Each iteration of the architecture should build upon previous versions and provide an incremental improvement to the organization. Enterprise architecture should also focus initial efforts on near-term opportunities—what is within reach—that can provide immediate business benefit. Small but quick wins will help win allies from both the business and technology areas and will best demonstrate the value of agile enterprise architecture.









It's very important to capture & communicate business/technology drivers well in succinct manner to all the stake holders.Defining architecture satisfying all the stake holder is a very difficult task. Un-informed stake holders will have unrealistic architecture goals. CEO expects application to scale for decades, Manager wants no duplication efffort at any cost & expect that to come completely from architecture, Developers exepects libraries to ease out their coding & Designers expect check lists.


All the stake holders are correct in their own way on the expectation from architecture, but unfortunately Architecture cannot accomplish all the individual requierements & it's important to set the expectation clearly.

Architecture -> Is About Making Technology choices. In my openion this is the most important feature that will have huge impact on any projects. Application maintainence, Deveoper availability should be given as much importance as techincal excellence of the tools that we choose.
Architecture -> Is About understanding the context. All patterns/anti patterns work with aparticular context. If we miss out this context the patterns/anti pattern knowledge can have adverse impact on our judgements.
Architecture -> Is About understanding of problem space & strong connection with business. Respecting the domain experts & their openion is very important here.
Architecture -> Is not just about Reusable artifacts but flexible artifacts. There will be always better ways for implementiion techniques as time progresses. So "re-use" is overloaded word. So I guess flexibility is more important thatn reuse. Interfaces & Dependency Injection (Spring is excellent in serving this pupose & latest version gets you out of XML hell as well :-)) is more important as they give the power of changing the implementation with better means over the time without much pain.
Architecture -> Is about understanding varying scalability requirements, Every one don't require eBay or Amezon kind of requirements. I have personally executed 3 eBay projects as a lead & the rules of the game is totally different for those kind of applications.
Architecture -> Is About breaking rules.eBay doesn't rely on transactions. The data is heavily de-normalized, Most of the processing happens at the application tier. The asynchronous channels are heavily used. ThreadLocal's are used heavily. Scratch DB is preferred way to store the session data. Scaling happens @ the web-tier & EJB's doesn't even qualify as contenders & the XSL templates rule the presentation layer.These rules are definitely not suited all kinds of applications. It was very difficult for me to come out of hang-over of eBay code base.

Architecture -> Is about understanding the Return On Investment & ability to predict the future with political, local & legal aspects in the mind. Documenting patterns & anti-patterns in these areas will be invaluable as experience/historical knowledge holds the key.

Identifying the need of different stake folders & making sure that architecture artifact look relevant to all stake holders is important.So it is better to write different documents focusing on different stakeholders.It is important to keep the theme same in all artifacts. For Example,"System Architecture" document should capture the over-all technical decisions that were made along with explaining the rational & tried alternatives. This document should act as technical design review check list. It's important document non-functional or operational requirements as clearly as possible. Architecture goals and means (usually debating 2-3 options for achieving the same).Integration of various application modules (critical ones).
For any web based applications screens plays very important role & they capture the requirements in best manner. This exercise ideally should be part of requirements definition & this "System architecture" definition process should begin sometime during the middle of requirement definition. Unfortunately it's a tedious task define the screens manually& as of now there is no credible way to convert HTML directly into production by few tweaks (Frameworks like Tapestry & Wicket web frameworks certainly makes better compare to other approaches).
"Application Architecture" Should capture the different technical layers & their components. It also should capture the over-all application modules & their relationships. Integration is the most painful aspect development as it not only involves code integration it also requires to people also communicate & integrate. More emphasis should be given in declaring the interfaces & handshaking mechanisms between the teams & modules.

Conclusion:
Software Architecture is very challenging, I guess aspiring Architects need to focus get in touch with real developers & do active coding rather than relying on marketing literature available in Internet. The best brains sitting in ivory tower developing specification (Martin Fowler calls them Harvesting frameworks) really doesn't add much value compared to specification/frameworks that comes out as by-product of developing real time applications like Spring, Rails. (Non Harvesting frameworks). Unless Software Architects stop forming their opinions based marketing literature and blogs, they will be forced to be called as "White Elephants" of software industry.

Friday, August 17, 2007

Some tips on developing Swing applications:

I recently developed one small application in swing . Here are some comments on tricks & libraries I used.

1. Using Template Method Pattern to simplify code

ActionTemplate :

JButton button = new JButton("Ok");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
//do some stuff here
}
});


This is how usually we handle the actions (I don't believe in separating out actionlisteners code from components declarations, it is always easier/better to keep them as near as possible) Anonymous classes provides easier way of achieving this;

from the above code I noted that,
1. In anonymous class, I will never use ActionEvent class & I don't want see it. It's an intrusive model.
2. Many times I want to handle actions asynchronously (like using SwingUtilitilities.invokeLater() routine), I don't want to repeat in every action listeners code.
3. I also want to display a wait cursor during this time & should be optional way of passing the icon @ runtime;
4. I also want to print the time taken & memory consumed for each actions; & I don;t want to repeat that, I also want to take that out from the code once the application in production.
6. actionPerformed method name is not intuitive, although ActionEvent not used 90% of time I do use it 10% of time when I write inner or separate ActionListener classes, it should be available for me.

Here is how I propose to handle this using template pattern.

ActionTemplate is abstract class having clicked() as abstract method & with default implementation for all other features.

button.addActionListener(new ActionTemplate() {
public void clicked(){
//do some stuff here
}
});
This is fine for me 90% of time.Now code is less & looks simpler. This is clearly non-intrusive model & all the extra features are defaulted and can be easily changed by over-riding when required. (Convention over configurations)

In case to handle the event asynchronously whenever some tasks lot of time, we can over-ride isAsynchronus call. isAsynchronus() will return false by default.

button.addActionListener(new ActionTemplate() {
public void clicked(){
//Some lengthy stuff here, So cannot make AWTDispatch thread to wait till it's completed
}
public boolean isAsynchronus(){
return true;
}
});


Similarly there can be callback methods for setting the wait cursor, monitoring completion status, (getWaitCursorIcon(), done() etc...)
We can also get the ActionEvent which is member variable for ActionTemplate class.

Similarly for WindowTemplate we can add closed, closing, max(), min() etc...
& for MouseTemplate methods like pressed(), clicked(), hover(), rightClikced(), doubleclicked() etc...
These will easier to understand & code than their Listener counterparts. I guess this approach is near to closures. Lack of closure in java is really a bottleneck while coding event handling. We are forced to deal with anonymous & final variables

Although I don't feel these tricks can be used in large projects (As adding more APIs to likely to confuse more) but this type of techniques will be very useful when we write applications in a very small team or by a single person, where using AOP, XML, Properties file based action handling framework using reflection is not an option (because of learning curve & the increase in total java source/jar size)

2. Window Dissolving tricks
While going through the
Swing Hacks book, I came across one dissolving hack. I also wrote some inspired from that, although they are too simple but can give window closing a different experience :-), Feel free to copy & use it from here.

3. External libraries
There is no point re-inventing the wheel, there are abundant high quality JFC components freely available, but the problem is most of them will come big baggage (in terms of size & API complexity).
I used these libraries & had no problem whatsoever with these.
JFreeCharts - For reporting through charts
iText - For Generating the pdf reports
I have used these excellent libraries in the past (but with J2ee applications)
DesignGridLayout - As layout manager

It is always better to code these in such a manner that if even when we take out these jars, application should work properly.It is always better to make these libraries not to be part of core functionalities. I handled these by neglecting ClassNotFoundException in appropriate place where-ever I am using such libraries. In case of applet & webstart it is not practical to use these heavy libraries as they increase the download time & there might be issues with licensing as well. So for an applet/java webstart versions JFree charts, iText & other external jars will not be included, I just remove these jars from library to make it light weight, that's it no code change required :-)

4. Layout Manager
I did not wanted to use the GridBagLayoutManager, it was regarded as un-necessarily complex for developing Swing applications. I tried out generating UI with my favorite IDE NetBeans. I did not like the code it generated. May be I need adjust with this kind of code in future. First the code generated was looking ugly & I wanted to use my custom components which I need to add & I like doing small tasks by defining actionlisteners there itself, the code generated lot of default methods. As of now I can think of using it only for prototype.



I looked into layout managers who all claimed to be better alternative to GridBagLayoutManager, I actually looked into the code of most of these layout managers & they are indeed provide lot ease in developing layouts,some introduce more complexity
I came across this debate on from layout manager showdown, All kudos to John for the splendid job done by comparing layout managers.
I found MigLayout to be best in solving all types of complex requirements, But for my screens I found DesignGridlayout the simplest after going through the sample code. As it is based in standard JDK library, this should be first choice for developing simple grid based screens.

For example, I wanted to design my layout something like this;

& it took 12 lines to do that, & code was too simple.


public void build(DesignGridLayout layout){
layout.row().center().add(title());layout.row().height(10);
layout.row().center().add(new JLabel("Email ")).add(user());
layout.row().height(5);
layout.row().center().add(new JLabel("Password ")).add(pwd());
layout.row().height(5);
layout.row().center().add(new JLabel("Exam List")).add(examList());
layout.row().height(5);
layout.row().center().add(signIn());
layout.row().height(15);
cancelButton = new JButton("Cancel");
loginButton = new JButton("Ok");
layout.row().center().add(loginButton).add(cancelButton);
}


It was such a simple task to develop for other such screens as well

5. Code Samples from Experts
Swing Hacks & Filthy Rich Clients are the best books on swing written by highly credible authors. The source code that comes with these books are also excellent resources.
There are quite very nice ideas explained well for developing custom components. Source code for the both the books can be freely downloaded, but I suggest to buy the books.

These are the excellent code I re-used in my application
Pieter-Jan Savat's wonderful utility on page turning effects in swing.
Simon's flashy & bouncy effects with swing.
Hyper Link listener for handling links

6. Leveraging New JDK1.5 APIs Features:
Using new JDK1.5 features can simplify the coding to great levels; JDK1.5 has been one of the biggest release in java's history.
For example, now it is possible to add data to the to list like this;
List l= list("praveen","manvi","test"); using

public static List list(T... elements) {
List list = new ArrayList(elements.length);
for (T elem : elements){
list.add(elem);
}
return list;
}


These tricks will be useful especially writing Models for UI components. The above sample shows the usage of improved for() loop, generics & variable argument options.
I found this explanation good with samples.

Friday, June 01, 2007

Monday, April 16, 2007

Java Coding Guidelines
Establishing coding guidelines is one of the main task for Technical Lead or the Technical Architect for any newly starting project. This is one of the ritual which attracts very little attention form both developers & management.In past I have tried to set up coding standards for J2EE based projects & came up with guidelines for each layer (Service, DAO & Presentation).Although it was well advertised, most of the developers did not even seem to interested in looking into the content & were happy to follow the prototype. In that sense establishing a coding guidelines is the least impressive job & probably the most thankless job.

With this background, I started looking into the existing published coding guidelines with the intention of extracting best of all the published guidelines. Surprisingly Sun published java coding guidelines in 1999 & never thought it to be worth revising.I also looked into the other big java shops like IBM,eBay,BEA & Oracle, I couldn't find any content that focuses solely on coding guidelines (Please correct me if I am wrong on this).

Most of the information I found on coding guidelines was repetitive. With Java replacing C,C++ as primary computer learning language & with best IDEs taking care of indentation, style etc... there is no need to have full fledged coding guidelines & I guess Sun is right in not revisiting their coding guidelines document. So coming up with Java coding standards doesn't make sense, the best option would be come up with Technology centric coding guidelines (SWING, EJB ,Spring, custom framework etc...) to really have some value addition in this area.

So I am enumerating few topics here that I have observed over the years while writing Java code & mentoring programmers. I guess the that's the better option than coming up with a clone of sun coding standards by changing some content here and there.

These are some mistakes (or bad practices from Java point of view) I found with the guys coming from C++ background & junior engineers in my experience:
1. Method name starts with capital letter (Like GetThis() etc..)
2. Passing reference & manipulating them (Writing methods like void getData(SomeObject obj) etc...)
3. Very conservative about writing many methods & classes (over cautious about the performance)
4. Poor understanding of abstraction
5. Writing lot of static methods
I think best way to understand the Java coding standard is by going through the source code of JDK.

Objective of coding standards:
- Creating concise code
- Improve readability, maintainability & continuity (Programmers changing over the time should have least impact)
So although it is trivial, it is important for the organization to enforce the coding guidelines strictly(Sun java guidelines).

List of some good Coding Guideline links:
Sun Java Code Conventions
Coding standards from nejug
Standard Java coding guidelines -This I found it very interesting as it was trying layout the guidelines on OO principles.
I am planning to come up my own standards some time in future.:)

I really liked the idea of Google where they have listed out only the missing information from Sun standards, This is the approach I am going to suggest for my future projects as well.

The most important point I feel in writing the good quality code is considering the good design principles & have them as part of coding checklist.I have also published some points on presentation layer in one of my previous blog.

Here are my favorite picks:
1. Handling NullPointerException
This is most disgraceful error that a programmer can make, I always used to treat this exception as insult the programmer's intellect. Before calling any methods programmer needs to ask himself a question "Hey can this be Null" if he thinks it can be a null, then putting Null check is also not always correct. I personally hate null checks in the code. Most of the time it is like looking at both side of the road while crossing a one way road.
So I prefer throwing IllegalArgumentException & usage of assert keyword

2. Exception Handling
Spring, Hibernate & other popular frameworks have established one fact that Checked Exception are over-used & Runtime exceptions offers best solutions in most of the cases. Although James Gosling hails Checked Exception or Fault containment as one of the best feature of Java I tend to agree with usage of Runtime Exceptions heavily with the caviet that if there is alternate flow that API user can handle or we are writing APIs & exposing them to third part checked Exceptions are the best.

All the Exception that happen very rarely & is because of hardware/network or programmatic errors which end user can't really help should be declared as RuntumeExceptions & any checked exception thrown by other APIs should be declared in throws bloc if we can't take any action in the try{}catch{} bloc.Exception should be thrown only in case of alternate flows.FinderException is very bad design in EJB as it throws Exception if there are no records rather than returning empty list, that's silly.

With the above stated principle RemoteEception should have been RuntimeException & ParseException (For example Integer.parseInt(String str) is likely to go wrong should have been Checked Exception.

3. Usage of proper APIs
There are quite lot of hidden gems of Java that are under-utilized, Like java reflection, WeakHashMap & lazyloading with Runnable can really make applications more faster and robust. Spending time Javadocs is most useful exercise.

We need tools to correct the mistakes, We need to make static analyzers intelligent enough to take care of all the unit testing issues & performance issues. Tools like PMD offers the huge benefits in terms of productivity and improving the quality of code. The plug-ins available for popular IDEs like NetBeans, eclipse makes developer life still simpler.

PMD rules Index - There are about 200+ PMD rules for code checking.As prevention is better than cure, it always better if developer visits these before writing code.

Here are my best rule that can be used as mandatory ones
Unused Code Rules
Basic Rules
Optimization rules
Strict Exception Rules
Import Statement Rules

These rules also can be considered
Design Rules
Type Resolution Rules
Code Size Rules

Some bad rules;
AtLeastOneConstructor
CallSuperInConstructor
LongVariable
ShortVariable
I fully agree with this link on PMD Bad Rules.

Hammurapi,CheckStyle and Findbugs also provides similar rules, It will be good exercise to extract best out of all these.

Finally,
All the rules, Conventions & best practices are here to help us. We definitely not here to help these rules. Common sense should prevail over these guidelines. Consider what you like & disdain what you don't like.

Monday, March 05, 2007

SWING CORNER

I have been involved with development J2EE projects from past several years. As I am looking to work with a SWING project now, I wanted to collate all the information regarding the SWING that should help me refresh my SWING skills & also to help new programmer to ramp up on SWING quickly. I also wanted to use this SWING corner blog as my SWING books-marks for my future reference.So I will keep adding/editing the information here. :-)

Real time applications are best testimonials for any technology.Here are the set of real world applications for non-SWING believers.

Selected links to start with Java SWING for beginners:
wikipedia SWING Overview
Best place to start with SWING
Swing Pointers
Sun Tutorial about SWING architecture

It is always best approach to follow best SWING programmers to learn new techniques. Here is my favorite list & their blogs. All are committed SWING developers.
1. Romain Guy with amazing swing skills
2. Ethan Nicholas
3. Kirill Grouchnikov & http://www.curious-creature.org/
4. Shannon Hickey
One more interesting Blog

There are also equally credible people saying Java SWING has reached it's dead end :-( like Bruce Eckel of Thinking Java fame, So it is equally important to watch the Adobe Flex APIs & Flash. Also there is lot of effort is being put on developing JSF components. After looking into exadel , GWT & ICE Faces I feel like do we really need thick clients?

Some Useful Java Links that will be helpful for new development:
Lot of links Articles,Blog Posts,Interviews Commercial/Open Source Projects reated to dektop development
Libraries that can ease development effort
Great Swing Components from Swing labs
Popular layout managers (Gridbaglayout has been very un-popular & difficult to work)
FormLayout
TableLayout
JGoodies Validation https://validation.dev.java.net
JGoodies Binding https://binding.dev.java.net
Spin http://spin.sf.net
Foxtrot http://foxtrot.sf.net
Swing Code samples http://www.codeguru.com/java/Swing/index.shtml
Chart building solution for swing : http://www.jfree.org/jfreechart/ - Google AdWords & Jasper Reports uses these APIs

SWING Books & Frameworks:
Popularity of any technology can also be measured from the dedicated books that have been published, Considerably there are less books focusing completely on SWING, although most of Java generic books covers SWING superficially.

There are two books published by Orielly on SWING (Hacking Swing & Java SWING ) & both are good ones, There is one book from Manning publisher Swing in Action & there is also Desktop Java Live—published by SourceBeat.
USefulness of these books are limited as it will be totally API driven & time bound, the sample source code can be downloaded from the provided by these books & learn APIs by going though the code.I guess that's the best way.The cooper's book on Design patterns contains the design pattern samples developed in SWING. The book & source code are free & can be downloaded from here.

The single-most important point to choose Java as development platform for me is availability of massive amount of open source frameworks & utilities. I guess this point is not well appreciated by well known Java opponents Joel& Paul Graham (Although they are on my favorite writers list :-)). Unfortunately there are less number of frameworks & re-usable components in SWING compared web part of java (Tag libraries, JSF components, Library utilities & MVC frameworks)
But these are few attempts in that direction, basically these frameworks tries to come up with configurable action handling, form validation, custom UI components built over standard JFC components, Custom layout managers to ease out component layering & lot of static utilities for handling validation & threading issues.

http://www.artima.com/lejava/articles/swingframework.html
http://carbonfive.sourceforge.net/springadapter/api/com/carbonfive/flash/spring/package-summary.html#documentation
Spring Rich Client - As usual Spring efforts here also are very singificant in reducing source code lines which is very important in the SWING context
"Spring makes heavy use of the "Inversion of Control" (IoC) design pattern, meaning simply that individual JavaBeans don't go looking for data, the data is handed to them by the framework"

Some important Concepts:

1. Understanding lightweight architecture
Each heavyweight component contains its own z-order layer (the notion of depth of layering) all lightweight components are inside heavyweight components and maintain their own layering scheme defined by Swing.when a heavyweight inside another heavyweight container is placed it will, by definition overlap all lightweights in that container.we have to use mixing of AWT and SWING components cautiously

-A lightweight does not contain the native code (JNI), You don't have call "System.loadLibraty()" & hence inherently cross-platform, there are may off the shelf compoenents providing different Skins (Look and Feel Factory)
-A lightweight component can have transparent pixels; a heavyweight is always opaque.
-A lightweight component can appear to be non-rectangular because of its ability to set transparent areas; a heavyweight can only be rectangular.
-Mouse events on a lightweight component fall through to its parent; mouse events on a heavyweight component do not fall through to its parent.
-When a lightweight component overlaps a heavyweight component, the heavyweight component is always on top, regardless of the relative z-order of the two components.
-JFrame , JDialog, JApplet,JWindow are the top level components & hence extend AWT compoenent instead of JComponent unlike other components

A Swing component does NOT have a corresponding native OS GUI 'peer', and is free to render itself in any way that is possible with the graphics APIs.However, at its core, every Swing component relies on an AWT container, since (Swing's) JComponent extends (AWT's) Container. AWTEvent root event class for all AWT events & there is no JVM from Sun that uses full HW acceleration, and it SHOWS. Java 5 has *partial* HW acceleration - although significantly more than java 1.4 had

2. Understand the layout managers,data binding & validation well

3. Understand Good coding practices w.r.t. Swing.
Follow the open source code (As per the list above)
Written in 2000, but still good to go though http://www.oreillynet.com/pub/a/oreilly/java/news/learningjava_0500.html

There has been quite lot of work on SWING (Now unfortunately it looks like Sun also started loosing interest in pushing Java in Desktop arena, It is now left for Open source hacker to push SWING to next level) Swing classes contributed heavily to the exponential growth of total number of java classes as part of JDK.JFC has got solid framework & design.
Total # of Java Files as of now stands at
JDK1.4 : 3,883
JDK1.5 : 6,558
JDK1.6 : 7,061


As a SWING proponent (With vested interest :-) as of now), I tried to find some answers for the current stated SWING problems

A. SWING is very slow compared to SWT.
IntelliJ-IDEA (Swing) is faster than Eclipse IDEA and Eclipse have matching complexity, and despite Eclipse is backed by IBM and open source. it still does not perform better than IDEA, which is proprietary and developed by small group of smart individuals.
Java IDE's like netbeans, Weblogic Workshop & JBuilder are written in SWING.
So it's all about how well we understand & write the code.

B. SWING solutions have very bad distribution model (With lot of confusing JREs)
I guess Sun is working hard to improve the Webstart & I hope it will be as simple as Flash plug-in (Both in terms of size and ease of use)

3. SWING is complex to work with
I guess with proper usage of scripting (Like F3, Groovy solutions) & XML should make the life simpler

Hope this blog will be useful for new as well as old SWING developers. :-)

Friday, February 02, 2007

Bug Patterns:

While developing web applications there are few common errors that get reported again & again.We don't usually document these.I thought it will be very useful if we can document these after consolidating from the existing bug reports. So here I am trying capture few.

I am sure it will be helpful for new developers to reduce the bug count in their web related projects. I will be updating this list whenever I come across new common mistakes. So keep checking this blog for latest updates :-)

UI Bug Patterns:
  1. Handle the date display format @ common place, there is great possibilityto change here.
  2. Screen resolution/Browser Compatibility issues (Make sure these are part of NFR) should be tracked well.
  3. Make sure about UI issues while taking off the shelf tag libraries (For example tag libraries dsiplay tag, struts-menu comes with good CSS support)
  4. "Enter" key should work for search screens (Use java script), Struts submit won't work most of the times
  5. Session clearing issues- (Use case dependency, back button), Test all the scenarios by pressing the back button & clicking on the links/buttons.
  6. Horizontal scroll bar appearence (Look for fields that accepts more than 80 chanracters) Look into IFrame, Frame and <div> tags.
  7. Wrapping of characters in the table (header & other cells) will certainly help to avoid the horizontal scrool bar but for a continous String (without any space) wrapping will no effect
  8. Placement Calender window(Any pop-up for that matter) all platforms (Mac Safari, IE behaviour), Don't include calender option below the screens with vertical scrollbar.
  9. Don't use high resolution images(for thumbnails etc...),it takes more time to load the screens
  10. Don't add all the import statements as part of the common init.jsp.
  11. Before using any variable from session check for null if you are taking care of clearing of that session variable (Ideally you should not keep variables in the session,avoid if possible)
  12. Instead of hardcoding, use context Root path in JS or JSP files, make sure to use the relative paths. Use http server to load all the static data (if possible). It will reduce the sover-all ize of EAR file to be deployed.
  13. If user provides some junk values as HTML parameters (by directly editing url in the browser, just forward to same page by validating the same.For Example: He may enter the some String for Integer ID, then expect NumberFormatException & forward it to the same page.
  14. Cache all the Combo-box related data & make sure to update cache data as well if there is seperate edit(add/update/delete) for these data.
  15. Make sure to trim the input data while reading from text area/text feild
  16. Use "value".equals(value) rather than value.equals("value") to avoid NullpointerException - This is not general rule but failrly applicable in web applications
  17. Externalize all the user visbile data into properties file even if internationalization is not required. Eclipse(or any IDE) provides easy way to refarctor for externalizing
  18. Handle duplicate submission
I am also planning to come similar kind of list for Java track as well. I am sure this would be helpful for developers/managers as checklist which can be part of defect prevention activity.

Monday, January 15, 2007

Choosing Java Web Framework - Evaluation Parameters

Selecting a web framework is one of the difficult decision that Java architect has to make upfront while architecting enterprise web application
First, we need to select between MVC frameworks(strut, struts-2, Spring MVC) and Component oriented frameworks (JSF,Tapestry... they are also MVC based although!). Hybrid solutions also can be better solution for certain areas.General thumb rule should be, if the application is more stateful with less read only pages we should go for JSF (Possibly the best bet is using frameworks like Seam & ICEFaces).We can also look into Spring webflow(I have not tried) if the application is tightly coupled with Spring back end & has to work with pre-Java EE envioronment. These frameworks offers much mature in handling statefulness of application (we don't have mess around HttpSession) and inbuilt future to handle workflows & continuations.Struts(Classic one) should be fine for small CRUD app with read only pages.

It is very difficult to layout the evaluation parameters for selection. I tried to come up following parameters (which I think are going to be more important in the year 2007) after spending some time in going through these frameworks. Depending on weightage (which is project dependent) we can take decision. I also give set of links that should help the evaluator for zeroing on some framework (or frameworks :-))

1. RIA Factor
I guess giving rich experience to web users is not going to fancy feature but a mandatory one in coming days.If this feature becomes #1 driving factor while building the application (Ex:entertainment & media industry) traditional MVC frameworks cannot be even part of such evaluations.
JSF - Because of better ecosystem provided by the IDEs and off the shelf component libraries that easily blend with AJAX.
Adobe Flex - For all Flash lovers. I am yet to see a practical full fledeged scalable big enterprise application although samples & SDK look impressive
Web start SWING - The most undervalued/under utlized option according to me while developing intranet applications
targetting finite #(with known pattern) of users.
Winner : JSF

2. Data Validation and Binding framework
Struts 2 (Webwork) is the best choice here. Although Tapestry (Good but has steep learning curve),JSF and Spring MVC provides fairly good data binding/validation support, but are not as attractive as Struts 2 (with OGNL)
Classical Struts is very poor (because of fundamental design flaw in dealing with web forms & stateless Action classes) while handling this.
Winner : Struts 2

3. Presentaion layer options
Some times it becomes importnat political decision to go with trditional templating engine option (like Velocity & Free marker) when we need to build a very big site with seperate UI and back end team. But I personally don't value this point much, all the frameworks should be able to use all the best of off the shelf custom tags (Like popular Display tag & Struts menu & site mesh ) & template options.
In these situations Struts 2 and Spring MVC shines well. Although HTML native support by Tapestry is the most likable option if need to provide HTML prototype for each & every screen before implementation & we can directly roll-out the screens to production by making the changes to HTML itself.Spring MVC comes with inbuilt design for handling the various presentation layer option from it's inception.
Winner : Spring MVC

4. Navigation mechanisms and Controller infrastructure
Component Frameworks provide event driven approach (JSF & Tapestry) like our regular SWING listener classes that we can attach to UI components. we can even attach sophistgated renderers. But I personlly not very receptive this idea of taking away request/response paradigm and low level APIs (HttpRequest, URLs, etc...) from the developer.
Struts 2 comes with most sophistigated support for the interception infrastructure and works well with our thinking.
Winner : Struts 2

5. People & deadline Issues/Learning curve
Usagewise Struts 2 doesn't seem to have won many supporters (Unfortunately :)).There is pretty steep leanrning curve involved with all non classic -struts option as 90% developers are familiar with struts & continue to use it. (Why to waste time 1 month time in learning for 3-4 months simple CRUD project even if it takes is a valid argument). I think some time down the line we need to work with better tools and techniques that can simplify the programmer's job.In longer run it is better to scale up programmer's ability to learn new things & adopt in a faster time. Hence it is better to use latest tools rather than relying on classical struts. After all we are not going to work with single project throughout our life. We have to consider market behaviour, effort investment & future project oppportunities.
Winner: JSF

In Summary,
Unless the site to be built is not stateful, small & includes mainly read only pages with less complex navigation requirement compoenent framework is over-kill & traditional MVC framework (Struts -2 my favorite) is a better option.
Most of time we end up evaluating frameowork based on their technical capability rather our business applicability. I felt it is important to focus on business applicabilty rather than techincal capability.

1. Total # of lines of code for accomplishing particular activity(use cases)
2. Non functional requirements. (People,longevity,envioronment,existing investments etc...)
should also guide the decision

I came across these links & found useful.
SWING is not better option compared to JSF as per Oracle
Java Web Framework Sweet Spots - By Matt Raible

Sunday, December 24, 2006

ORM Vs SQL

There are two religious parties in java development world while dealing with persistence part of enterprise applications. It is one of the toughest decision Java architect has to make while joining any one party here.
  1. ORM

  2. SQL (JDBC).

Let me confess first, I belong to first party & here I am trying to put some arguments in favour of that. :-)

As the software evolving from monolithic programming to object oriented (We have procedure oriented, structured way of coding in b/w these two)the main motivating factor has been,

  1. Code reuse (No code duplication) that improves time to market and maintanability.

  2. Smallest possible lines of code providing the same result without sacrificing the readability & acceptable performance(Speed,memory etc...).

Hence the major criteria for assesing quality of the source code is reusability and maintainability.

Why SQL affects maintanability and scalability of application?

Working in Java - my business logic is object-oriented (With the advent of lightweight framework and domain driven design). As we cannot create well designed java application without (OOAD, all the data-models and queries that I need to construct separately & is definately duplicate my logic, even though I already have the same in my Java code I need to duplicate the same in SQL, so source code is defintely not going to be short.And worst part is every time I change my Java code, SQL also needs to be changed.

With ORM (Hibernate), we have persistence meta information in annotation ( with XDoclet) & we don't have to write any extra code if we change any of java code(for schema change). An ANT task should be able to do all the schema update. Database is a persistence layer. There is no need to know anything about persistence layer, except that it stores data persistently & that's it business logic should have nothing to do with it. ORM also gives RDBMS independence it can be huge benefit supporting business agility and avoiding vendor lock.

"PL/SQL provides better performance"
I don't think (from my experience) where well designed ORM fails on performance. Proper use of cache layer should solve the problem. Now cache frameworks are much matured, and ORM tools comes with out of box solution for the same.
Thinking about performance too much is counter-prodctive. It is always possible to revisit 5% of usecases where we might have to refactor to improve the performance with PL/SQL. With the productivity gain that we get out of ORM it is very much possible to absorb this 5% effort very easily in the end.

“ORM Learning curve is huge”
This is usual execuse given by the project development team & these set of lazy developers/managers who refuse to learn new techniques & happy using old worked solutions. Actually dead line pressure should be more strong reason to choose ORM. Nothing can be done to DBA paranoids who cannot (probably unwilling) learn and appreaciate the the importnace of Object orientation.

Having said that here I am not trying to downplay the importance of SQL. ORM tools are using SQL behind the scenes - that's what they do and I beleive Hibernate is the most popular exactly because Gavin King made it as SQL-friendly as possible. But it needs proper usage, that's all. One has to learn & appreciate the importnace of referential integrity and durabability that RDBMS has brought into the software and the huge cost/effort investment made in these RDBMS products will be valuable in the coming decades as well. We need to understatnd why it is an bad idea to run the same query in a loop, why there should not be many networking calls, why the query needs to be optimize to minimize the scan, why we should try to bring minimum possible data to presentation layers, why to device a way to handle open ended queries etc...

In situations like complex legacy model, Many set based operations (Batch updates/inserts), too much normalization or denormalization (No natural O/R mapping) ORM is definitely not a good candidate. It's a overkill.
In these scenario's also iBatis SQL map & Spring JDBCTemplate offers better value proposition.

Finally,
In any new development project there is no technical or political reason left to use plain JDBC/SQL solution for handling persistence layer.

Monday, December 18, 2006

Interviewing Tips

With the plenty of FAQ/Q&A available on Internet & books, most of the programmers are aware of nature of questions. If any one get selected by sheer power of raw memory, I guess that is unfair. Memory is also important but definitely not more important than the ability to assimilate just in time information to solve a particular problem.This is more important with the advent of Google Search & development methods.
So I think we should give 50-50 for both knowledge & analytical skills. We cannot undermine any of these two, both are equally important. With amount of APIs and available options we cannot afford to have good analytical person taking more than few months to learn & apply in 6-7 month duration projects.

Give a problem and see how the API design is done for that. Look out for class names, method names and handling alternate flows. Naming classes/methods and cache invalidation remains toughest part of the software creation. Probe on what difficult problem they have solved so far.

Q & A on factual details aren't useful & definitely not the best way to reject or consider.

Here some personal set of questions that I would like to consider while interviewing a person for technical lead position in my company. (Mainly in Java) All the professionals claiming developing web applications should be able to answer all these questions without any hesitation.

JSP/Servlet:
1. Difference b/w forward and re-direct & use cases for them
2. Difference b/w jsp include and include directive
I would expect senior guys to explain the actual implementation details by the container rather than just functional difference & usage scenario's.
3. Scope questions (session,request,application & page)
I expect senior guys to explain me terms like web clustering, session replication, load balancing, thread safety & it's implications on data stored in above scopes.
4. Explain the stateless nature of servlets,(It's life cycle, SingleThreadModel, web.xml entry requirements, ActionServlet implementation details if the candidate is aware of struts)
5. Filters, Listeners & their importnace.
6. Web framework(s) & their relative merits/de-merits. (Action Oriented Vs Component oriented) again from senior guys.

Core Java:
1. java.io.Serializable/Cloneable marker interfaces and their importance. Deep cloning Vs Shallow cloning.
Different ways of instantiating Objects & their overheads (new, Cloning, Serialize/De-serialize, reflection)
2. Collection API knowledge
Choosing a right collection is one of very important characterstic of good java programmer.
Practical usage of Map,Set,List from their experience. Performance related issues with them.
(Synchronized, Non synchronized)
A problem that involves string manipulation and usage of Collection APIs.A successful candidate should be able to produce code using proper APIs.
3. Exception Handling, Im-mutable nature of String
Any programmer who has coded in java should be easily able to tell about the difference/example for Checked Exception & Unchecked Exception. It will be interesting to debate on this issue with senior guys taking popular framework into consideration.

For Example reversing a given set of string with removing duplicates & sorting them alphabetically in case insensitive manner. Candidate should be able to pick up TreeSet, Comparator & String manipulation stuff.
Searching for a string in a file, It's important to see how the candidate write the code taking performance, Exception handling into consideration.

JDBC/Database:
1. Importance Normalization & join.
Difference between an inner join and an outer join
2. foreign key constraint? what can happen when you try to delete a parent record?
3. ResultSet, Template pattern, How to get column details from the table name?
4. A SQL query problem involving either outer or inner join with aggregate functions.
5. Dirty Reads/Phantom Reads. Selecting isolation levels .
6. Spring/Hibernate value addition
Spring Templates (For JDBC, Hibernate) I can't forgive a experienced person who is unaware of these APIs or he should be able to explain the home grown utility to reduce the # of lines of code in persistence layer
7. Lazy, Eager & Batch fetch strategies (With hibernate context if the candadate is hibernate aware)

Overall application architecture:
(These are must for architecture position)
1. Layering of application and their importance.
2. Technical benefits of EJB & how i tries to solve all these issues.
a. Security
b. Transactions
c. Distribution
d. Scalebility
Pragmetic aproach here should be appreciated rather than violent pro-against stand on EJB's.
3. Lightweight architectures
4. Design patterns & their benefits
You can pick up any pattern & discuss on the same, It's amzing to see people's answers when I ask difference b/w Singleton/class with all static methods & use get/set methods of javabeans :)
5. OOAD
Difference static binding Vs Dynamic binding typically discussion should start with questions like can we extend String class (with String being a final)
6. Knowledge of existing frameworks & shortcomings of those.
7. Asynchronus communication JMS, Thread, MDB

After this I have this matrix to measure the technical competence

Technology Ratings (1-5)
Core Java 3
Application/Framework 2
JDBC/DB 2

I will enter my rating (1 min & 5 Max), & it is more 5 in any he should be hired or can be considered for next round. If it is 3 in all the cases he can be considered.

In the same fashion non-technical competence can also be considered. But I don't want really want to consider this (May be as input for HR guys)

Criteria Ratings(1-5)
Previous Company/Projects 3
Academic Record/Stream 1
Attitude/Communication 2

For management position
1. Estimation techniques.
FP, WBS
2. Conflict solving ability
3. CMM levels & overall quality processes

Any thoughts on this??

Thursday, December 07, 2006

It is very important to articulate the expectations on attitudes and behaviour from new developer coming into software development team.
Here are the few points I tried to come up with...
I want to ask them to take oath on these principles

  • I will not resist learning new techonology to solve problems, but enjoy the chance to do so.
  • I will not blindly accept any technology or innovation but will always try new solutions, I will research & keep up on news about new solutions & never feel unconfortable to modify the source code that I am not familiar with.
  • I will not throw out the same working solution to a problem every time. I want have the good track record of introducing new ideas & technologies
  • I will try solutions without complaint or fear of failiure (& always own up the responsibilities to correct the same & never insist on using the same solution behind other's backs to management
  • I will never undermine the technology & always ready to accept good things coming from all the ways.


Tuesday, October 24, 2006

Java Framework Information Fatigue Syndrome

'We're often seeing a failure of concentration. We're seeing a loss of motivation, loss of morale. We're seeing greater irritability.'
-- Psychologist David Lewis
This was attributed to ammount of information that a person has to take in general these days & I guess same principle can be easily applied to Java space as well. There are so much of frameworks and patterns. I have seen this syndrome in many senior Java developers. It is becoming harder to cope this day after the day.

In today's competitive business world it is imperative that everyone delivers at a pace that has never been imposed upon in the past. User expectations risen sharply & there is growing trend of technical awareness & knowledge how technology can help to deliver products with shorter time to market impose an overwhelming challenge to business.

More often than not it is discovered that the programmers/designers requires technical assistance and guidance to be able to successfully deliver. It is also observed that strong technical people get caught in several non-technical (but important) aspects. This quickly results in shift of focus, diluting the drive towards technology & there is no time validate the information & the quality of web frameworks. There is so much of options to choose & there is no time to spend in learning.

Annual increase in the rate of change of information is moving at alarming speed. A modern java programmer after spending about 18 months to attempt to master JDK 1.4 >25,000 methods, now he must learn an additional 8,000 more methods for JDK 1.5 ( >30% increase) Now we have huge JEE stack with all the tons of web & persistence frameworks.

So forget about mastering all the stuff, it is nearly impossible to master all the technlogy stack with fighting ever ending project dead lines. Can we afford to neglect these trends? Not possible ! especially people in technical stream (Like me) & decided make living out of technical career, the situation is frightening.

So what's the way out?
There is no way we can get out of reading/learning new frameworks; We have to understand and appreciate the innovation to stay competetive. We have to spend quallity time in going through the code/innovation.

I think the best for us to follow the best brains. I consider Rod Johnoson & their ilk of Spring are the best bet. We have wasted enough time on EJB's & many JCP specs. JCP seems to be worried about only the technical excellence (Making generic very clever specs) but forgetting the developer life. Even big guys like IBM & BEA didn't help. It was Rod & his ilk who made our life simpler by making us to write lesser code & hence lesser bugs. So that we can spend more time with our family.

Even if people with same caiber like Gavin King (JBoss Seam), Geert Bevin(RIFE)... can innovate better, Spring team should be able to adapt these innovation without any hesitation. (By seeing there track record of DRY). So it is best for us to stick to them.

Final words,
The key to survival and indeed productivity is to have a firm grasp of fundamentals, and to be able to assimilate just-in-time information.
&
Blindly follow the Spring.

Saturday, October 07, 2006

Java Design patterns - Few tips to learn.

Learning & remembering design patterns is a difficult task. The real world working samples & real world metaphors on how really a pattern is used to effectively to solve a problem in a given business context is very important in learning patterns.

I found this article on http://mahemoff.com/paper/software/learningGoFPatterns/ is very useful in that context.

I really liked idea of coming up with metaphors for each pattern & I think if the metaphor includes some of the open source code as samples it will be really great. Cooper's book on design pattern gives only the java SWING samples & I guess it would be better we can extract the patterns samples from popular server side java framework. 90% of java programmers write server side programs & not the SWING.
Unless untill these pattern samples are captured from successful implementation (with real productivity improvements) I really doubt the effectiveness of the patterns. I felt the samples provided by Martin Fowler (Both in Archtectural & Refacatoring pattern books) are mediocre & outdated.

Here are the few steps I suggest to learn patterns;
1. Buy this book, best book to learn & remember patterns
http://www.oreilly.com/catalog/hfdesignpat/
2. Go through the popular open source framework code (Hibernate, Spring etc...) . Most of the class name start with pattern names & it is very grasp their usability that way very easily
3. Don't ever force yourself to use patterns in your code/application. It is very important that it should come naturally & read the related anti-patterns before using it.

Remember real stregnth of patterns is when it is used as design communication tool & hence it has to be correct, precise & contexually relevent.
Bookmark and Share