Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on 

Refactor a Java code

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];
    
}
}

Open in new window

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
Java

Avatar of undefined
Last Comment
dpearson
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

Hi,
I thought of one more approach... What if i take the code out into a separate function and pass a map like :
Map map = new HashMap<>();
into the function... and then set the three values sharerUserId, subCommand, and repository inside the map... This will also work...
What do you think of this approach compared to the above one which you suggested...
Is there any pros or cons in this ??
Thanks
Avatar of dpearson
dpearson

Yes passing in a Map would work.  However, I'd say it's not as good, because there's no guarantees that the values are set or that the names being used match:

E.g.
Map map = new HashMap() ;
setMap(map) ;
String command = map.get("subcommand") ; // Fails - can you see why?

public void setMap(Map map) {
    map.put("subCommand", command) ;
}

Open in new window


A (small) class makes all of these things impossible.  The fields must be set (if you only set the values in the constructor) and if you ask for the wrong name, the compiler will tell you that it's wrong.

So a map would work, but not great here.  Lots of small classes are fine :)

Doug
Avatar of dpearson
dpearson

Only answer.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo