Whether to separate out methods or not

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật