Sharing an IntelliJ project via VCS

I found this post Sharing IntelliJ IDEA Project Files in Version Control via the twitter-sphere. 

They talk of all the usual stuff like ignore the iws file, but they introduced a neat trick for project variables which I didnā€™t know about.

If you edit your ipr or iml files, you can replace values with the text $whatever_project_var_you_please$ and then when someone else checks out the project and doesnā€™t have the vars set, a Configure Path Variables dialog pops up and asks you to set them.

Iā€™ll be keen to give this a try at work as we recently switched from .ipr to the .idea dir format to avoid some conflicts weā€™ve been having, but I suspect that doing so is not the silver bullet alone, the above tip may help ā€“ by replacing what varies and let the devs set variables locally.

The next questions to ask is how this gets handled by Teamcity (I presume there are some system properties that you set in the team city project) and how in Idea, you can change these project variables.  But otherwise its a very neat tip.

Edit: To change these vars, I believe you can simply go into the File->Settings->Path Variables ā€¦ Simple

Netbeans JavaFX plugin experience ā€“ and what about IntelliJ Idea?

Warning: Many of the following criticisms are superficial, but I wanted to document my experience of learning a new IDE.Ā  Iā€™ll come back to this post in a year to see whatā€™s changed.

I should also state, Iā€™ve got nothing against the Netbeans editor itself, and respect how it is able to support all the new Java platforms and standards in line with their release to the public.Ā  Because of this, itā€™s as easy to adopt a new sun-backed Java platform enhancement as it is in the .net world, and the support provided makes it easy to learn new Java technologies in both regular desktop and enterprise space.

So now for the whinge:

Trying out Netbeans 6.8 and its best of class JavaFx support left me a little disappointed.Ā  Following the JFX tutorials was ok, but copying the code from the tutorial into the IDE was a little frustrating since the formatting would be all screwed up.Ā  Organise Imports (CTRL+SHIFT+I) plus Format Source (ALT+SHIFT+F) become my new best friends.Ā  Maybe its because Iā€™m trying in a new IDE, but I dont remember having to do these things with Idea, imports would resolve (or prompt for resolve) on paste, and formatting would normally match the target source.Ā  (Iā€™m a spoilt IDE brat, lol)

The JavaFX palette left me wanting as well.Ā  Sure I could drag and drop a template from the palette into my code, but like the copy and paste, it didnā€™t respect the formatting of my code.Ā  Sure once the template was there, I could tab b/ween the fields within the object literal, but my first instinct is to make the code pretty and doing a Format Source.Ā  Doing so meant that I could no longer tab between the elements.

It would be nice to have a visual guide as to where in the code you could drop a said palette template into (perhaps highlight code blocks yellow if it was within a content or def assignment, but orange if it was somewhere else in the scenegraph it didnt make sense).Ā  This is because if you put the item in the wrong place, you have to spend a minute figuring out the order of ] and } in order to get a compiler fx class again.Ā  The scenegraph should make it easy to identify which element belongs to what, but its easy to miss a line.

Iā€™d also like to see elements which took a sequence (like content) but only had one element assigned to them (eg content: Rectangle { ā€¦ }), become a group or a sequence automatically when items were dragged into them.Ā  (eg: content: [ Rectange { ā€¦ }, Circle { // i just added this } ]

I also found that CTRL+Space within an object literal to find the other properties didnt always seem to work.Ā  As I got more experience with the IDE, I found this happening less.Ā  I think the problem could have been to dragging and dropping in the wrong place, as soon as you have a syntax error in your code, the intellisense isnā€™t going to work, regardless if your CTRL+Space is just within the braces of the object literal.

Also in Netbeans, a lot of refactoring functions were neutered. Options like Move or Copy class or even create a variable (ALT+SHIFT+V) would prompt they didnā€™t work in the context of the file type.Ā  Doing a block comment using the default comment keyboard shortcut (CTRL+SHIFT+C) didnt work, and its menu option under Format was greyed out, but you could press CTRL+I, type comment and select Toggle Comment for it to work there.Ā  Strange.

End whinge

To Netbeans credit, the JavaFX preview is a great feature to help pick up the language, and of course the different profiles (Desktop, Common, Mobile) along with the integration with the the emulator is excellent.Ā  The above things are pretty superficial and since Netbeans has the jump on the other IDEā€™s, will probably get around to addressing those other concerns later as the platform is adopted by more devs in future.

Many of the above criticisms are borne out of wanting more refactoring options and polyglot support like Iā€™m used to in IntelliJ.Ā  At present IntelliJ offers a mere syntax highlighting when opening Fx files, but thats about it.Ā  What Iā€™m really waiting for is the super refactoring options when working with a project that has Scala, Groovy and other facets.Ā  As well as just being able to use the refactoring options that Iā€™m used to (eg rename a var/class in java, and refs in the Groovy and scala get changed too).Ā  However, those things arenā€™t automatic in IntelliJ, I appreciate those refactoring features would have to be developed specifically for JFX, just as they have been for Groovy and Scala.Ā  The list of refactorings isnā€™t as wide as the java ones.

IntelliJ Idea

So what else can you do to get some java fx love in IntelliJ?Ā  At present not much, you can compile & run JavaFX using Intellij using the External Tools function or by using the ant script as specified in the link.

There was a JavaFX plugin that was being developed by Brian Goetz back in 2008 but word on this has gone quiet.Ā  In 2009 a blog was started by someone about writing a JFX plugin for idea but this too has gone quiet.

So its a case of watch this space.

Pair Programming and Programmer Psychology

This is a great article in the IEEE Software journal looking at why pair programming does and doesn’t work and why it should or shouldn’t work.

In fact the article doesn’t have all that much to do with pair programming but looks at how developers work with each other. The article sites 4 mechanisms where pairing can have influence.

For example, the great scenario whereby describing a problem to an ‘expert’ colleague helps you find the answer, even if the colleague has little idea about the problem or the domain to begin with.  This reminds me of the Code Consultant plugin for IntelliJ Idea. 

Or when someone misses an error right in front of them (inattentional blindness), a pair can pick up the error.  The old psych lab of Did you notice the woman dressed as a 400 pound gorilla in the picture the second time around?

The article talks about how pairs can help keep each other using proper development patterns over change, compile, run, fail, change something else, compile, hope for change loop that beginners use when they first start developing. 

Finally, the article talks about how having a pair helps get a view of your own self since developers working solely don’t usually have their techniques reviewed critically and often, working with a pair also enforces them to do some introspection.  A yard-stick?  People who don’t pair are less likely to do this, and the article sites the scenario where job applicants who claim to know something, get stuck when it comes to explaining these things in practise.

Overall, I think it was a good article, not because it was talking about pairing, but more because it highlighted the tribulations of everyday developers, and how pairing could help or detract as an afterthought.

Multiple Inheritance in Java

This is something thatā€™s usually taught to first year Java students.  Though I had a small mental blank about how it was implemented. I knew that to get around the ā€˜limitationā€™*, you could specify a class with multiple interfaces, but that was only half the solution. What about the implementation?

Surprisingly it was hard to get a clear answer from the fractal patterns that Google found, a lot of developers saying, ā€œoh yeah, you need interfacesā€ but it didnā€™t sound like they knew themselves.

So, this was my way, probably duplicated a zillion times, but here you go, hereā€™s a way:

We are going to have three classes, A, B & C (sorry for the lack of imaginative names here).  Class C will extend both A & B.  Weā€™ll extra interfaces for A & B dubbed Ai & Bi respectively.

First an interface for the classes we are going to ā€˜extendā€™ from.

   1: public interface Ai {

   2:     public void aMethod();

   3: }

   1: public interface Bi {

   2:     public void bMethod();

   3: }

and now some implementation:

   1: public class A implements Ai {

   2:  

   3:     public void aMethod() {

   4:         System.out.println("aMethod");

   5:     }

   6:     

   7: }

   1: public class B implements Bi {

   2:     public void bMethod() {

   3:         System.out.println("bMethod");

   4:     }   

   5: }

And now for our binding class, C.  The class implements both interfaces, and takes in implementations of each of the super-classes as constructor arguments. The implementations of each method defined in the A and B interface is a delegate to the relevant A or B class that was injected in the constructor.

   1: public class C implements Ai, Bi {

   2:     private final Ai a;

   3:     private final Bi b;

   4:     

   5:     public C(Ai a, Bi b) {

   6:         this.a = a;

   7:         this.b = b;

   8:     }

   9:  

  10:     public void aMethod() {

  11:         a.aMethod();

  12:     }

  13:  

  14:     public void bMethod() {

  15:         b.bMethod();

  16:     }

  17: }

The call to the implementing a.aMethod() is effectively a super() call.  You can then append your own extra implementation, or remove the delegating call and define your own.

So what about the diamond problem, when both A & B define a method that is named that same thing.  In Java if you try to say a class implements interfaces that have duplicate signatures youā€™ll get a compile time error.  If Ai and Bi both had a method called cMethod() then the C class would refuse to compile.

Other ways

My research found other mentions.  If you got lazy, you could have C extend A, and implement Bi.  Then you wouldnā€™t have to write the interface Ai and C would be a little smaller since it would only inject the B implementation and have a bMethod() delegate.

Another way was to have the 2nd class as a static inner class.  Although I have no issue with this implementation working, as soon as I see ā€˜innerā€™, Iā€™m led to believe its re-use value becomes a bit more limited and certainly more cumbersome to refer to.

Notes

BTW, if you want to find a decent explanation of what to do and an interesting reading of a Mixin pattern implemented in Java, then see here.

* I say ā€˜limitationā€™ in quotes because apart from the well known diamond inheritance problem that crops up with multiple inheritance in C++, from what I read, using multiple inheritance could be a code smell.  Certainly the ambiguity that comes from not knowing exactly which one will be called or whether or not youā€™ll know at compile time, or run-time is a concern.  (Donā€™t delay the Bad-news)

Inline Java In Perl

A former perl guru colleague of mine showed me Inline::Java, a way to embed Java in your perl programs and call perl from your Java classes.

The way it works is it invokes javac and creates some bytecode.Ā  It then runs its own perl mapper (reflecting on said bytecode) so that the Perl code can use the Java public methods and attributes.

If you reference a java package outside the scope of your code (eg java.util or your own packages in a separate jar), then this module will also reflect those too.Ā  You can specify a CLASSPATH environment variable so it knows where to look for these jars.

According to the bugs db there weren’t too many though how active the project still is remains to be seen.Ā  Apparently the implementation can be a bit slow but there is a JNI option to speed things up.Ā  I also suspect there is some problem with more unique java properties, apparently the module balks when it tries to see the Oracle 10 JDBC driver jar because of a public synchronized property.

Regardless if you’ve got Perl code that you want to migrate to Java, this would be a nice first step to bridge the gap.

See these sub modules for more info on how you can bridge the gap from the Java side

Inline::Java::Callback, Inline::Java::PerlNatives, Inline::Java::PerlInterpreter

Based on this, the equally cool, Java::Swing project emerged which leverages on the above.

Java Application Examples Blog

Ok, so I went to uni, and in uni they are big about plagiarism.  Not big in the sense they promote it, but big in that if you do it, you may as well consider yourself chemically castrated with no hope for a career in <chosen field> or especially academia ever again.

Uni made me wary of copying work.  It frustrated me to see other students copy assignments the night before something was due.  If you weren’t working something out yourself, you were missing out on the richness of the experience of, well, learning what the assignment was supposed to teach you. 

All this, work it out yourself, got turned on its head when I entered the professional programmer world.  Mentors advised to read as many code samples as possible, it will help you learn.  And whilst code samples aren’t necessarily cheating, and using this code in your own work provided you cite it and the license permits, should also be ok, it took a long time to get away from the stigma obtained during my time at uni…. which really when you think about people who further their academic careers by way of research papers that require you to reference other peoples work, is a really dumb stigma to get across.  I put it down to the plagiarism fear engulfing what should be best practice in normal circumstances.

So with that guilt trip swept away, there was a great blog: Top 5 Websites for Java Application Examples which referenced Java2s, Java DB, and Java Galaxy.  All sites that have come up in my Google searches before when I’ve lapsed my memory and forgotten the trivial convert something to a byte array or whatever other simplistic, seldom used, grassroots technique.

Whilst you cant be taught on these alone, the fact that they support you when you are the most stressed and your memory works its least efficient means I have no problem in recommending these sites šŸ™‚