1

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.

  • Could you please be more mindful of your code formatting? The indentation is all over the place (and you seem to be mixing tabs and spaces, making it difficult to format well, in addition to the extraneous spaces). – Oded Oct 26 '13 at 10:35
  • 1
    One thing I sometimes do is use a text editor (or the IDE) and paste directly from it for my code. – Oded Oct 26 '13 at 10:37
  • 1
    Yes, these are generally OK. See what is on-topic here. Conceptual issues tend to be fine. – Oded Oct 26 '13 at 10:39
  • 1
    @Oded I will do, notepad ++++ and I read what's on topic, it's just I'm in the baby stage.. and feel like it's probably like 1+1 to you guys.. I am pedantic and like to understand what and why I am doing things, if I get good habits now, it will pay later – user208372 Oct 26 '13 at 10:41
  • 1
    Most certainly. If only more developers were as mindful of their learning. – Oded Oct 26 '13 at 10:41
0

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. ;-)

| improve this answer | |
1

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.

| improve this answer | |
  • thank you comprehensive and a useful guide for me to use – user208372 Oct 26 '13 at 11:19
  • accepted other answer as he addressed private vs public sorryr – user208372 Oct 26 '13 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy