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 4324386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:07:32+00:00 2026-05-21T09:07:32+00:00

In Java, given a pair of public methods, enableFoo and disableFoo, that set a

  • 0

In Java, given a pair of public methods, enableFoo and disableFoo, that set a boolean field named isEnabledFoo to true or false, respectively, should the method check to see if Foo is already enabled before setting? If yes, should an exception be thrown? If yes, should it be checked or unchecked? If unchecked, what should it throw? IllegalStateException?

A twist: Although the current implementation simply sets a boolean field, I’m purposely not implementing it as one setFoo “setter” method with a boolean parameter because the implementation might be changed later on to include side effects (maybe even not setting a field at all). Keeping it as enableFoo/disableFoo seemed the best way to guarantee encapsulation.

Should I do this:

public void enableFoo() throws myCheckedException
{
  if (! this.isEnabledFoo)
  {
    this.isEnabledFoo = true;
  }
  // Is this overkill?
  else
  {
    // Foo is already enabled...Should a checked exception be thrown?
    throw new myCheckedException ("Foo is already enabled.");

    // What about an unchecked exception?
    // throw new IllegalStateException ("Foo is already enabled.");
  }
}

or simply like this:

public void enableFoo()
{
  // I guess we don't care if foo is already enabled...
  this.isEnabledFoo = true;
}

or even:

public void enableFoo()
{
  // Is this just code bloat?
  if (! this.isEnabledFoo)
  {
    this.isEnabledFoo = true;
  }
}

I think that while the last two versions are simpler, they would hide a possible bug where the developer was calling enableFoo thinking that it was disabled, when in fact it was already enabled (but would that really matter?). What’s the best design here?

  • 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-21T09:07:33+00:00Added an answer on May 21, 2026 at 9:07 am

    I don’t really think there’s an answer to your question. all of them seem valid, it depends on the operation you are doing and on the consistent state of the object following the operation.

    For instance: I have a method which enables syntax highlighting, if you choose to enable it and it is already enabled what would you care. In such a case you would simply set the boolean flag regardless of the previous state (option 2).

    If on the other hand I don’t just change a boolean field but rather perform some complex logic which may be expensive to re-run such as go over the DOM of the document and color all the right places I wouldn’t want to do it again just because someone “rechecked” the enable flag.

    Something like this:

    public void enableHighlighting() {
      if (!isHighlighting) {
        isHighlighting = true;
        colorView();  // very expensive method
      }
    }
    

    Now let’s assume you have a case where double enabling reflects an inconsistent state of the machine (something like what you would use assert for. In this case you would want to throw an exception to denote that the user did something illegal.

    For instance let’s say syntax highlighting only affects certain types of files such as XML or Java code, and the user tries to apply it to PHP code. Now the highlighting is already enabled but the frustrated user tries to enable it because he doesn’t see any change. In such a case you might want to print a message to the user saying that highlighting is already enabled. In such a case a return value for whether a change occurred would probably make more sense, but you could use an exception as well.

    Return value:

    public boolean enableHighlighting() {
      prevState = isHighlighting;
      isHighlighting = true;
      return prevState != isHighlighting; //return true if value changed
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following java enum: public enum AgeRange { A18TO23 { public String toString()
I am looking for a framework to turn given Java class into WebService (may
Given a java.util.Date object how do I go about finding what Quarter it's in?
Given the following Java code for generating a binary file: DataOutputStream out = new
In Java, given a timestamp, how to reset the time part alone to 00:00:00
Given a large API (an in particular, the Java or J2EE standard libraries), is
I have a Java object which is able to configure itself given an XML
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
What is the easiest way to test (using reflection), whether given method (i.e. java.lang.Method
I'm writing a Java application that will instantiate objects of a class to represent

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.