best practice for passing many arguments to method ?

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-03-12T11:51:09Z Indexed on 2010/03/12 11:57 UTC
Read the original article Hit count: 283

Occasionally , we have to write methods that receive many many arguments , for example :

public void doSomething(Object objA , Object objectB ,Date date1 ,Date date2 ,String str1 ,String str2 )
{
}

When I encounter this kind of problem , I often encapsulate arguments into a map.

Map<Object,Object> params = new HashMap<Object,Object>();
params.put("objA",ObjA) ;

......

public void doSomething(Map<Object,Object> params)
{
 // extracting params 
 Object objA = (Object)params.get("objA");
 ......
 }

This is not a good practice , encapsulate params into a map is totally a waste of efficiency. The good thing is , the clean signature , easy to add other params with fewest modification . what's the best practice for this kind of problem ?

© Stack Overflow or respective owner

Related posts about java

Related posts about efficiency