Friday, 5 December 2014

A nerd's java practices, might not be universal though

Here's what a java nerd thinks on practices while coding. 
Purpose could be to 
* make beautiful code,
* improve perf
* improve readability etc

1. class members vs passing args between internal methods

Prefer class members or passing arguments between internal methods?

Do methods in class instances take a place in memory?

2. Temp variable in foreach iteration of a large loop

Temporary variable used for each iteration of a large loop, strings are immutable so what should I use?

3. Immutability habit (an affair with final) 
Immutability are to simplify a program, here's what the veterans have to say;
"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible." - Joshua Bloch
"Immutability Fosters Concurrent Programming." - clojure
If immutable objects are good, why do people keep creating mutable objects?

Excessive use “final” keyword in Java

Wikileaks To Leak 5000 Open Source Java Projects With All That Private/Final Bullshit Removed



Thursday, 25 September 2014

Side look on DSL


I’m a huge fan of DSL these days and try to implement wherever possible. I first heard of DSL through Hibernate query DSL, then groovy lang, Proto Buffers and so on.

Martin Fowler would define DSL as A a computer lang that's targeted to a particular kind of problem, rather than a general purpose lang that's aimed at any kind of software problem.

I found some libraries like hibernate dsl making their own embedded DSL, which we have to follow as it is. But there’s always a royal road for us to create our own DSL to solve our domain problems.
So, Internal DSLs aka embedded DSL aka Fluent Interface ( FI ) aka semantic facades are particular ways of using a host language (say Java) to give the host language the feel of a particular language.
eg. in Elasticsearch Java API,
           AggregationBuilder aggregationBuilder  =
                   (DateRangeBuilder)AggregationBuilders.dateRange("dateRangeAggs")
                                                     .field("transactionDate")
                                                     .addRange("Oct-1989", "1989-10-28", "1990-10-28")
                                                     .addRange("Nov-1990", "1990-10-28", "1991-10-28")
                                                     .subAggregation(AggregationBuilders.sum("transactionAmtSum")
                                                                                               .field("transactionAmt"))
                                                     .subAggregation(AggregationBuilders.avg("balanceAvg")
                                                                                                .field("balanceAmt"));
Internal DSLs are about readability.

Other internal DSL examples
Squill
While External DSLs have their own custom syntax and we write a full parser to process them.
eg.  1)

/**
 */
servers = {
   clusterName("gccount cluster")
   server(){
           name("Node1")
           hostname("localhost")
           port("9160")
           keyspace("gccount")
}

But we need a parser for above DSL, in groovy there’s groovy.util.BuilderSupport for creating arbitrary nested trees of objects. In Java we can write parsers as here.


References