Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Sun, 21 Oct 2007 03:13:46 -0400
Roedy Green wrote:
On Fri, 19 Oct 2007 16:03:53 -0400, Lew <lew@xxxxxxxxxxxxx> wrote,
quoted or indirectly quoted someone who said :
It is a best practice to prefer the most general type possible for the compile-time type of a variable. This provides the most bug-free and maintainable code.
how does that help reduce bugs? I would expect the opposite, to a
mild degree since it is less clear what classes you are actually
using.
You could for example use a LinkedList inadvertently and have it not
as obvious.
Actually, for reasons that you stated,
The main reason is to avoid the temptation to use methods of HashMap
that are not part of Map. That way you can change to some other sort
of Map very easily. If you allowed yourself to use methods peculiar
to HashMap, you would have to rewrite the code to avoid them when you
wanted to change the Map implementation.
and others that Zig stated.
Joshua Bloch covers it quite well in /Effective Java/.
--
Lew
.
- References:
- why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- From: www
- Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- From: Mark Space
- Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- From: Lew
- Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- From: Roedy Green
- why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- Prev by Date: Re: base64binary
- Next by Date: IllegalBlockSizeException when .jar compiled into .NET dll
- Previous by thread: Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- Next by thread: Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
- Index(es):