Perks of learning JavaScript or a new language

For 9 years, I wrote majority of my professional code in Java. I still do. For last one year I wrote lot of JavaScript. Learning new language gave me different experience and perspective. I am not expert in JS. In fact, I hesitate to call myself  average. So take any advice in the post with grain of salt.

Initial fear

I poked at JS several times. The absence of static feedback while writing code scared me. I never thought about it while writing SQL, XSLTs etc. In college, I didn’t know what eclipse is. I was happily using Turbo C Editor. I was just biased without getting my feet wet in JS.

Change the code and refresh the browser, you got your output and errors. The results are quick. After getting used to this, I don’t miss static typing, and any IDE.

How I Learned?

Some thoughts and advice

  • Learning new language introduces you  to new concepts. In this case ,I got my hands on concepts like functions, closures, lambdas, and prototypal  inheritance. It also encouraged me to try Scala.
  • Learning JS is not sufficient for web development. Know CSS and cross browser incompatibilities.It saves you lot of time.
  • Some of the best practices of coding in Java applies here too. For example, Single responsibility principle. Don’t cram all the code in single function. Learn from frameworks like Backbone. Explore the other best practices of the language.
  • Like in Java world, there are frameworks here too. For example, underscore, jquery etc can be considered as equivalent of guava or apache commons. Use them, rather than write your own utilities.
  • You can be productive in different ways. For  example, I had to develop UI  using REST api which is under development. I used node and express js to simulate the REST api, which is damn easy. I am not saying you should learn node and express just for this scenario. If you learn new language, your alternatives will grow.
  • Practice what you read. I have to tell this to myself over the years, several times. I made the mistake with other languages like Groovy, Python etc. I just read the books and didn’t write much code. Implementing a project changes everything.
  • Finally, don’t worry about whether you will be as good as your primary language. Probably you won’t. But does it matter when the journey itself is fulfilling?

Spring security, DWR and session time out

In AJAX applications, using Spring security, handling session time out is a bit tricky. The problem is summarized here. Fortunately with DWR it is easy. DWR provides a way to handle errors, exceptions etc globally. Check  more on this here . As mentioned in the doc, you can use ‘textHtmlHandler’ to handle session time out. Here is the snippet,

dwr.engine.setTextHtmlHandler(function() {

//If the current page is secure, Spring security redirects you to login page
document.location.reload(true);
//Or you can set the document.location to your login page.
})

PL/SQL function to claculate minutes overlap

I am not good at PL/SQL, I wrote a function which gives minutes overlap between two date ranges. Can this be enhanced further? Are there any function libraries available for date time calculations?


CREATE FUNCTION DATETIME_RANGE_OVERLAP_MINS(startDate1 TIMESTAMP, endDate1 TIMESTAMP ,
startDate2 TIMESTAMP ,endDate2 TIMESTAMP )
RETURN NUMBER IS
overlapMins NUMBER:=0;
startDateMax TIMESTAMP;
endDateMin TIMESTAMP;
BEGIN
IF ( startDate1 > endDate2 OR endDate1 < startDate2) THEN
--No overlap
return overlapMins;
END IF;
–Find maximum of two start dates
startDateMax:=startDate2;
IF startDate1>startDate2 THEN
startDateMax:=startDate1;
END IF;

–Find minimum of two end dates

endDateMin:=endDate2;
IF endDate1
endDateMin:=endDate1;
END IF;

--We will get overlap in days. Convert it to minutes.

overlapMins:=ROUND( TO_NUMBER( CAST(endDateMin AS DATE) - CAST(startDateMax AS DATE)) * 1440) ;

return overlapMins;

END;

 

Pramati @ Sun Tech Days 2008, Hyderabad

Sun Tech Days will be back in Hyderabad from 27th Feb at the same venue as last time. It’s been 1 year since my first attendance at Sun Tech Days. It was a good experience and exposure.
This time it will be special for me.Pramati got power slot session on 28th of Feb from 4-30 to 4-50 Pm, on Alternate HotSwap for Java EE Applications‘. So be there and you will enjoy it.

Acegi internationalization for apps without Spring DispatcherServlet

Acegi by default provides i18n support(For version 1.0.5, it supports Chinese,French, German and English). Unlike specified in the documentation(please read this before reading this post), you don’t need to specify explicit message source, unless you have existing MessageSource defined with id ‘messageSource’ or you need support for additional languages or both, in which case you need to copy acegi related keys from acegis files(which can be found under org.acegisecurity package) to your respective message bundles. AcegiMessageSource takes care of language bundles provided by default.

If you don’t provide message source, acegi classes use AcegiMessageSource through MessageSourceAccessor(Helper class which wraps MessageSource and provides overloaded getMessage methods). If you call one of the getMessage() method on MessageSourceAccessor with out Locale as argument, it tries to get message in default locale provided to it. If default locale is not set, then it tries to get Locale from LocaleContextHolder. But who sets Locale to the LocaleContextHolder? If you are using Spring DispatcherServlet for your requests,a LocaleResolver will do the job for you. For other apps you need to do this before authentication processing filters(For example, AuthenticationProcessingFilter) kicks in. Otherwise i18n won’t work.

In summary, you need to set the Locale to LocaleContextHolder, for apps not using DispatcherServlet, for Acegi’s localization to work. Let me know, if there are any workarounds or I am missing something here.

GWT4NB:Introduction to the Google Web Toolkit Framework

GWT4NB is the GWT plugin for Netbeans. Check the article here to get started.It looks neat and really help full for the starters. If you are Eclipse user, like me, you can check, Cypal Studio and GWT Designer. GWT Designer is commercial and it got graphical editor, have features like previews, drag and drop (Both features are very useful for prototyping, can not rely on it for production) . Try the trail version before going to buy the license. For me, Cypal Studio, is more than enough to get the job done.