REST per semplificare

Summary: cool post by Scott Raymond Update 2011/06/08: backup copy on the Internet Archive

Scott Raymond scrive di avere adottato per il suo sito lo stile REST, in cui si cerca di modellare tutto in termini delle operazioni CRUD: Create, Read, Update, Delete. Si diminuisce drasticamente il numero di azioni, si aumenta il numero di risorse su cui queste azioni standard si applicano. Il bello è che queste azioni standard si mappano bene sui metodi di HTTP: POST, GET, PUT, DELETE. Questo permette di ridurre il numero di url necessarie. GET /users/3 significa dammi i dati dell’utente con id 3, mentre DELETE /users/3 cancella l’utente numero 3, ecc.

Rails è un eccellente design che si basa su alcune idee importanti: DRY, MVC, Convention over configuration, buon gusto. La nuova buona idea che si aggiunge a queste è REST, come DHH ha spiegato nell’ultima RailsConf.

Il bello è che come conseguenza di questo ulteriore vincolo di design, le applicazioni si semplificano. Raymond scrive:

So, by adding a few controllers, I cut the total number of actions by almost twenty. That’s a pretty big deal, because actions are like moving parts in a machine—the more there are, the more can go wrong. The fact that I could cut almost twenty actions indicates that there was a lot of redundancy hiding beneath the surface. …

Cutting actions is great, but even more significant is that the remaining ones are almost completely uniform. There are seven standard Rails actions: index, new, create, show, edit, update, and destroy. Everything else—oddball actions—are usually a clue that you’re doing RPC. … The upshot is that the controllers are very uniform, which makes the entire application conceptually simpler, and thus easier to maintain, test, and extend.

Leave a Reply