Tuesday, October 23, 2007

How to add DZone voting to your Blogger blog

Update:Formatted XML and removed top-level div tag and added instruction to expand widget template, thanks to James.

DZone released voting scripts which allows site visitors to vote from the site itself. You can find voting widgets for Wordpress as well as for Jroller, but nothing for blogger :(.

So here's simplest way to add it to your Blogger blog, Just copy/paste lines below to your Blogger HTML template (after expanding widget templates):
<div style='float:right; margin-left:10px;'>
<script type='text/javascript'>var dzone_url = '<data:post.url/>'</script>
<script type='text/javascript'>var dzone_title = '<data:post.title/>'</script>
<script type='text/javascript'>var dzone_style = 1</script>
<script language='javascript' src='http://widgets.dzone.com/widgets/zoneit.js'/>
</div>


right before

<p><data:post.body/></p>


Here's the image screen shot:



Click on it to enlarge. You might want to customize it a bit, just try out different div styles. Developers, Keep voting!

Monday, October 22, 2007

On IClass (anti-)pattern

I'd been re-reading this post by Sidu and the comments on it. I had to do exactly similar to IClass anti-pattern for one of the development effort. And I think having IWhatever completely makes sense for the current state of affairs in my development environment.

While it's unfortunate, most of the time, to have an Interface for just about every class in the code-base, It does have certain uses in some situations (apart from the (IoC) frameworks which mandate you to have them) like:
  • Support parallel development.
I will define parallel development as a situation where there are many components involved in developing a software and your component (or module or whatever..) and it's arbitrary version has to support other components (and their integration tests depending on your environment). For example, for something as important as (so called) service layer in an Enterprise application you need the Interface with single implementation at one time, it might just be a 'placeholder implementation' for a dependent component while the new version being developed parallely. It just makes sure that there is something to hold the place which can be tested easily. This is one more learning from distributed development, you're not alone developing it man!
  • When using Generative development methods
If you use code generators heavily, say, most of your code-base is generated out of models (UML, BPMN, BlahL ...) then depending on the generation pattern you'll find interface for just about every implementation (or other way around for EMF generator - one implementation for each interface). This is necessary as you might want to customize forward engineering by overriding parts of generated artifacts.

Most of the Eclipse code does have this pattern, for every class there's IClass. For Eclipse, In my opinion, it is justifiable (to some extent) to have them considering it is a development platform rather than just an application. However, I would be more than interested to know conceivable reasons to have those IInterfaces which I might be missing.

Tuesday, October 16, 2007

How would you compare XMLs?

I am supposed to deal with something similar to comparing two gigantic XML documents in wild ways.

I can think of several upfront techniques to achieve it which might involve performance and maintainability trade offs. As you might know, writing code for parsing XML by hand was the the activity of ancient times (hey there are you still writing code for parsing XML?), today we've plethora of tools to parse, bind and persist XML with very less pain. I came across several XML binding libraries like JAXB2.0, XMLBeans, JiBX etc (and given a change why not EMF?). JiBX seams to be interesting but since I'm bounded by not using open source at will, I tried JAXB2.0. The XML schema provided to me was a huge XSD document, the JAXB binding compiler spitted 550 Java classes out of that.

A test driven simple recursive depth-first reflective (opps, too many adjectives) traversal algorithm on the generated object tree sufficed the requirements to identify XML delta information. This was very obvious and pretty fast solution (fast to develop), the downside is, it would require maintenance of 550 generated classes, though they can be regenerated and synchronized with the help of XJC ant task but still the memory foot print and object creation time can be circumscribing for production code.

The other approach I tried was calculating XML diffs using XML processing. I found a nice little utility library XMLUnit among others, which does almost the same what I want. XMLUnit is a tool primarily for unit testing XML-intensive applications, It is very small with clean API and well documented (if you want to read i.e.). There are several utility classes which shields you from looking/writing ugly XML processing code which I used to get the XML diffs. Although I need to poke around on XMLwith XPath still because of the complex requirements.

I would have tried my favourite XStream as well but FAQs suggests me not to, anyway, What would be your strategy to deal with something like this ?

Wednesday, October 10, 2007

Pain of Sourceless Development for an Open source developer

I'm proudly an open source developer, working on Java, And I just realized I can't live without source code of every framework or library I work with. I know many of you are like me, we know we speak code, we write it, we hear it and we communicate in it. Not having source means we are impaired.

Before it happened to me, Neither could I imagine nor perceive myself working with a piece of software without source code, It was so common to have code before, and hey shouldn't that be included in programmer's bill of rights? But, First time in last few years, luck had it and I am working with a closed propriety framework, its painful for all. If you're in the great world of open-source software, you might have started thinking how painful it can be to program without code.

Not having source code means lot of things, debugging becomes nightmare which not only makes you feel stupid to debug but also it frustrates as you feel back in blind environments like the one in M$'. If there's a bug you have no way to look what's going on in the library or framework, you would wait till their (software vendor's) "support team" identifies or guesses what's the problem and gives a patch, which would take like weeks.

In Java, You can try decompiling the binaries, it's easy with Java unless binaries are obfuscated, this can be helpful to an extent. But most decompilers are not very debug-friendly, it would generate source in arbitrary assumptions, so unless you recompile the source back, you're stuck. But decompiling, recompiling and then looking around the weird identifier and method names and reordered statements will drive you crazy, but for Java developers there is a cool eclipse plug-in which would feel like a medic, it's name is JadeClipse. Though, it's too much for just knowing what's going on inside the third party's code.

While wider acceptance of Open-source in the industry is a definitely good thing, but beware it makes you feel less comfortable working with proprietary closed source softwares once you are used to it. I'm still wondering, how long would closed source software exist in platforms like Java or languages like Ruby and likes?

But please, for the hack's sake, please let us, the software developers on the mother Earth, see the SOURCE CODE.

Monday, October 08, 2007

The art of not repeating yourself

I discovered a great bug in the my code today, It was weird hibernate related bug. If you have been working with hibernate enough you know you've to be careful about the ordering of statements relating to your persistent object model, that may sound little odd but that's how it is with hibernate. If you hit the SQL constraint exception and you've multiple joined entities, be cautious, you might want to isolate call to some many-to-one references earlier than populating your domain object.

There are several ways you'll force flush to hibernate session and that flush (a flush can be a batch update) will cause DB validation and if that flush happens before statements that set the non-null fields, you'll be greeted with boring exceptions. They are not very interesting exceptions to deal with and it might take sometime before you understand what's going on. In general, debugging hibernate code is never interesting unless you've less experience with some outstanding ORM patterns.

The problem that I hit today was slightly different than what I described though (but I've experienced similar problems many times), My problem was that I was inadvertently modifying a read-only row which had 'long' primary key with '0L' as value, if you've worked enough with hibernate types you know 0 (zero) is the default value for long and integer types. It is just because I was modifying that row by mistake hibernate decided to save that row with different primary key (new sequence number), it assumed 0(zero) as default value (dirty value in Identity Map) for long primary key type, this is bad assumption but there are less ways any ORM framework can find dirty objects. So from next time onwards be aware about these little details If you hit problem of similar nature.

How I prevented myself from repeating was the way of writing a simple JUnit test case which isolated the problem in my code and pointed me to the place where I should be looking without long (very very long) J2EE debugging session. Let me tell you, that's great relief! With that test case I was able to refactor the code and resolve this bug without too much pain. Which makes me and my manager happy :)

If you've been experimenting something similar, I would be glad to know..

Friday, October 05, 2007

Layman's review on Modeling etc. features in Netbeans 6.0

I know, I'm not going to sound very good for this post on the planet and expecting very less dialog in form of comments on it, but anyway...

The Java development blogosphere is experiencing a new wave of marketing campaign, yes everyone is talking about Netbeans, but hold on, it's not just viral-marketing, I found reviews where some says it's ruby support is the bomb, some are finding profiler better, someone is just finding it coming-up with better attitude:). Well, I've to tell it, I'd tried Netbeans in various forms in last few weeks, I can tell you it can do pretty much what all IDEs do these days. Netbeans might be looking less refreshing with all that swing (elegant inside!) rendering, but giving Eclipse Key binding, for example, really shows it's eager to compete your favorite IDE. In this post I'm not trying to say Netbeans is better or something like that, my love for Eclipse is unparalleled for various reasons, but yes I am, sure, eager to tell you my modeling experience with Netbeans.

Before I do that I'd just write down my opinions about Netbeans 6. I would say, profiling is really pretty good, I don't need to understand nefariously complex architecture to use it, I don't need to go to help to use it. See you would say, TPTP is complex only for lame users. Are you talking about UI? how many perspectives you have as part of TPTP? which one to use for profiling? and what you've to do to profile remote Java application? can you answer it without looking in documentation? Don't get me wrong, I'm trying to be as unbiased as I can.
As for Ruby support, It is just about as good as DLTK Ruby IDE, I'm no Ruby big shot as yet, but things works pretty OK with DLTK as well, cheers! Netbeans has fantastic UI editor though, VE plain sucks, oh and is it around now? last time I heard it's dead.

Now on modeling, To My fellow Eclipse committer guys, let's face it, Eclipse doesn't give anything in modeling front which an average user can use out of the box. It might have innovative and wonderful framework / UML infrastructure for building tools around it though, which is very well appreciated by 2% eclipse plug-in developers (including me), fine. So for our hacky interests EMF might replace MOF but Eclipse users are not getting any advantage of it. I know, some might say it's no big deal to not have modeling as a part of IDE, but hey that's the whole point, everything from single place is the point. Few would like to use Visio to model class diagram and then write code for it. We've quite a few other reluctantly-opensource projects, in the community, controlled by arbitrary licenses which are not very useful.

For those who don't agree, download and try Netbeans for modeling, You will, for sure, appreciate the efforts put into it, it can do hell lot of stuff (It can't copy and paste model elements in single diagram though :) ), Java reverse engineering, simple modeling, in context design pattern appliance, it's just there and works! you don't need to find something on Internet and read it's license thrice before using it. And the best part of it, It doesn't cost an arm and leg to get these features. OK, it might behave little in-development but it's just there for you, try it!

Eclipse is the great Plaform and I can't even start to compare it or any of it's innovative aspects with something as trivial as Netbeans in that context, but guys, shouldn't Eclipse consider it's "end-users" who expect nice features? Advocacy on plain words don't help, it needs strong backing of solid product which offers something more. Learn Learn..

Thursday, October 04, 2007

Dealing with tedious job

So, You are stuck with the wrong job and want to do something different to get your inner-geek rolling. Well, my friend let me tell you what I've done so far when I'm in such situation, I get frustrated initially with all that chore and boredom, naturally. I know it makes you feel bad to do stupid work, but then you can be creative in that too!

I was supposed to work on completing a framework which was designed by senior team, what I'd been asked was to do all repetitive soporiferous work (I'm serious..Zzz..). Well I completed it pretty quickly but by then I thought I would do something interesting to take revenge and I jot down an automated test framework to test entire framework, now thaatt was pretty challenging. "Developer testing" makes you think wise, and I was able to understand framework quite easily. With all that fancy techniques put in the test framework, the "original" framework extensions are effortless to test now.

Well that was one thing, but sometimes you just get enterprise-world-weary to write data centric service layers with focus on all those if/else business requirements. Man, you know you've gotta a chance to show how creatively you can utilize design patterns to satisfy those requirements. Reuse your code - stop copying/pasting it, refactor it - clean it up a bit, you know sometimes you can be so creative, for example replacing repeating loop and other redundant Java statements with template methods and anonymous class like this(or if your language is pretty, use lamda forms):

public class IMWhateverClass {
public abstract class IMDoer {
private final Set elements;
public IMDoer(Set elements){
this.elements = elements;
}
public void process() {
while (elements.iterator().hasNext()) {
Z var = elements.iterator().next();
if(!doProcess(var))break;
}
}
abstract public boolean doProcess(Z arg);
}
public void doSomething() {
new IMDoer(new HashSet()) {
public boolean doProcess(String str) {
return str.matches("any");

}
};
new IMDoer(new HashSet()){
public boolean doProcess(Set arg) {
return arg != null;
}
};
}
}
I know it's uselessly verbose in Java but isn't that cool compared to copying and pasting it around? And over-engineering is sometimes fun..

So what I would suggest is, if you're at the place where you don't have enough challenges in your daily assignments and you can't switch job ;), you can always create some excitement on your own. Heh, if you think how will you get time for this, Man forget all these, you're 9-5 laborer.,

But, If you've done something interesting in such situation then I would be glad to know and try it.