troubleshooting Question

Refactor a Java code

Avatar of Rohit Bajaj
Rohit BajajFlag for India asked on
Java
4 Comments1 Solution102 ViewsLast Modified:
Hi,
I want to refactor the following code :
String sharerUserId = "", subCommand = "", repository = "";
   
 if (jsonNode != null) {
    
    String[] commands = jsonNode.asText().split(" ");
   
     sharerUserId = commands[0];
     
   StringBuilder subCommandBuffer = new StringBuilder();
  
      for (int i = 1; i < commands.length - 1; i++) {
 
           subCommandBuffer.append(commands[i]);
    
        subCommandBuffer.append(" ");
      
  }
       
 subCommand = subCommandBuffer.substring(0, subCommandBuffer.length() - 1);
 
       repository = commands[commands.length - 1];
    
}
}
In the above code i am extracting sharerUserId subCommand and repository from jsonNode.
This code was initially a part of a function. But i dont want to keep this code in that function as it destroys the SLAP principle that is single level of abstraction. To make the function cleaner i want to move this part out to a separate function. How do i do it ?
One way i think is to make a class with fields sharerUserId, subCommand and repository.
But I want to avoid this as this way i could come up with lots of unneccesary classes.. If i could pass these string to some other function which could be set from there ... but this is not possible as Strings will be passed by value.
Please suggest some Good approaches for the same...
The sharerUserId , subCommadn and repository is used later in the original function...

Thanks
ASKER CERTIFIED SOLUTION
dpearson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros