Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NULL arguments a bad practice?

Tags:

Is it a bad practice to pass NULL argument to methods or in other words should we have method definitions which allow NULL argument as valid argument.

Suppose i want two method 1. to retrieve list of all of companies 2. to retrieve list of companies based upon filter.

We can either have two methods like as below

    List<Company> getAllCompaniesList();     List<Company> getCompaniesList(Company companyFilter); 

or we can have one single method

    List<Company> getCompaniesList(Company companyFilter); 

here in second case, if argument is NULL then method return list of all of companies.

Beside question of good practice practically i see one more issue with later approach which is explained below.

I am implementing Spring AOP, in which i want to have some checks on arguments like 1. Is argument NULL ? 2. is size of collection 0?

There are some scenarios where we can not have null argument at all like for method

    void addBranches(int companyId, List<Branch>); 

This check can be performed very well by using Spring AOP by defining method like following

@Before(argNames="args", value="execution(* *)") void beforeCall(JoinPoint joinPoint ,Object[] args ) {             foreach(Object obj in args)            {                  if(obj == NULL)                  {                      throw new Exception("Argument NULL");                  }             }    } 

But problem i am facing is since i have defined some of methods which should accept NULL argument for multiple functionality of one single method as mentioned above for method List getCompaniesList(Company companyFilter); So i can not apply AOP uniformly for all of methods and neither some expression for methods name match will be useful here.

Please let me know if more information is required or problem is not descriptive enough.

Thanks for reading my problem and giving thought upon it.

like image 344
Maddy.Shik Avatar asked Dec 26 '10 19:12

Maddy.Shik


People also ask

IS null bad practice?

NULL is a terrible design flaw, one that continues to cause constant, immeasurable pain. Only a few languages have managed to avoid its terror. If you do choose a language with NULL, at least possess the wisdom to avoid such awfulness in your own code and use the equivalent Maybe/Option.

Is it good to pass null?

As with most method calls, it's good practice to avoid passing a null reference, which will likely result in a NullPointerException at runtime.

Is it bad to pass null as a parameter?

Code Gardening. Null arguments are evil. Don't pass null as an explicit argument into a method. The problem with passing null as an explicit value into a method is very similar to that of passing explicit bool values into a method - the code is fundamentally unreadable.

Why returning null is bad?

Getting a null value is an ambiguous for caller, because it doesn't say whether the null is returned due to the bug or due to the fact that the order was not found in the database. Returning null is definitely not a domain-driven design approach.


2 Answers

It's fine, in cases when there are too many overloaded methods. So instead of having all combinations of parameters, you allow some of them to be null. But if you do so, document this explicitly with

@param foo foo description. Can be null 

In your case I'd have the two methods, where the first one invokes the second with a null argument. It makes the API more usable.

There is no strict line where to stop overloading and where to start relying on nullable parameters. It's a matter of preference. But note that thus your method with the most params will allow some of them to be nullable, so document this as well.


Also note that a preferred way to cope with multiple constructor parameters is via a Builder. So instead of:

public Foo(String bar, String baz, int fooo, double barr, String asd); 

where each of the arguments is optional, you can have:

Foo foo = new FooBuilder().setBar(bar).setFooo(fooo).build(); 
like image 182
Bozho Avatar answered Dec 28 '22 22:12

Bozho


I use a very simple rule:

Never allow null as an argument or return value on a public method.

I make use of Optional and Preconditions or AOP to enforce that rule. This decision already saved me tons of hours bugfixing after NPE's or strange behaviour.

like image 41
henning77 Avatar answered Dec 28 '22 23:12

henning77