Home > Mobile >  Is it okay to pass null/empty unused parameters to a method?
Is it okay to pass null/empty unused parameters to a method?

Time:01-06

I'm trying to find what is the best practice to do in a similar case, say I have a method

String getContent(String id, Boolean remove_tags) {
    …
}

This has a long implementation, and I need the functionality but in some cases I don't want to send the second parameter, is the right practice for this case to pass it as null or to create a another method with only the first parameter and make that method return this one this way:

String getContent(String id) {
     return getContent(id, null);
}

I've read a suggestion in this question [here][1] to only pass it as a null.

I might be making a big deal of nothing, but I've tried to make a couple researches about such a topic and couldn't find something useful, so I need some opinions please about this!

[1]: https://stackoverflow.com/questions/21205492/how-do-you-call-a-method-using-only-some-of-its-parameters#:~:text=You can pass,null, null, null);

CodePudding user response:

Either way is allowed. I have seen both used, even within the libraries bundled with Java.

Prefer overloading

String getContent( String id , Boolean remove_tags ) { … }
String getContent( String id ) { … }

  •  Tags:  
  • Related