Le blog à Fred

Aller au contenu | Aller au menu | Aller à la recherche

mercredi 10 février 2016

Why you should define your own types

Most of the people coming here know that I'm an Ada enthusiast.
In my job, I'm mostly developing in Java and one of the remarks I got in the past about Ada type system is that it's useless.
A lot of developers are arguing that they never have bugs due to a type error. So, the use of range constraints and so on in Ada are almost seen as useless verbosity.
For once, I've another point of view about this feature and it's not related to bugs but to maintainability.

The reason

It's not really a secret that I'm working on cartographic products. Our product is written in Java and we use OpenMap for displaying some kind of maps.
Few weeks ago, we found that the old version of OpenMap we were using contained a bug about a specific raster format. After looking into the new source code[1], I found that the bug was still there.
One of my colleague fixed it and provided a patch to the project. Then I decided to test the patch on the brand new version of OpenMap by migrating to the last version.

The problem

Migrating is not always as simple as it might seem.
Between the two versions, 4.6.5 and 5.1.13, there has been a lot of changes that were easily detected by the compiler but the main one only seemed to be a package name change.
The LatLonPoint class was in the past inside the package com.bbn.openmap and changed to com.bbn.openmap.proj.coords.
Ok, that's easy but if I show you the beginning of the code, you will see.

/*
* For version 4.6.5
*
*/
public class LatLonPoint implements Cloneable, Serializable {
    // SOUTH_POLE <= phi <= NORTH_POLE
    // -DATELINE <= lambda <= DATELINE
    public final static float NORTH_POLE = 90.0f;
    public final static float SOUTH_POLE = -NORTH_POLE;
    public final static float DATELINE = 180.0f;
    public final static float LON_RANGE = 360.0f;
    // initialize to something sane
    protected float lat_ = 0.0f;
    protected float lon_ = 0.0f;
 
    public final static float EQUIVALENT_TOLERANCE = 0.00001f;

And the newest one

/*
* For version 5.1.13
*
*/
public abstract class LatLonPoint extends Point2D implements Cloneable, Serializable {
 
    /**
     * 
     */
    private static final long serialVersionUID = 4416029542303298672L;
    public final static double NORTH_POLE = 90.0;
    public final static double SOUTH_POLE = -NORTH_POLE;
    public final static double DATELINE = 180.0;
    public final static double LON_RANGE = 360.0;


Do you see ?
First of all, the class is now abstract... You can imagine what happened in my code as this class is really centric :)
So how do you create LatLonPoint ?
Inside the LatLonPoint, you have two inner classes defined as follows

public static class Float extends LatLonPoint {
 
        /**
         * 
         */
        private static final long serialVersionUID = -2447464428275551182L;
        protected float lat;
        protected float lon;
        protected transient float radLat;
        protected transient float radLon;
 
[...]
 
/**
     * Double precision version of LatLonPoint.
     * 
     * @author dietrick
     */
    public static class Double extends LatLonPoint {
        /**
         * 
         */
        private static final long serialVersionUID = -7463055211717523471L;
 
        protected double lat;
        protected double lon;
        protected transient double radLat;
        protected transient double radLon;

This was done to mimic the Point2D class but everywhere I instanciated a LatLonPoint, I now need to choose between Float or Double version.
Ok but what happens if I prefer to keep the higher abstract view ?
Well, for getting latitude in degrees the type returned is float whereas for radians, it's double.
Mixing such kind of types creates a mess inside our application because everything was float in the past.

The Ada solution

First try

A first solution might be to declare types for latitude, longitude and radians.

type Latitude is digits 10 range -90.0 .. 90.0;
type Longitude is digits 11 range -180.0 .. 180.0; -- for 7 digits after dot
type radian is digits 15 range -2*π .. 2*π; -- with π from Ada.numerics
 
type LatLonPoint is private; -- which should define the way we store the coordinates

The problem is that the client code don't have any way to change the precision the same way Java more or less allows it.
It's time to extend the functionalities with generics.

Second try

It would be better for the client code to define the precision.
Here is one solution... But I'm sure there's something clever than this[2]

generic 
	type real is digits <>;
package coords is
   type latitude is new real range -90.0 .. 90.0;
   type longitude is new real range -180.0 .. 180.0;
   type radian is new real range -2*π .. 2*π; -- with π from Ada.numerics
 
type LatLonPoint is private; -- Using the types defined above
end coords;


You should wonder why not using Real types directly. First, because I want the compiler to check as much as possible.
Second, because I don't need the precision of a Java double.
Let me explain :
With 7 digits after the dot for a latitude in decimal degrees, two latitudes in my code will be distant at equator level from 1 centimetre.

Conclusion

By providing the precision on the client side through a generic unit, it is possible to avoid the types mixing we had with the Java version while keeping type safety.
Changing precision is a client decision and not a implementer one. This way, the compilation problem[3] can be avoided.

Notes

[1] Open source is really great for this

[2] Maybe because I wrote it after drinking wine with another Ada enthusiast :D

[3] Around 1500 compilation errors in my software

vendredi 19 avril 2013

Balancing bullshits

After spreading bullshits, our Bullshit Company has been overflown by bullshit requests.
It's time to provide a bigger architecture than the one we used. Let's balance the Bullshit Generator.

For the impatients, this post will deal with several problems that a beginner could encounter or fear : exceptions handling and sequences

Lire la suite...

dimanche 24 mars 2013

Spreading bullshits

I decided today to write a small cute tutorial to create the best of the best for scalability, for highly distributed and high reliability while being multi-platform, multi-language and potentially multi-paradigm... Well, the two first ones, ok but for the last, I don't really know and finally, I don't give a damn :D
At this point, with the number of buzzwords I used, your trollmeter should explode !! :D
But, from now, I won't tell you anything else, you'll have to read what follows !!

Lire la suite...

vendredi 22 mars 2013

Distribution de conneries

J'ai décidé aujourd'hui de faire un petit tuto sympa pour réaliser le top du top en matière de scalabilité[1], de haute distribution ainsi que haute reliabilité[2] tout en restant multi-plateforme, multi-langage et potentiellement multi-paradigme... Enfin, les deux premiers, c'est sûr, le troisième, j'en sais rien et je m'en tape :D
Normalement, là, avec le nombre de buzzwords utilisés, vous devriez déjà avoir le trollomètre au taquet !! :D
Mais je ne vous en dirai pas plus, il faut aller voir, LA SUITE !!!

Notes

[1] J'adore cet anglicisme tellement il est foireux

[2] Bien aussi, celui-là

Lire la suite...

mercredi 21 novembre 2012

Release often... Enfin, presque

Bon, je sais plus trop si je vous ai déjà filé une version de mon excellent petit soft de gammes et accords que j'ai décidé de nommer Scalada.
Comme mon pote Albert[1] me guette, voici donc cette masterpiece of code :D
C'est sous licence BSD modifiée, histoire d'être compatible GPL et c'est fourni en l'état.
Mais comme je suis gentil, vous trouverez à la fois l'archive proprement dite ainsi que le référentiel Monotone[2] pour naviguer dans l'historique et voir ainsi mes errements.
Vous pourrez même contribuer activement... Si je veux bien accepter vos pull requests :)

Notes

[1] Zheimer dit Al Zheimer

[2] En version 1.0 me semble-t-il

vendredi 26 octobre 2012

Code, gloire et beauté, Switzerland

Alors que j'avais dénoncé, dans ce ticket et ses prédécesseurs, les dérives du fork à tout-va subies par Natacha, une nouvelle histoire, très jolie celle-ci, vient d'arriver en provenance de Suisse.
Aux détours d'une conversation sur un bout de code, Gautier De M.[1] m'a expliqué que plusieurs sociétés ont contribué à Zip-Ada[2].
Bon, ça n'a l'air de rien comme ça mais quand on sait que le logiciel sus-nommé est sous licence MIT, ça devient plus sympathique.
En effet, jusqu'à maintenant, les détracteurs des licences type BSD, MIT et Apache prenaient comme arguments comme ces licences permettaient à une entreprise de prendre le code et de ne jamais reverser les modifications. C'est vrai... Mais, il arrive que certaines le fassent quand même !!
Analyse du principal intéressé sur la non-obligation de contribuer :

Juste, mais l'avantage de contribuer est que je fais la maintenance :-)


Allez, parce que je sais que vous n'en croyez pas vos yeux, le code contribué est ici, et encore ici.

Au passage, félicitations à Gautier qui a finalement pris le temps et réussi à virer tous les pointeurs pour en faire un truc très propre :)

Notes

[1] Le nom a été tronqué pour ne pas donner trop d'informations :D

[2] Ok, ça date pas d'hier mais bon, j'inspecte pas tout le code que produit Gautier non plus.

mercredi 28 décembre 2011

Hocus Bogus

Comme tout bon sorcier de l'informatique, c'est grâce à l'agilité de mes petits doigts que j'ai ouvert la boîte de Pandore du compilateur GCC pour en sortir un bogus.

Lire la suite...

mercredi 21 septembre 2011

Que faire ?

Alors que je me promenais sur le Web, j'ai abouti sur le site de l'excellent Pascal Obry.
Sans le connaître personnellement, ce que je regrette compte tenu des contributions Ada du personnage[1], je passe de temps à autre sur son site mais aussi et surtout sur le site de Vision2Pixels.
Qu'est-ce qui a fait que je n'ai pas regardé de plus près la rubrique Guitare de son site ?
Et bien, je ne sais pas vraiment. En tout cas, les gens qui suivent mes billets de près verront que mister Obry a écrit il y a onze ans un petit logiciel en Ada pour calculer les gammes et les accords à la guitare.
Du coup, je fais quoi, moi, avec mon Scales ?
Ben je continue mais sans copier sur mon prédécesseur, en espérant que jamais il ne regardera la qualité de mon code :D

Notes

[1] Ainsi que le très bon article sur la triade Trac/Subversion/Buildbot co-écrit avec Olivier Ramonat

mercredi 7 septembre 2011

Show must go on

Juste un tout petit billet pour fournir une nouvelle version de Scales, LE logiciel de calcul de gammes musicales en Ada !!
Bon, c'est pas encore all-guitar-chords mais ça avance :D

lundi 22 août 2011

Old experiences

This post is a translation of this post in french, after reading the comment of Charlie5.

Several years ago when I was younger, nicer with a lot more hairs, I brightly passed my last exam for beeing engineer.
13 years later, here I am trying to do the same experiences again as professor Frankenstein would do...
Well, in fact, I don't try to raise the dead or building some kind of zombie out of blood, skin and body parts.
That said, my last sentence is not so far from reality as I'm trying to build a big piece of software with many others talking to each others.
Now it's time to show !! I'm talking about CORBA and more generally about distributed computing.
The only difference with my studies is that, this time, it will be done in Ada... Obviously... And on FreeBSD... Obviously too :D

Lire la suite...

- page 1 de 2