0

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 from ClassB
  • If testMap is not Empty and contains certain values then execute - when called from ClassA

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?

4
  • try with this condition if(testMap != null && !testMap.isEmpty())
    – Sarvesh
    Sep 20, 2019 at 6:26
  • Thanks @sarvesh - But this fails since passing null will cause de-reference error... And passing empty map will TRUE the condition != null... Since empty map is not null map
    – sfdcnewbie
    Sep 20, 2019 at 6:41
  • Please see my answer
    – Sarvesh
    Sep 20, 2019 at 7:31
  • IMHO always pass instantiated collections, even if empty rather than passing null. Code that references a map collection will generally use if myMap.contains(someKey)... or for (SomeType t: myMap.values()) {.. logic anyway and hence do the right thing, even if map is empty.
    – cropredy
    Sep 20, 2019 at 21:06

2 Answers 2

3

A better approach would be to overload methods. You can create 2 methods with different parameters for your use case as below:

public class classC {

    public static Object meth1(List<String> lstStr, Boolean bool, Map<integer,string> testMap) {
        // logic
    }

    public static Object meth1(List<String> lstStr, Boolean bool) {
        // logic
    }
}

Depending on the parameters you pass, appropriate method will be invoked.

1
  • Thanks @salesforce-sas but I've ~100 lines of code in meth1... But I'll try to make a new common method which these two overloading methods can further call. Need heavy restructuring though
    – sfdcnewbie
    Sep 20, 2019 at 6:44
1

Pass the empty map as a parameter and check if map.isEmpty() Map documentaion

class c{
    static void method(List<String> lstStr, Boolean bool, Map<integer,string> testMap){
    if(!testMap.isEmpty()){
      //logic
    }
    else{
    }
 }
}

class B {
    //pass the empty map in method
    c.method(lstStr,true,new Map<integer,string>());
}

class A{
    //pass the data is map
    c.method(lstStr,true,testMap)
}
2
  • Thanks again @Sarvesh - This is exactly what I've done right now and that's the question :) Is it acceptable and best practice?
    – sfdcnewbie
    Sep 20, 2019 at 8:03
  • Yes it is the best way to handle map, or else if your map logic is completely different than you should create a separate method with the same name as difference params mean - method overload, see salesforce-sas answer
    – Sarvesh
    Sep 20, 2019 at 8:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .