Here's the scenario:
I've two different classes (say ClassA
and ClassB
) which will be calling a Static method (say meth1
) of ClassC
.
meth1
has three arguments: List<String> lstStr, Boolean bool and Map<integer,string> testMap
The only way to differentiate between ClassA
and ClassB
is that ClassB
doesn't create any Map<integer,string>
but ClassA
has.
meth1 will be executing logic only if the IF condition satisfies and following is the condition:
- If
testMap
is Empty or null then execute - when called fromClassB
- If
testMap
is not Empty and contains certain values then execute - when called fromClassA
I've done OR between the above two conditions in IF.
If I pass null from ClassB
then I would get null de-reference error (since I've !testMap.isEmpty()
and containsKey()
in IF condition bullet 2).
So how feasible is it to purposely pass an empty Map<integer,string>
from ClassB
method (without affecting resource usage since I'll be having huge data in lots of other variables).
This will make things easier since I can use both !testMap.isEmpty()
and containsKey()
for ClassA
and also check for testMap.isempty()
for classB
.
Is it even a good practice?
if(testMap != null && !testMap.isEmpty())
if myMap.contains(someKey)...
orfor (SomeType t: myMap.values()) {..
logic anyway and hence do the right thing, even if map is empty.