Since uni, I’ve programming in Java for 3 years, although I am not fully dedicated to this language, I have spent quite some time in it, nevertheless. I understand both ways, just curious which style do you prefer.
Please focus at the main method – that’s the section I concern the most.
public class Test
{
public static void main(String[] args)
{
System.out.println(getAgent().getAgentName());
}
private static Agent getAgent()
{
return new Agent();
}
}
class Agent
{
private String getAgentName()
{
return "John Smith";
}
}
I am pretty happy with nested method calls such like the following
public class Test
{
public static void main(String[] args)
{
System.out.println(getAgentName(getAgent()));
}
private static String getAgentName(Agent agent)
{
return agent.getName();
}
private static Agent getAgent()
{
return new Agent();
}
}
class Agent
{
public String getName()
{
return "John Smith";
}
}
They have identical output I saw “John Smith” twice.
I wonder, if one way of doing this has better performance or other advantages over the other. Personally I prefer the latter, since for nested methods I can certainly tell which starts first, and which is after.
The above code is but a sample, The code that I am working with now is much more complicated, a bit like a maze… So switching between the two styles often blows my head in no time.
I would recommend against the latter style, since it mixes data retrieval with “busines logic” (i.e. what to do with the data).
With the first approach you keep a high degree of freedom in what you want to do with the data, e.g.
If you chose to do things this way at least name the functions with names that clearly describes what is done, e.g.