mercoledì 31 ottobre 2007

Leopard: things I like and things I dislike, a developer perspective

It's more than a full day working on Leopard right now. So, here are my first impressions on what I like and what I don't like from a developer perspective.What I like:
  1. Time Machine. Easy, almost everyone paid lip service to TM. But it's still impressive when you look at it
  2. Dtrace and Instruments. This is a joy for developers. More on this in next posts, in the meanwhile have a look at Bryan Cantrill blog,  dtrace creator 
  3. Spaces. Another easy one. Bye bye buggy Virtue Desktop, welcome pre-organized spaces (yes, you can assign applications to predefined spaces)
  4. Terminal. You won't need anymore iTerm to tail logs in tabbed windows.
  5. Calendar icon now shows the real date and not 17 of July!
  6. Ruby/Rails out of the box. Very nice to have ruby/rails integrated. Well, I would have preferred a java 6 virtual machine, but it's nonetheless a nice feature. Not as much as the Calendar icon, but a good one :). Jokes aside, is nice that ruby apps on apple are also Dtraceable
What I don't like:
  1. No Java 6. No workarounds: but java 5 looks very fast, and rumors are that we will see java 6 very soon. (hopefully with fast opengl rendered Swing, working Java Sound and a lot of dtrace probes)
  2. PostgreSql from MacPorts failed to compile, and I still hadn't had time to see why
  3. The 3D Dock looks ugly. Easy workaround:  simply type "defaults write com.apple.dock no-glass -boolean YES" and then a "killall Dock" in the command line
  4. The tranlucent menu looks ugly too. This isn't easily and completely solved right now
  5. Skype works only the first time! This happens because Skype self-modifies itself after the first launch, and the app signing mechanism break. Either wait for Skype folks to solve it or reinstall with the firewall disabled. More info here
  6. Intellij Idea 7.0.1 can't be assigned to a space :(

mercoledì 17 ottobre 2007

Migration Assistant, thank you very much!

Yesterday I received a shiny new MBP. Being my 5th Mac with OSX, I thought I almost knew all of the basics. Well, I was wrong.

I was almost ready with my rsync scripts and a great dose of patience to migrate all my stuff to the new beast when one of my colleagues pointed me to Migration Assistant. For me, that was one of those misterious icons you never use, probably being there for obscure reasons I certainly don't need to cope with, like Grapher (/Application/Utilities/Grapher.app), or ODBC Administrator (/Application/Utilities/ODBC Administrator.app), or [Put your favourite example here]. You know, that kind of stuff even Steve Jobs doesn't know exactly why is there or what is for.

Well, it turns out that Migration Assistant is really really useful and it does really really work. All you need is a firewire cable, then:


  • you connect the old mac with the new one

  • you start the Migration Assistant (/Application/Utilities/Migration Assistant.app), or, even better, you choose to "migrate data from another mac" during the setup procedure wizard

  • you (re)start the old mac keeping the T button pressed: that makes your mac go in "firewire disk" mode. You will see a fancy firewire logo moving on your screen when it's ready

  • you choose from a wizard what you want to migrate (Users, Applications, and so on). I selected all the checkboxes

  • you wait :)




And then you will have a new mac, virtually identical to the old one. Same desktop, same apps, same stuff in the trash bin, same history in the browser, same network options, same items at startup, etc. I had only 3 minor issues, and here are the workarounds:



  • Some icons on the Dock was broken, showing a question mark. I just clicked on them to solve this :)


  • Alfresco didn't work anymore. Looking at the logs, it was really a postgres problem: the postgres user didn't migrate (in fact, it wasn't even selectable in the Migration Assistant wizard, probably because the UID was to low or too high to be in the normal user range, who knows?). This is really a piece of cake, just recreate the postgres user and group with the same UID/GID with Netinfo Manager (/Applications/Utilities/NetInfo Manager.app) or using the shell.


  • The admin user wasn't anymore an admin! The user data correctly migrated, but OSX was confused and, although it had the Admin checkbox correctly set, couldn't act as an administrator (sudo for example didn't work). Being the only Admin user, OSX don't let you unset/reset the checkbox. The workaround here is simply to create another Foo admin user, then unset and reset your real Admin user Administration checkbox, and then delete the Foo admin user.





This time, the god of system administrators smiled at me and I had an up and running laptop in a few hours (just about 2 hours for 50Gb on the 400Mb firewire) and a few minutes to fix minor issues. Well done!

domenica 14 ottobre 2007

Ok, you say you believe in god…

A lot of people say they are religious, or that they believe in god. But what do they exactly believe? There are different "levels", and terminology is ALWAYS important. Simplifying a bit - philosophers will forgive me - you can be


  • Theist - A theist believes in a supernatural intelligence who created the universe and influences the fate of the creation, especially the human kind (answers prayers, does miracles, punishes sins, etc.). This is the "classic" god of Judaism, Christianity and Islam

  • Deist - A deist believes in a supernatural intelligence who created the universe, setting up the initial laws, but is not involved in human affairs. This is the Locke, Voltaire, Diderot position.

  • Pantheist - A pantheist doesn't believe in a supernatural god, but uses god as a synonim of Nature, Cosmic intelligence, and so on. This is the Spinoza's god

  • Agnostic - The agnostic thinks that he can't possibly know, and so is skeptic. He doesn't take a clear position, nor he wants to

  • Atheist - The Atheist believe there is no supernatural god. He doesn't call nature god and usually thinks that theists are the root of all evil in the world



Richard Dawkins says pantheism is "sexed up" atheism, and deism is "watered down" theism. The difference between Pantheism, Agnosticism and Atheism is quite subtle if you think about it, and usually unreligious people can't say exactly what term best describes their beliefs.

It is not so common that Scientists or Engineers are Theists/Deists. This obviously doesn't prove anything in a sense or the other, because scientists are by no means any better than other people (I don't agree with Dawkins on this point), but it's interesting. My (small) numbers - I led different software teams - are that in Software engineering at least 80% of people err between pantheism and atheism. What are yours?

mercoledì 10 ottobre 2007

Odio più i “piuttosto che” o i “quant’altro”?

Beh, prima o poi dovevo cominciare a scrivere delle cose che odio, visto che il blog si chiama "Legendary Diplomacy" :)

Non so se ci avete fatto caso, ma l'informatico in prevendita, o più in generale nelle riunioni (più spesso in quelle in giacca e cravatta), adotta un linguaggio un po' diverso dal suo solito, un linguaggio con cui cerca di darsi un tono. Non solo l'informatico, certamente: ma purtroppo frequento quasi esclusivamente questa bistrattata categoria...

Tipicamente nei discorsi del prevenditore in azione troverete avverbi inutili, oscure espressioni pseudo-anglosassoneggianti che pretende tutti capiscano, snobismi vari e più in generale parole artificialmente forbite che hanno sinonimi molto più semplici ed immediati. Penso la cosa sia in fondo piuttosto normale, poichè nelle riunioni spesso si cerca di apparire più fighi ed intelligenti di quello che si è veramente. E nelle riunioni in cui si cerca di vendere qualcosa ovviamente molto di più.

Ma in tutto questo [normale] fastidio ci sono però alcune espressioni veramente antipatiche, parole che mi danno letteralmente il voltastomaco e che passano il confine del semplice irritamento cerebrale:


  • Piuttosto che

  • Quant'altro



Frase tipica: "Beh, qui il prodotto usa hibernate, e come ben sapete con hibernate potete usare MySql [pausa artificiale] piuttosto che Postgres [pausa artificiale un po più lunga] piuttosto che Oracle, [qui fa la faccia navigata, del tipo che ha la tecnologia in mano, li sa tutti ma non serve dirli] o quant'altro"

A questo punto di solito mi voglio alzare ed urlare, ed ultimamente mi trattengo sempre più a fatica. "Piuttosto che" in italiano non ha valore disgiuntivo, non è sinonimo di "oppure". Manco per sogno. Si usa per dire "Piuttosto che comprare il tuo prodotto, mi faccio frate", ossia esprime come minimo una forte preferenza, se non addirittura una scelta già fatta od obbligata. Usarlo come disgiuntivo è un vezzo snob, ed è pure sbajato. Secondo Beppe Severgnini il mostriciattolo "piuttosto che" è di provenienza "agiato settentrionale", ma credo che ormai abbia invaso tutta l'Italia.

"Quant'altro" invece non so proprio da dove venga e perchè. Forse il neosnob, memore dell'avvertimento della maestra delle elementari di non concludere mai le frasi con eccetera, pensa di trovare salvezza in questa schifezza? Suvvia, diciamo eccetera, o meglio ancora non diciamo niente. Oppure concludiamo con una bella ricorsione infinita così il cervello va in tilt e il problema è risolto alla radice.

Ora mi sento molto meglio: ho messo giù i miei pensieri sconnessi e mi rendo conto chiaramente che odio molto di più i "piuttosto che" che i "quant'altro". E' già un bel risultato, no?

martedì 9 ottobre 2007

Idiomatica #1: il polimorfismo dei poveri

Comincerò a postare regolarmente il materiale pubblicato sulla versione cartacea di JavaJournal . Questo è il primo numero della rubrica.

Voglio cominciare questa rubrica con una “worst practice” fra le più orribili, il polimorfismo dei poveri. Non che non abbia materiale a disposizione per decine e decine di rubriche, e centinaia di esempi di come non si dovrebbe scrivere del codice in Java... ma questa particolare pratica, oltre a essere veramente brutta, ha delle caratteristiche che secondo me la rendono unica. E per questo deve avere l'onore del primo numero di questa rubrica dedicata alle brutture nel design.



  • E' assolutamente non necessaria, e l'alternativa è nell'abc della programmazione ad oggetti

  • Non offre nessun vantaggio particolare rispetto all'alternativa corretta, ma solo svantaggi. Altre brutture perlomeno hanno una qualche giustificazione più o meno fondata, questa no.

  • E' incredibilmente diffusa





Il polimorfismo dei poveri è caratterizzato da codice simile a questo:



if (a instanceof b) {

// fai qualcosa

} else if (a instanceof c) {

// faiqualche altra cosa

}



Ora, gia' l'uso di instanceof di per se' è criticabile: instanceof in un linguaggio ad oggetti non dovrebbe mai essere usato, se non in un caso particolare che lascio come simpatico esercizio agli amici lettori.

Ma usare instanceof per fare eseguire logiche diverse ad un oggetto a seconda del suo tipo, è veramente abominevole: a cosa serve il polimorfismo – che fa esattamente la stessa cosa - se poi devo ricostruirmelo malamente da solo? Notare che il fatto che l'operazione sia eseguita sullo lo stesso oggetto su cui si è operata la condizione o su un altro è irrilevante: in quest'ultimo caso è solo segno di un cattivo assegnamento di responsabilità e della necessità di spostare l'eventuale metodo al posto giusto.


a.faiQualcosa()


E' evidente come il vero polimorfismo sia infinitamente superiore alla sua brutta copia: l'”if” viene fatto dal runtime automaticamente, e voi non dovete fare nulla di particolare se non usare degli oggetti in maniera polimorfa. Il polimorfismo dei poveri invece va invece mantenuto, ogni volta che si introducono nuovi tipi e nuove logiche: di solito fra l'altro questo tipo di strutture condizionali si trovano ripetute in diversi punti del codice, poichè è molto comune che esistano diverse logiche da eseguire, rendendo l'introduzione di bug così probabile da essere quasi scontata. Il vero polimorfismo invece vi permette semplicemente di aggiungere nuovi metodi per ogni nuova logica, concentrando così in unico punto tutto ciò che ha gli stessi motivi per cambiare.

Eseguite per curiosità un bel


grep -R instanceof .


nella directory demo del vostro JDK, e troverete molti esempi di poliformismo dei poveri. Sembra che Swing sia un portatore (sano?) di polimorfismo dei poveri!

Permettetemi di riformulare il concetto in maniera più immediata: se trovate del codice che usa “il polimorfismo dei poveri”, è codice strutturato e non codice ad oggetti. Che sia nelle librerie di java, che l'autore sia considerato un guru, che sia uno dei software open più blasonati, che l'abbiate trovato su un libro (di solito di API, non certo di design), o che l'abbiate scritto voi stessi in quello che considerate il vostro capolavoro di programmazione ad oggetti, non cambia la sostanza: non sarà che in 20 anni questa benedetta programmazione ad oggetti a molti non è ancora entrata in testa?

Esercizi:
Eliminare il polimorfismo dei poveri da uno qualsiasi degli esempi Swing del jdk. Come dite? Eh no, così bisogna rivedere le gerarchie... e poi inserire nuove interfacce. E poi bisogna eliminare quelle istanziazioni di oggetti concreti... bene, quella si chiama programmazione ad oggetti.
Ora salutate con la manina quella brutta e vecchia programmazione strutturata, e cercate di lasciarvela per sempre alle spalle!

Conclusioni:
Il polimorfismo dei poveri è una delle peggiori pratiche che si possa adottare per avere codice difficilmente manutenibile, inutilmente complicato e soggetto a bug. Il polimorfismo dei poveri indica inoltre che non state sfruttando la potenza degli oggetti. Se siete pagati per linee di codice, o per bug risolti, usatelo pure liberamente! Ma se poi vi trovate nei guai, non dite che nessuno vi aveva avvertito...