Cliché all the way or "Year in review vs the Future"

Inspired by Martijn Verburg article on Java/JVM year in review I decided to write my own version of some public and some more personal events that happened during the past year. My idea is also to add some cliché section in the style of "New Year Resolutions".

Looking back at 2013

This has been quite an eventful year for me.

JavaDay 2013

In the middle of March we had JavaDay, a JugMK organized event, where I had two talks. One was called QueryDSL - Clean way to have SQL in your java applications and the other with a more unconventional name Premature optimization, is it normal where the various pros and cons of selected optimization were overviewed.

The book

html5 data book I had my first book, which I co-authored with Gorgi Kosev called "HTML5 data and services cookbook", published. The book took a lot of my personal time which basically became non-existent. In any case this was an awesome experience for me and I liked the outcome of the book. This a not going to be some classics programming book like Pragmatic programmers, Clean code or even like Head First Design patterns, it will not be something of value 10 years from now, but I did not expect it to be when I got involved .

On the other hand, I will not say "Do Not Buy This Book", like Jeff Atwood did for his The ASP.Net 2.0 Anthology: 101 Essential Tips, Tricks & Hacks, but then it is quite possible that I'm not all that self-critical. Our goal was to create a book that would be of a great use to a programmer who has not really seen javascript as a real language and has only changed stuff here and there. To some extend we have achieved that. On the other hand, as my friend Andon said "you got to be crazy to write a HTML5/Javascript book nowadays". I went throught the book-writing process for the first time similarly as Jeff Atwood did, and can confirm that it is far from glamorous. My personal perspective on his points and few of my own is the following:

  • Writing a book is hard work - lots of the parts surrounding the book-writing can be hard (there is a joke in here somewhere). It is time consuming and the hardest part is not having immediate results or if you are doing things the right way. I can't agree with Jeff that there is no enjoyment it the process, since if that was the case, I would have given up very soon. However, it is true that you slave away for you publisher for a long period of time, and the additional challenge is that you need to stay fresh for your real day job and girlfriend.

  • Writing a book does not pay - this was something I was familiar with from the start and it is true for most of the programming books. The book-contracting involves an advance for the authors that is intended to feed them until the book is published. The main problem here is that they are usually way too low for you to only be doing the writing. Our publisher was PacktPub and its rate is usually about 3000$ for first-time authors (1500$ if they are two). This is similar to Apress, where the initial rate is 5000$. There are various rates between publishers for the advance but what's probably more of an interest are the book rates. Packt gives 16% of the wholesale price of the book, meaning for a book that costs 100$, the wholesale price would be about 50% and the authors rate would be about 5$. There is a more detailed article by Charles Petzold that should give you a better insight. In general, power houses like O'reilly can spend good amount of money in promoting you, and with that your book, but this is not the case with other smaller publishers so you might wanna consider self-publishing. There is also a cool post by John Resig about Programming Book Profits. On the other hand, Pragmatic Publishing recently came out with interesting numbers, but then again those are most likely the one percenters for programming books.

  • Cookbooks are restrictive - my book was a "Programming Cookbook" and added various restriction of what can be written. Each of the "recipes" needs to be an independent seciton with its own subsections, a short intro where you have the freedom to add anything you want, a "Getting ready" section where you have the code setup, a step by step guide on "how to do it" (yet another joke), the explanation on "how it works" and my favorite section "There's more". I have a major dislike for the "how to do it blocks" mostily because at times they felt like large sections of code that would not be a part of an non-cookbook type of book. Additional constrains were added by our publisher since for some reason we had to use .doc/.docx file format for the writing part. We were even disqussing the creation of a tool that generates document with the publisher styles from a reasonable markup(markdown,asciido even latex), but this whole part is a longer story.

  • Anyone can write a book - I completely agree with this. There is the ego-filling moment when you show up on Amazon or when you get the print copy in you hands, or even when you get errata( Oh the errata), even if we ignore the financial reasons. I personally don't find the book as some sort of major accomplishments. As Jeff notes, people are impressed by authors, which also to me is kind of crazy. Basically anyone can write a book, I wrote a book surely you can as well. The only thing you need is a desire to write it and it will get published. Even if it is not accepted by a classic publisher, there is Amazon direct publishing, Lean Publishing as well as many other self-publishing options, and those books are not any better or worse than the rest.

  • Most books will not succeed - I previously added that anyone can be an author but not many have successful books. As Jeff states, the only real measurments are sales. One can argue that "50 shades of Gray" was not artistically as valuable as this year's Pulitzer Prize winner and that may or may not be true, but it sure outsold that one. The same goes for programming books, I'm not the biggest fan of Head First Design Patterns if I compare it with most of the other O'reilly books, but that one outsold almost all of programming books, so they must be doing something right.

I won't suggest you not to write a book like Jeff did, I sure did not listen to him so why should you?! Before you start, make sure that such undertaking is what you really are passionate about and consider all the other options. I would now personally prefer blog posts over writing a full book just because you get the immediate feedback. If you are still considering it, read Seth Godin's Advice for authors.

Java2Days

As I noted in a previous blog post this is an event that happens 5 years in a row and it is the nearest real conference to JugMK's location. These type of events are great for connecting the various smaller communities near Macedonia and Bulgaria as well as creating new opportunities for all attendants. This one had the importance for me since it was a first public talk in front of international audience. I have done several lectures in the past while I was working as SUN/Oracle trainer as well as few internal company events but none of these were really in front of a public audience.

Java Day#0x02

This is the event that I'm also very excited about that happened just a week ago. This was probably the best organized event by JugMK so far, mostly due to the involvement of our member Pance in the event preparations. The event went very smoothly and had great selection of talks. The talks are being added to Pareleys but for better or worse most of them are on Macedonian. This was the 4-th JavaDay in the past 4 years and the second this year which brought great new crowd. My talk was titled "State of the lambda" and was greatly inspired by the finial document for the Java 8 Lambdas. In a sense this is the year where the rebirth of Java is getting started, it is something that was mentioned by Stephan Janssen on the opening of Devoxx and it is very accurate. I do expect 2014 to be a great year for all JVM development. Note that I'm writing JVM and not Java since Java/JVM has become lot more than a language, but more of an ecosystem that is awesome for development.

The goals for 2014

This would be my short list of goals for next years. Hopefully if I make them public I will make the most effort in fulfilling them:

  • I will make an effort not to write a book ( how is that for a goal ?). Joke aside, I will make an effort to write 2 blog posts per week. It is not something unreasonable and that would mean at the end of 2014 there should be at least 104 posts.
  • Personal/Side projects - I pledge to finish some of my half-done personal projects that never saw the light of day due to various reasons (main one here is procrastination ).
  • The uncomfortable side - I will pick a language/technology that I feel uncomfortable with and make a whole project in it. There is some deciding to be done on this,but still I have a pretty good idea.
  • Get out more - I think this is something that was not happening for the past 2 years and it is really important in order to have a good work/life balance.
  • Contribute to open source - Try to make significant contribution an "important" project, or at least something that I find important. I have submitted few patches in the past and some bugreports here and there but nothing I'm really proud of.

All of these should be simple enough and reachable within reasonable amount of time. By announcing them publicly I am adding additional incentive to make them happen. This is in some sense a commitment device something that the authors of freakonomics would define as :

A means with which to lock yourself into a course of action that you might not otherwise choose but that produces a desired result

Time will show if this will work. I like to finish the whole story with a quote from Trainspotting

Choose life. Choose a job. Choose a career. Choose a family. Choose a fucking big television. Choose washing machines, cars, compact disc players and electrical tin openers..... Choose DIY and wondering who the fuck you are on a Sunday morning. Choose sitting on that couch watching mind numbing, spirit crushing game shows, stuffing junk food into your mouth. Choose rotting away at the end of it all, pissing your last in a miserable home, nothing more than an embarrassment to the selfish, fucked-up brats you spawned to replace yourself. Choose your future. Choose life..... But why would I want to do a thing like that?



photo credit: gnews pics via photopin cc
Published on Dec 31, 2013 by Mite Mitreski
Updated on 12/31/2013 12:16:00 AM

Functional Java Collections

This blog post crossposted and originally part of the 2012 edition Java Advent Calendar a lovely initiative from Attila-Mihaly Balazs and the Transilvania JUG 

There is a lot of functional hype these days so I would give a short overview on what is out there at least when it comes to collections in Java. Personally I like standard collections API but i some cases can be awkward and add additional verboseness. This should not be a problem in latter version of Java 8+. There we would probably worry about not creating callback hell but hey there is no silver bullet for most stuff why should there be one for programming?

The Guava Way

Guava project is one of Google's core libraries where there are plenty of different core language aspects and problems covered. There are utilities and extensions for everyday usage like : collections, primitives, caching, common annotations, string processing, I/O, Math, Reflections and many others. We will only take a look at the Collections goodies so lets see some of them :

    // list
    ImmutableList<String> of =
        ImmutableList.of("a", "b", "c", "d");
    // Same one for map
    ImmutableMap<String, String> map =
         ImmutableMap.of("key1", "value1", "key2", "value2");
    //list of ints
    List<Integer> theList = Ints.asList(1, 2, 3, 4, 522, 5, 6);

The Guava Collections are compatible with the JDK collections since they mostly extend or implement the standard classes. There are several cool utilities that are part of the API and have similar names with the ones from java.util.Collections. Basically any programmer who knows the JDK collections should be able to transfer to Guava easily. The ones for List is called Lists, one for Set is Sets, for Map is Maps and so on for the rest. For example:
//create new List
List<someLongName> list = Lists.newArrayList();
//create new  LinkedHashMap
Map<someKeyType, SomeValueType> map = Maps.newLinkedHashMap();

//initalize Array List on the spot
List<String> someList = Lists.newArrayList("one", "two", "three");

//set inital size for readability as well as performance
List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);


Methods corresponding to a particular interface are grouped in a very intuitive manner. There are also some extremely good ways of building cache with various of features :

 Cache<Integer, Customer> cache = CacheBuilder.newBuilder()
        .weakKeys()
        .maximumSize(10000)
        .expireAfterWrite(10, TimeUnit.MINUTES)
        .build(new CacheLoader<Integer, Customer>() {

          @Override
          public Customer load(Integer key) throws Exception {

            return retreveCustomerForKey(key);
          }
        });

Since Guava is available in most of the maven repositories its very easy to add it to your build

lambdaj

The idea behind the project is to manipulate collections in a functional and statically typed way. This is achieved in a way the avoids repetitions of simple tasks we usually do with collections. Repetition makes programmers to go with copy/pasting and creates makes them create bugs by doing so. Accessing collections without explicit looping provides way of filtering, sorting, extraction, grouping, transforming, invoking a method on each item or sum up the elements or fields of those element in a collections. Additionally to all these features lambdaj is also a DSL in a way since it adds very cool "sugar" features to the syntax making it more readable in pseudo-english. This is done with static methods so in order to use them we include them directly:

import static ch.lambdaj.Lambda.*;
As it comes to checking and matching lambdaj relies deeply on Hamcrestmatchers. So for example to create a check for an odd integers and then filter a list with that check:
Matcher<Integer> odd = new Predicate<Integer>() {
        public boolean apply(Integer item) {
                return item % 2 == 1;
        }
};
List<Integer> oddNumbers = filter(odd, asList(1, 2, 3, 4, 5));
      
and as expected the list will return the list [1,3,5]. Lambdaj take a step further with it's DSL, for example :
List<Beneficiary> beneficiaries = with(transactions)
    .retain(having(on(Transaction.class)
              .getQunatity(), lessThan(100)))
    .extract(on(Transaction.class).getBeneficiary())
    .sort(on(Beneficiary.class).getName());
      


Performance costs

Although the best way to make your application fast is to have the cleanest code as possible there comes a time when you must optimize.In order to do that there is some info provided by the creators on the memory usage and time. Lambdaj has a performance wiki page with code examples. There are also some test done by other programmers where they compare lambdaj with JDK8 for example. There are also some measurements on memory usage of Guava. As for performance of Guava most of it's functionality is standard JDK classes builders and utilities so the overhead is minimal. At the end of the day it's up to you to decide how much effect each of these libraries will have on your project and if that is positive. I'm for the idea that almost every project must have Guava on it's classpath.
Related links summary
Published on Dec 7, 2013 by Mite Mitreski
Updated on 12/07/2013 08:35:00 PM

Java2Days 2013 : Modern workflows for JavaScript integration

For the past 4 years we (JugMK) have visited Java2days but this year I decided to have talk of my own. Java2days is somewhat local conference in a sense that the attendees are mosty from Bulgaria and some from Macedonia and Seriba, but it is definitely the best Java conference in the area.

How did I end up with the topic?

My last few projects required good integration of JavaScript with Java EE, so I decided to share my previous experience in a talk. The presentation included what's out there and how a good use of Yeoman  integrates nicely into Java EE project.



As for the demo, I went through the basic files of the Angular generator for Yeoman without getting into the specific of the scaffoldings of directives and other Angular specific features.


Img by @hsilomedus


Links
Published on Dec 3, 2013 by Mite Mitreski
Updated on 12/03/2013 04:37:00 PM