Java Code Generators – A short rant

Java is known to be a verbose language and the situation worsens when you step into bloated enterprise java world. You need to write tons of code and configure a lot of JXXX to make your simple webapp work. Though the situation is improving in the recent years with the introduction of convention over configuration approach and also of annotation based configurations but still, if you compare the amount of code required for a functional webapp in Java then it would be at least 2X to 4X more than that of similar webapps written in other frameworks like Django or RoR.

A lot of java frameworks and IDEs tries to hide this complexity by generating code – from simple getters and setters to entire DAO layers and what not – that might give small productivity gains in the beginning but eventually every additional line of code in your project, either hand-written or generated by a state of art code generator, someone will need to maintain and evolve it, which according to some is almost 90% of the total software costs.

Thats why framework like Play feels like fresh air and it seems to be making inroads in the bloated enterprise java world.

My first Scheme program

I’ve just started experimenting with Scheme, specifically to follow Structure and Interpretation of Computer Programs. Here is a Scheme procedure that returns the sum of squares of two larger numbers out of the given three.

(define (sumLargeSquare a b c)
  (cond ((and (< a b) (< a c)) (+ (* b b) (* c c)))
        ((and (< b a) (< b c)) (+ (* a a) (* c c)))
        (else (+ (* a a) (* b b))))

'protected’ implementation in Actionscript 3 is BROKEN

Consider following code:

Theoretically, the call at line no. 13 should not cause any error as count is defined for testObj as a field in SubClass, but it gives a Cannot create property count on SubClass. Interestingly if we move modifyProperty method to SubClass or make count public, it works without any error.

So it can be said that we cannot access protected fields dynamically in a subclass.

How Facebook (probably) implements SMS threads

A lot of web applications provides you the ability to receive or post updates using SMS (text messages). Twitter is the posterboy of such applications.

But the feature that distinguishes Facebook SMS updates from any other service is that you can actually reply to individual messages and your message then becomes a part of the aforementioned thread. You don’t have to specify any special command to do so. Just hit the reply  button and you are done.

How do they know which message belongs to which thread? Its simple, each message comes from a different number. For example, say A writes something on your wall, then you will receive a message from +91922305501. Also B sends a message to you, a message comes from +91922305502 and when C comments on your note, you get a message from +91922305503. How many numbers do facebook own? Probably 100 in India, from +91922305500 to +9192230599.  A per user mapping of the number from which the message is sent and the object/thread id for that message is kept on the backend. So when the user replies to a particular number, say +91922305502 , it knows that is was a reply to the message sent by B and hence treats as a reply to that message. A rotation policy is used to decide from which number the message is to be sent. So after recieving a message from +9192230599, the next message will be from +91922305500  and now you cannot reply to the thread represented by the earlier message from the same number.

Discalimer: This is just a guess of how this functionality might have been implemented (or at least how I would implement it) and based strictly on SMSes received in India. Actual implementation might (probaly is) be entierly different from what is explained here.

Bing/Google toggle bookmarklet

I’ve been trying out Bing as my default search engine for last few months and the results are definitely at par with that of Google for most of the consumer categories. But sometimes you just need a quick toggle to see what Google would return for your query. So here is a small bookmarklet that I wrote that toggles from Bing to Google and vice versa.

Drag this to you favourites bar: Bing/Google Toogle

Note: Its tested on IE8 only. A few tweaks might be required for Firefox.

Minifying Javascript/css without changing file references in your source

Rule 10 of Steve Souders High Performance Web Sites: Minify Javascript

The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the easier ways to do this is to make it part of the deployment process.

Here are the relevent steps involved.

I’m using YUI Compressor.

#Execute this script after checking out the latest source from repository.
#Minify all javascript files
cd /path/to/javascript
for x in `ls *.js`
        java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o ${x%%.*}-min.js --preserve-semi  $x
#Minfiy all css files
cd /path/to/css
for x in `ls *.css`
        java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o ${x%%.*}-min.css  $x

Now you don’t want to replace all references to x.css or x.js in your development code with references to x-min.css and x-min.js respectively. So what you can do is rewrite all those filenames at the web server level.

For apache the following rewrite rules work fine:

#enable rewriting
RewriteEngine on
RewriteRule /(.*)\.js /$1-min.js
RewriteRule /(.*)\.css /$1-min.css

Caution: Remember to delete existing minified css/js file before running the minifying script or you will end up with file names like x-min-min-min.js and so on. One way to do this is to clear the js/css folder before checking out files from your source repository.

Humanizing the time difference ( in django )

django.contrib.humanize is a set of Django template filters that adds human touch to data. It provides naturalday filter that formats date to ‘yesterday’, ‘today’ or ‘tomorrow’ when applicable.

A similar requirement which the humanize pacakge does not address is to display time difference with this human touch. so here is a snippet that does so.

Running Glassfish as a service on CentOS

Here is how you run glassfish as a service on CentOS:

  1. Create a user glassfish (you can call it anything you want) under which Glassfish will run.
    #useradd glassfish
  2. Install glassfish in /home/glassfish.
  3. Create the startup script /etc/init.d/glassfish for glassfish.
  4. Install the service
    #chmod +x /etc/init.d/glassfish
    #chkconfig --add glassfish
    #chkconfig --level 3 glassfish on
  5. Start glassfish.
    #/etc/init.d/glassfish start

Embedding flash object in Facebook Apps (FBML)

Yesterday I was trying to include a flash object in a facebook app using FBML fb:swf tag. The flash object needed to change the url of the page it was running on on a particular event. But since facebook prevents direct script access from flash, this could not be done.

Here is a simple workaround:

1. Create a simple html page containing only the flash object that you want to include.

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   	<object type="application/x-shockwave-flash" data="foo.swf" width="300" height="300">
   		<param name="movie" value="foo.swf">
         	<param name="quality" value="high">
         	<param name="scale" value="noscale">
         	<param name="salign" value="LT">
         	<param name="menu" value="false">

2. Embed this html on the facebook page using the fb:iframe tag.

Now you can make all the Actionscript calls you want to. Only caveat to this approach is that you will no longer be able to make Facebook API calls directly from flash.

Web 2.0: Tactical Questions

Tactical questions to ask about your Web 2.0 Strategy:

  • Are your web site, storefront, web presence, development ecosystem, and user experience aligned with and open to collective user value best practices?
  • Are you (or is your management) comfortable with letting users have their own independent voices on your site?
  • Do you allow users to participate on your site? Can they share their own questions and ideas there?
  • How do you attract users to participate on your site? What brings them there initially, and what encourages them to come back?
  • What features of your site help users make connections with each other? Can users form groups with other users?
  • How sense-rich is the participation you support? Can users present sound, video, or even just formatted text?
  • Do you provide mechanisms (tagging, for example) to help users create their own navigation through your site?
  • Do you support user annotation of your site?
  • How could your site learn from user behavior and adapt itself to users? Which categories of user behavior have the most potential in your situation?
  • How do you encourage users to bring other people to your site?
  • How do you encourage users to share information in public, where it can generate positive network effects? In other words, is your site “public by default”?
  • Can you create—or tighten—feedback loops between user requests and your company’s ability to fulfill them?
  • Can users keep up with your site through syndication—RSS or similar approaches?
  • Do you provide programming interface (API) access to developers who want to combine, or mash up, the contents of your site with complementary materials from elsewhere?
  • What licensing do you use for your site contents? And what licensing can users specify for their own contributions? (For example, All Rights Reserved versus Creative Commons with some rights reserved.)
  • Do users feel they can control their information once they’ve entrusted your site with it? Can they extract it later?
  • How do you support your active community members?
  • Is there an opportunity to charge for the services your site provides? If so, can you structure those charges so that the people benefiting most directly from your site are the ones paying, while those contributing pay less or nothing?

Addendum: Should you implement a Social Network? Ask these questions ( again from Web 2.0 Strategy )

  • How might your users relate to each other? (Or, how do they presently relate to each other?)
  • If you have an existing place for users to communicate, would you benefit from enriching it with profiles and contacts?
  • Do you provide mechanisms for your users to communicate among themselves?
  • Do you provide tools for users to invite others to join your site?
  • How much information are your users really willing to share about themselves?
  • What balance of openness and privacy is appropriate in your business’s context? What mechanisms do you have to maintain that balance?
  • What value might user information create for you in this context? Advertising value? User happiness value? A new business of people who want to contact your users?
  • Do you know how tightly knit your current users are? Are they tightly clustered, loosely clustered, or purely individuals who haven’t yet formed connections?
  • Have you identified key individuals in your user base who have developed the trust of others and can make things happen on your site?
  • Would your site benefit from a full-blown social networking component, with contact lists, degrees of separation, and more, or might simply adding a profile page for users provide more immediate benefits?
  • How does the current nature of your user base influence your potential for expansion? Are there specific subjects you should explore or projects you should undertake to maximize network effects building on who your current users know?
  • What are the ratios of different kinds of users on your site? How many are contributors, how many are readers, and how many are active in community-building or networking? How can you monetize some groups without alienating others?