30 July 2013

My journey from Python to Java, to Scala... and back to Java

Here's an update on my work for improving the Construct library in java and make it more type safe and easy to use 
https://github.com/ZiglioNZ/construct

Previous versions would work like this.
1. define a Struct at runtime, and composite it by passing in static constructors of other Constructs:

Struct s = Struct( "struct",
     UBInt8("a"),
     UBInt16("b"),
     Struct("foo",
           UBInt8("c"),
           UBInt8("d")))

2. then we can use this struct to parse a byte array. That returns a Map type called Container:
Container c = struct.parse(ByteArray(1, 0, 2, 3, 4));

3. we can then extract the parsed value from the map/container:
assertEquals( 1,  c.get("a"));
assertEquals( 2,  c.get("b")); 
assertEquals( 3, ((Container)c.get("foo")).get("c"));
assertEquals( 4, ((Container)c.get("foo")).get("d"));

You can see there are a few problems that make this API not so nice to use: 
first, the problem with maps and key strings, one has to remember the names and refactoring for those names is painful
second, all that casting: due to java's lack of type inference, it's difficult to deal with HMaps, of the type that parsers return, therefore casting has to be added.

As a way to mitigate these problems, I've looked at Scala.
Scala offers nice things like elegant default constructor and case classes. Those two things combined could help creating a CLASS, instead of a runtime collection of fields. 
Having a class would help the IDE with code completion: no longer I would have to remember field names, they would be class fields, therefore IDE code completion and refactoring.

I've thought long and hard and tried different things, and stumbled against some limitations of scala, case classes and inheritance.
At the end I went back to using Java reflection, with the idea that later I will look into Scala macros to improve it.

How have I done it?
Well, first instead of passing objects to a Struct at runtime, I statically define a Struct with a number of fields:

    class Foo extends Struct {
      public Foo(String name ){super(name);}
      public UBInt8 c;
      public UBInt8 d;
    }

    class S extends Struct {
      public UBInt8 a;
      public UBInt16 b;
      public Foo foo;
    }

Then I've added code to the Stuct constructor that at runtime uses reflection to inspect the Struct fields and create an instance of it, by passing the name of the field, that is also inspected via reflection.

The last trick is a way for each field to hold an Object, that is the result of a call to parse(). The Struct itself updates this value for each field, after parsing.
Now I have a get() method that returns that value for each field, see:

    S s = new S();
    s.parse(ByteArray(1, 0, 2, 3, 4));

    assertEquals(1, s.a.get());
    assertEquals(2, s.b.get());
    assertEquals(3, s.foo.c.get());
    assertEquals(4, s.foo.d.get());

I think it's much nicer to use. It's not type safe yet, since I need to update all my field definition in order to return the correct type, but I'm definitely getting there.

I think this is a win for java, that is still kicking.
+Pascal Voitot Dev can definitely explain all the subtleties why scala reflection would be better than java's, and why macros would be even better. 
From the practical point of view I think this is a good compromise,


https://github.com/ZiglioNZ/construct/blob/83c01e14e30f7c04875f303cd423eecd14d97834/src/main/java/com/sirtrack/construct/Core.java

https://github.com/ZiglioNZ/construct/blob/83c01e14e30f7c04875f303cd423eecd14d97834/src/test/java/com/sirtrack/construct/ConstructTest.java

14 March 2013

Google Reader Alternatives

Google Reading is shutting down 1 July 2013. What are (cloud based) alternatives?

Promising
  1. http://www.newsblur.com/
  2. http://feedly.com/
Both products promise seamless integration with Reader, that is the ability to import all existing feeds. Both services are experiencing an obvious surge in traffic and are unable at the moment to accept new users.
Newsblur looks like it has good client support, web and mobile. Also it is an open source project.
From the FAQ thought it looks like their web service might not be able to poll regularly all the newsfeeds, but mainly the ones that people request the most.
I'd like to try it, and I'll be more than happy to pay for a good service.

Google's move also means definitive death for Listen, the podcast catcher I've been using all these years on Android.
Well, there are other solutions I know, but will they integrate with either newsblur or feedly?

[UPDATE, 2 April]
The latest Android update for gReader Pro claims "gReader will work after the closure. We are working on a solution." Fingers crossed


23 February 2013

Loving Open Source

Pretty much every component of our system is made out of open source components I can and have changed/contributed to.
Not every one of these components has a large community behind but sometimes I'm surprised by Pull Requests and Q&A on google groups or stack overflow.
I find it invaluable having direct access to people who have implemented a library, sharing ideas, asking questions about their design.
These days sometimes it feels like I'm not growing as much as I used to, I don't learn entirely new things every day anymore. But looking back, gosh! the progress over the years has been constant and the accumulate knowledge significant. More than anything: written software still appears sound. If I go back, and I look at how certain things got implemented, even smaller things, they look simple and beautiful (not everything of course).
As a proof of that, extending and providing new services doesn't take too long. 
Iterative refactoring allows me to consolidate the good things and to get rid of the bad (copy&paste) ones.

I'd like to take a chance and thank all the contributors to these projects:
Siena Project
Objectify
Construct
Open Layers
SlickGrid
KnockoutJS
Vosao CMS
Force.com Web Service Connector
All Google's Contributions and Resources!


plus several others... 
For my contributions in my free time and at work go to:

github.com/ZiglioNZ/
github.com/sirtrack



PS Recently I've been invited to take part of a project related to my latest interest: strabismus and amblyopia.
There's no public information available for this particular project yet but it will eventually become Open Source on GitHub!