Whether to separate out methods or not

Posted by Skippy on Programmers See other posts from Programmers or by Skippy
Published on 2013-10-26T10:18:50Z Indexed on 2013/10/26 16:07 UTC
Read the original article Hit count: 270

Filed under:
|

I am new to 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.

© Programmers or respective owner

Related posts about java

Related posts about clean-code