Tuesday, March 16, 2010

A thing about C portability, or why you do not need to leave the C side when someone says they work with Java, Python or anything

I am not used to write about programming here, but this fact really got my attention nowadays at work.

I was required to pass my algorithm to Java and a bit reluctant about the intensive workload which would result in two codes with twice the maintenance time. So I decided to challenge myself again and try a different solution, that is, why not interface the C code to Java?

Well I have nothing against programming in Java or anything. But porting code is a terrible straightforward thing that should never be done by humans, or at least, not by myself.

So I found a challenging, entertaining, useful and much better solution. To interface!

My task was a little difficult from only calling a library and so on, because my library must use a function defined by the user and this way Callbacks are a must. I was a bit scary at first if all this would end up nice, because JNI (Java Native Interface) did not seem to have much followers on the net and if it was CNI I would have dropped the subject, because it would be certainly lack either documentation or efficacy... but Java is another world, not better not worse, just another. And most of the things done in Java, although too complicated, they are not done by third parties.

And at the end, I just got a pretty system working well under Java and using the C++ shared library :).

Most people would probably port code and would end up with duplicate maintenance. I am really happy to have worked until now with 2 managers that really understand and accept better solutions. I know there are a bunch that only wants simple and fast solution.

All this happened once before when I worked in Nokia and they used scripts in a proprietary system, I just needed to compile a DLL from my C code and it all end up nice as well :). But I should say, that time things was much easier than this time around.