Web Log di Adrian Florea

"You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
posts - 440, comments - 2715, trackbacks - 3944

My Links

Archives

Post Categories

Image Galleries

.RO Blogs

.RO People

.RO Sites

Blogs

Furls

Links

vinCitori

Do return an empty array instead of a null reference

Questi giorni, oltre al progetto .NET su cui normalmente lavoro, dedico parte del tempo a preparare un corso di JSP e Servlet che terrò da settembre a dicembre. Sono ormai quasi 2 anni e mezzo da quando ho lasciato Java e sono scappato con .NET in un'inaspettata e dolcissima avventura :-) Guardo quindi Java con altri occhi, adesso che devo rivedere la materia.

Per esempio, il metodo getParameterValues dell'interfaccia javax.servlet.ServletRequest ritorna java.lang.String[] :

public java.lang.String[] getParameterValues(java.lang.String name)

"Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist."

Già il fatto di ritornare un array di stringhe, quando tutti gli altri metodi della stessa interfaccia ritornano java.util.Enumeration, mi sembra strano, ma ritornare un null al post di un array di lunghezza zero non è proprio una "best practice". Anzi, Joshua Bloch (che adesso lavora con Google), nel suo bellissimo libro "Effective Java. Programming Language Guide" dedica un intero capitolo (pp. 134-135) a questa cosa da evitare:

"It is error prone, as the programmer writing the client might forget to write the special-case code to handle a null return. Such an error may go unnoticed for years, as such methods usually return one or more objects. Less significant, but still worthy of note, returning null in place of a zero length array also complicates the array-returning method itself.

It is sometimes argued that a null return value is preferable to a zero-length array because it avoids the expense of allocating the array. This argument fails on two counts. First, it is inadvisable to worry about performance at this level unless profiling has shown that the method in question is a real contributor to performance problems. Second, it is possible to return the same zero-length array from every invocation that returns no items because zero-length arrays are immutable and immutable objects may be shared freely. [...]

In summary, there is no reason ever to return null from an array-valued method instead of returning a zero-length array."

Aggiungo che la stessa best practice vale anche per .NET. Brad Abrams, in questo post su "On Designing Good Libraries" consiglia:

"Do return an empty array instead of a null reference.
Nulls are generally unexpected to developers.
A common usage pattern for arrays:

foreach(char c in Path.InvalidPathChars)
{
}

throws an exception if InvalidPathChars returns null."

Print | posted on giovedì 22 luglio 2004 15:09 | Filed Under [ Carillon .NET Pattern Dappertutto ]

Powered by:
Powered By Subtext Powered By ASP.NET