Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6180837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:54:59+00:00 2026-05-24T00:54:59+00:00

So I am a relatively new programmer and am still learning, and have run

  • 0

So I am a relatively new programmer and am still learning, and have run into a bit of an issue (so apologies if my tags are a little off). Early on I had a chance to read an article discussing the advantages of breaking up methods that contained lots of code into sets of calls to many smaller, clearly-named methods. In general I felt like this made cleaner looking code, and definitely made unit testing a lot easier. However, I have some concerns about whether to make all these new methods public or private. Making them private seems like the right thing to do, since the rest of the code generally doesn’t need access to these methods. However, unit testing private methods can be messy. Is there a best practice for this?

What I’m doing right now:

 public class WashingMachine {
      public Load wash(Load load) {
           // Removes one sock from load
           for (ClothingItem item : load.getItems()) {
                if (item.getType().equalsIgnoreCase("sock") {
                     load.removeItem(item);
                     .. // logic for sending sock to proper dimension
                     break;
                }
           }
           // rest of logic for cleaning clothes
      }
 }

Turns into:

 public class WashingMachine {
      // Wash a load of laundry
      public Load wash(Load load) {
           // Removes one sock from load
           removeSock(load.getItems());
           // rest of logic for cleaning clothes
           ..
      }

      // Oh no, I can't unit test this easily!
      private void removeSock(List<ClothingItem> items) {
           ...
      }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:55:00+00:00Added an answer on May 24, 2026 at 12:55 am

    Think of your public methods as functionality you want to allow anybody to use.

    Some of this functionality may be complicated, and you’re right, that functionality should be split up into different methods. The question is, “What kind of access do they have?”

    Well, if they’re public, now external classes can access your internal functionality, which you really don’t want. Even if it won’t break anything, when you start working on large projects (with larger teams of people), by making it public, other programmers might use that functionality, which now prevents you from changing it.

    For example, taking a car analogy. Say your method is accelerate(). This will probably call releaseGas(). However, you don’t want anybody externally to release gas. It should only be called in a controlled environment.

    Then there’s protected access. Protected access is for methods that may be overridden by extending classes. These should be methods that do a specific chunk of work related to internal functionality. Taking the car example again, there might be a RaceCar clas which uses a special type of gas, and so it wants to provide its own method of releasing gas. This would be a reason for releaseGas to be protected.

    As for tests, you should be testing, primarily, your public contract. That’s what other classes use, and at the end, that’s what really matters. Your internal methods are internal to your functionality, and will be tested by way of testing your public contract. This also means your class should be designed based on its external uses first, and the tests built around that. (Even with test-driven development, this gets easier as you gain experience.)

    Granted, sometimes those internal methods are complicated enough that they warrant their own unit tests. However, you don’t need to make them protected. In that case, you can make them default access. (Neither public or protected). As long as your unit test is in the same package, you’ll be able to call them.

    Better yet, if you know that you don’t want anyone to extend it but you need it to be tested, make it protected final or just final. This still allows being able to call it, but at least prevents it being overridden.

    Edit:

    Ryan’s comment below is dead on. If your class is getting complex enough that it needs testing of its internal methods, those should probably be extracted into their own class, which can then be independently unit-tested.

    Overall, your tests should be testing the public contract of your separate units.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface Let me start off by saying that I'm a relatively new programmer and
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
Caveat: I'm relatively new to coding as well as TextMate , so apologies if
relatively new programmer here. Look at the two classes defined below. I'd like to
I'm an experienced programmer, but relatively new to SQL. We're using Oracle 10 and
Relatively new to mySQL so this is probably an easy one: I have a
I'm relatively new to ASP.NET MVC so I'm having a little trouble with this
Being relatively new to the software industry I have come across a question of
I'm relatively new to Nant, what i'd like to do is have a task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.