I am new to java and want to learn best coding practices and understand why one method is better than another, in terms of efficiency and as the coding becomes more complicated. This is just an example, but I can take the principles from here to apply elsewhere.
I have need an option to display stuff
, and have put the method stuff
separately from the method to ask if the user wants to display the stuff
, as stuff
has a lot of lines of code.
For readability I have done this:
public static void displayStuff ()
{
String input = getInput ("Display stuff? Y/N n");
if (input..equalsIgnoreCase ("Y"))
{
stuff ();
}
else if (input.equalsIgnoreCase ("N"))
{
//quit program
}
else
{
//throw error
System.out.print("Error! Enter Y or N: n");
}
}
private static String stuff ()
{
//to lots of things here
return stuff ();
}
Or
public static void displayStuff ()
{
String input = getInput ("Display stuff? Y/N n");
if (input..equalsIgnoreCase ("Y"))
{
//to lots of things here
stuff;
}
else if (input.equalsIgnoreCase ("N"))
{
//quit program
}
else
{
//throw error
System.out.print("Error! Enter Y or N: n");
}
}
Is it better to keep them together and why?
Also, should the second method be private or public, if I am asking for data within the class?
I am not sure if this is on topic for here. please advise.
8
It obviously depends on what “stuff” in this context is, if it is trivial then the second way is probably fine. On the other hand, if it is something you are going to need to call repeatedly and/or in different circumstances, or are going to want to call elsewhere, then it’s best to abstract it into a method.
That being said, putting it into a method is likely to be better for futureproofing your code (e.g. what if “stuff” changes and becomes unwieldy, meaning you end up having to put it into a method anyway) and for readability. Is “stuff” self explanatory or would it be better encapsulated in a method that names it with clear purpose. Bear in mind, even if it’s just you writing/maintaining the code, you might come back in two months and then you might as well be a different person for all you’ll remember of what the code did.
Re public/private, if you’re only calling it from within that class then it should be private. Again, it depends on the architecture of your program but if it’s an internal working of the class then that should be private, if it needs to be called from elsewhere, then public.
But put more succinctly: Think carefully before making your privates public. 😉
It is a good practice when the code inside the function is long or complex or used by several parts of your logic. For example, when you want to validate person name composed of 4 parts, you may want to isolate the name validation and not repeat it 4 times.
Languages like COBOL thrives on the concept of separating logic and some programming methods revolve around structured programming and top down design.
In OO you should separate class methods into their own of course. However, your question, as I can see, is about code within a method.
It is not so good a practice to isolate code within a method into another function when:
-
You have to return more than one result
-
You have to pass several parameters
-
When the function has not-so obvious side effects
-
Could raise serious errors that would need to be addressed by try-catch in the caller
I say ‘not so good’ because it could lead to confusion or difficulty in reading and possibly unnecessary longer code.
The above are just guidelines to consider before extracting out the code and placing it in a separate function.
2