It can’t rain forever…

June 2, 2008

Software and conventional engineering

Filed under: Object Oriented — bbossola @ 11:55 pm

“…the analogy to conventional engineering fails because the construction of an oil production platform is materially different from the production of a computer program. The detailed design specification of a program is its code; the actual building of the program is done automatically. One of the goals of almost all programming languages ever developed has been that the code shall be self-documenting. When we look upon it this way, 100% of all programmers do detailed design.”

“Why Programmers Don’t Use Methods And What We Can Do About It”, a column in ObjectEXPERT January 1997 by Trygve Reenskaug

May 24, 2008

The technology paradox

Filed under: Java, Object Oriented — bbossola @ 1:52 pm

“…the same technology that simplifies life by providing more functionality … also complicates life by making it hard to learn. This paradox of technology is often used by people as an excuse for poor design…”
Donald A. Norman – The Design of Everyday Things, 1988 (pp. 30-31)

1988… sounds actual? By “technology”, are you thinking, as I’m now doing, about the latest state-of-the-art framework you’ve been using on your last software project? And by “design”, are you thinking about the actual software design of such project?

Think about it.

May 16, 2008

Don’t judge a book by its cover

Filed under: Java, Object Oriented — bbossola @ 3:52 pm

Head First Object-Oriented Analysis and Design
This is not a spot. This is a book I’ve been leaving closed because of its cover… what a pity! This is the first book I’ve ever read that actually talks and explain object oriented in an easy and pleasant way. Most experienced programmers will find it not much useful, but for a newbie or an intermediate OO programmer this is a must read.

In case of doubt, just read it :) It will worth the time you’re going to spend. Definitely. And it’s also quite amusing sometimes!

January 23, 2008

Business logic: how do you return on application errors?

Filed under: Java, Object Oriented — bbossola @ 5:46 pm

(note: post will be translated in English as soon as possible)

Oggi qui siamo alle prese con una decisione di design che non ci decidiamo a prendere :) , ho parlato un po’ con il gruppo e con alcune vecchie conoscenze ma non ho ancora trovato una soluzione pienamente soddisfacente: cosi mi rivolgo a voi, magari avete consigli “sbloccanti”.

Normalmente nelle applicazioni che disegno cerco sempre, come molti, di disaccoppiare la logica di presentazione da quella di business: la user interface si occupa di interagire con l’utente, mantenendo lo stato conversazionale, mentre nella business logic troviamo il domain model con le logiche di business che si applicano ad esso.Attenzione: non stiamo parlando di applicazioni CRUD che si limitano ad usare un interfaccia utente per scrivere su DB, ma di programmi con un domain model articolato, che eseguono elaborazioni di una certa complessità una volta che la richiesta dell’operazione è stata inserita tramite una user interface (web, swing, xul che sia, non è interessante). Dovendo quindi disaccoppiare questi due strati tendo ad esporre dal lato business un’insieme di “servizi”: non pensate alla tecnologia, è semplicemente una scelta di design, che poi potrà essere implementata con dei POJO, dei web services, degli EJB, qualunque cosa ci venga in mente poi di ficcarci in mezzo.(proprio perchè la tecnologia è un mezzo, non un fine :D ).

Avremo quindi dei servizi come questo: (scrivo in pseudo-java, ma potrei anche usare IDL)

// una delle interfacce della business logic
public interface MyService
{
   public void executeComputation(Slot slot, Computation compu)
   ;

   public Slot[] selectAvailableSlots(Range range)
  ;
}

(notate come stia esponendo una parte del domani model, ma questa è un’altra storia)

Essendo questa della business logic vera ci sono anche delle regole di business che devono essere verificate, e questo è possibile farlo solo a livello di business (certamente la presentation fa il suo lavoro per validare e scremare i dati ineriti, ma si limita a quello). Esiste quindi un’intera classe di errori che la business logic puo’ generare a causa della violazione delle business rules del momento, che non sono comunque verificabili in nessun modo a priori dalla presentazione (sia perché queste possono variare rapidamente sia perché mi piace che non ne sappia un cacchio!).

Come fare a ritornare queste condizioni di errore? Io finora ho individuato due macro-soluzioni:
a) utilizzare un’eccezione applicativa (aka BusinessRuleException) contenente l’errore
b) utilizzare un valore di ritorno contente sia il risultato sia l’eventuale errore

La soluzione (a) mi piace proprio poco. Utilizzare le eccezioni per modificare il controllo di flusso è da sempre, almeno per me, una vera schifezza e inoltre rende il codice assai poco leggibile. Le eccezioni devono secondo me rappresentare eventi di tipo appunto eccezionale (problemi sull’infrastruttura, ad esempio il DB si è disconnesso, non si riesce a contattare un servizio secondario, ecc.ecc) e come tali vanno gestite. Pertanto, pur essendo tanto attraente almeno superficialmente, la scarterei.

La soluzione (b) è secondo me la più naturale, d’altronde la violazione di una business rule è una cosa perfettamente naturale per la business logic, ma si porta dietro anch’essa un codice un po’ pasticciato. Non posso pensare di realizzare tante classi *Result differenti per gestire la varietà dei risultati forniti dalla business logic (nel caso in oggetto un metodo è void mentre l’altro ritorna un array) e pensandoci un po’ ho trovato questa soluzione tecnica, che ancora non mi convince del tutto: realizzare una classe Result con i generics e quindi modificare la firma dei metodi in modo che tornino questa. Il codice, tanto per chiarire, sarebbe questo:

// la classe che incapsula il risultato di una chiamata al business (value object)
public class Result {
   private F value;
   private Error error;

   public Result(F value) {
       this(value, Error.NONE);
   }

   public Result(F value, Error error) {
       this.value = value;
       this.error = error;
   }

   public F value() {
       return value;
   }

   public Error error() {
       return error;      }
}

L’interfaccia del servizio (di tutti i servizi) sono modificate in questo modo, ritornando un Result generico in ogni caso:

interface MyService
{
   public Result executeComputation(Slot slot, Computation compu)
   ;

   public Result<Slot[]> selectAvailableSlots(Range range)
   ;
}

Ok, fine dell’elucubrazione. Il punto è: cosa ne pensate? Io sto per muovermi su quest’ultima soluzione.

July 3, 2007

A usecase is an object.

Filed under: Java, Object Oriented — bbossola @ 1:38 pm

It seems naive, doesn’t it? It seems to be that sort of thing that everyone know, right? Well, get into your code and, please, find the object representing a usecase. You didn’t find it, did you? Object Oriented is far away from being adopted, neither understood.

In these days I’m working on some code developed by one my fellow colleague, who’s absolutely a master in technology: he knows the very everything about each framework, tool and library… such a cool boy to work with! But it lacks OO, he always thinks in term of “how” and never of “what”, and its code is always “code”.

In my opinion, each usecase should be present in form of an object into your code, every system interaction should be present in form of a method exposed by the usecase object, the state of your system is the state of the usecase object. And, yes, this is not enogh, more to come. But start thinking about this, please, before switching to the next framework or tecnique. Please do business logic.

Blog at WordPress.com.