XPage dev Why Java

I recently received a comment that I would like to address:

Hey Greg. I watched the tutorial and followed it quite easily. My biggest block to Java is why should I use it? I am having difficulty understanding what the great benefits are, other than getting skilled in a new field.
I also am perplexed as to why, for example, would I need a session helper when I have session info with my standard XPage. Another thing, having an object of a Domino document and using the object to read and write fields back to my source data seems rather tedious seeing that I can read and write straight to my source data?!?
I’m missing something for sure because everyone seems to be wanting to go the Java route.
If I had 1000 users open an XPage application and view a document then why create 1000 objects, one for each document and session helpers. Does that not take needless memory?

But I am looking forward to the next tutorial!!

There are a few questions in here that I find good to ask and that can cause some debate in the professional world.  I will start off my answering my part to the question why.


Why use Java instead of JavaScript?

Some people will say, “Use Java!  Why?  Because .. white noise………”

I am sure that all of you have heard the “Because it is faster”.  Faster for whom?  The computer?  The difference is negligible.  In a test done by a colleague, iterating over a view with 75,000 documents, touching every one completed only about two seconds faster when done in Java as opposed to JavaScript (14 seconds rather than 16 seconds).  I say, if you are doing something in the real world that needs to touch that many documents and the user is waiting on you, then you are doing something wrong!  I do not care if that is done in JavaScript or Java.  Now, keep in mind that he was doing this for a test and to see if there was a measurable difference.  There was.   We thank him for doing his tests and for sharing the results with us! But to that end, lets make our first point:

Use the correct tool for the correct job!

First off let me say that I find this debate stupid.  It reminds me of the debates that I had to listen my friends engage in regarding their preferences to C# vs Java and Windows vs Linux or Mac.  Every task has its goals, and every goal has its best practice and optimized structure and technology.  If you are working on a project that will be small, used by few, and only requires a few pieces of JavaScript code, than maybe it is best to use pure JavaScript in your implementation.  Likewise, if you are going to be building an application that has code that focuses on customizing the controls, then that too might be best to be done in a pure JavaScript environment.  JavaScript simply is the tool for those jobs.  If you are going to be working on code that needs to be well documented, used by others, maintained by others, or shared between applications or one day may be shared by applications, then Java is the way to go, no doubt about it.  It just is the tool for that job!  Likewise, if you have to build code that handles jobs and working with many documents, then the code does not belong in either of them, but rather run in [scheduled] Agents.*

I know that the separation of Business Logic and UI Logic is another big push the separation of JavaScript and Java.  To that I say bullocks.  Because of the JSF infrastructure and the ability to change existing HTML generation code into actual controls, Java might indeed be the best place to store your code rather than using JavaScript to generate the code for <ComputedField>s set to HTML.  Granted customizing those controls must still be done using JavaScript, but depending on the use case, you might even have some of that logic kept in a Java Object.

Yeah, but, Why Java

Maintainability, Readability, Usability.  There is no better way to put it.

Think back to another Blog Series I did when I discussed the issues I had with debugging JavaScript.  I mentioned that the stupid error

if(apples = oranges) doSomething();

as opposed to:

if(apples == oranges) doSomething();

and that this was an error that should be caught, but never is.  This is my number one reason for pushing for java.  The stupid errors that can cost a great deal of debugging time are caught by the compiler and are easy to fix.  Another case, you have a larger function.  You are dealing with many different kinds of objects, and you accidentally try to set a document object to a documentcollection object.  In JavaScript, you will not discover this error until you run the program and you get an error that states that the desired function is not available, or you end up debugging because it manifested itself in some other way.  In java, a variable declared as a document can only be used for a document.  The error will never happen.  My first reason for pushing for Java in XPages is Usability.  You are not going to have as much code that contain errors and his hidden or uncaught by a compiler.

Another reason why I say usability is improved with java is the tooltips/inteli-sense or whatever else you want to call this feature.  If the javadoc for the API you are using is connected in the build path, you are able to look at detailed information provided by the developers, assuming that they maintained their javadoc well.  This also goes for the javacode that you write.  Of course you have the ability to write and look up comments in SSJS, but they do not show up like they do in Java.  If I am wrong about that please tell me, but I do not believe I am.  In Java, you have the ability to simply use shortcuts to import packages and classes using the inteli-sense, keyboard shortcuts, and see what members and functions are available in those classes whether they are known to designer or not.**  You write a class, that class is known by the inteli-sense. It is that simple.  It may not seem like a lot, but when you can quickly see what code does what, import, and comment all in one place, then why would you not want to?

Readability is also highly improved.  Lets say you have your classes all defined and you are using them.  In JavaScript, you do not have the ability to click on the method being called and have that method opened up in designer, at least not when those functions are defined in two different files.  With Java, this is not an issue at all.  Click on the call, file opens right to the spot in question, time is saved, finish.  The reader is just put into a position where it is efficient to go through your code and figure out what to do.  Another thing: in that same series I linked before, I mentioned that embedded JavaScript function calls are not listed in the outline view.  In Java, this is not the case.  All members, functions and embedded classes and their members are listed in the outline view thus adding to the readability of your code.

Because you are writing your code in a place that makes it readable and because it is in a very usable format that will not require a lot of learning about your code (ie reading your script files to learn how to use it and instead getting the comments via the javadoc and inteli-sense), you are saving time of future programmers who are using your code.  You are also saving yourself time when you need to change something 16 months later and do not remember what you did any longer.  You are also creating something that can be added to later!  Let me explain this.  By saying that, I am not referring to adding some code at some point down the line.  If you want to do that, than JavaScript is completely reasonable.  I am talking about finding out that there are these few classes that you use over and over and over again.  You have to port them into every database you own.  By having these classes in java already, you are in a position where it is not that expensive to port them over into OSGi Libraries that can be put on the server as globally available beans.  If you have them in JavaScript, you also need to translate them.  Furthermore, if you have an HTML generation function that you use for a computed field that displays html code, then it is not so far off to take that code one day and make a real JSF control out of it.  Likewise if you decide to make that code into a control, you can share that control via an OSGi extension.  Overkill?  Nope.  You are simply writing your application in a way that allows for future sustainability.

Why Java?  Not for the negligible speed increase, but for Maintainability, Readability, Usability.


Why a SessionHelper object?

This question refers to a video that I posted as a part of my Java XPage tutorial.  I created a Java object in order to access the session.  First off, I just would like to mention that this is primarily a class to assist other Java classes and not JavaScript.  I most likely would not put this into the faces-config file and register it as a bean, although I have done so in the past.  As the object stands right now, it is merely a list for static methods.  This is a poor candidate for a bean as an instance of the class is not necessary to use its functionality.  Get it when you need it, otherwise leave it.  The only functionality in this particular SessionHelper class that might be helpful down the line in JavaScript is the getODASession().  Otherwise, if I remember correctly, ODA creates a bean called “openSession” that allows you to access the ODA session object simply in JavaScript without any sort of self made utility class.  Even then, you could always access the ODA Factory utility.

There will be some who will say that my approach to getting a session in an external class is a bad idea.  I can only say to that that I have not had any issues getting the session or the database from other helper objects and methods.  An absolute no-go would be getting the objects under the database object.  Your cleanup would either be messy, or give you NotesExceptions depending on how you went about it.


 Java and Object Retention

Beans and the scoped maps are there for a reason.  Lets assume we have an external database that holds language information: labels, keyword lists and so on.  A typical approach from a few applications that I have taken over require that each form retrieve its own values using @DbLookup to fetch them from the source DB.  In XPages, this is dumb.  Likewise, this would be an asinine thing to do for a computed field or hiddenText field, it would be a horrible idea to put it into the viewScope, a dumb thing for sessionScope, and perfect for an applicationScope Bean which maintains a  HashMap of document labels and keywords based on some key and correctly retrieved by reading out the sessionScope bean that contains the preferred language information for the current user as read from his user document.  By putting it into the applicationScope in a bean you are creating a way to cache your expensive values and provide a way to reset those values at will.  Of course you can do similar things with JavaScript, but you cannot put a JavaScript “object” or function into the scoped maps.  We will be getting into such patterns later on in the tutorial.


Conclusion

As Scotty said in Star Trek V, “How many times do I have to tell you, the right tool for the right job!”  There are times when Java is the right tool, there are times when JavaScript is the right tool.  Do not let yourself be put into the position of thinking that only one option is correct, but keep an open mind and remember that future sustainability is directly influenced on your programming technique and tool decision.  Don’t get caught up in the stupid debate of which is better.  Do not listen to the “java is faster” crap.  The real reasons go much deeper than any of that, and if you are having performance problems, then there are much more likely causes than the language choice.

Java is also the only place where you can define classes for beans to be used with scoped maps.  Otherwise, you are just saving raw variables into your map and cluttering it up.  Use Java to correctly store and access your information so that it can be readily accessed and executed.  I am a strong supporter of Java use in XPage applications.  I hope that this article has helped explain why.

 

* This is really a case by case decision and must be decided upon specifically when designing the application.
** By “known to designer”, I am referring to basically hard coding the inteli-sense.  I actually do not know how this works and am merely guessing.  Does not matter though.

About reederProgramming

I already have an about me page, so I will just put a quick bit of info here. I am a Notes/Domino developer at holistic-net GmbH located in Hannover, Germany. I use Java primarily at home and as often as I can at work. I have dabbled in C# and a few other languages and platforms. This website started out as a place for me to quickly store and access some of my most important how-tos and has started to become a place where I can really help others too!
Tagged , , , , , , , , , , , . Bookmark the permalink.

4 Responses to XPage dev Why Java

  1. Do not listen to the “java is faster” crap. >> generally it is, because everything is loaded into memory and ready to go.

    • reederProgramming says:

      Agreed. As I said in the post, the difference is negligible. Use java for maintainability, readability, usability.

  2. Steven Podrouzek says:

    I like you explanation very much Greg. Knowing more tools the better for us! I like real life examples on where you would use what. Been viewing more Java in XPages videos e.g. notesin9 has got a few videos. My next application will have some java in it, just for fun!!

    • reederProgramming says:

      Once you start using java, get used to the IDE, and then get comfortable with Beans and EL, you will want to put everything into Java and only use JavaScript for accessing your Java objects and customizing your controls. Later, when you get even more comfortable, you will find that making your own controls for certain things is also an awesome side effect of having html generation done in Java. It is so easy to change things for future use cases without having to add “translation” time. (We will look at such html generation code in the next three videos that I did that are going to be released on NotesIn9).

      Also remember, “Right tool for the right job”.

Leave a Reply

Your email address will not be published. Required fields are marked *