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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:58:58+00:00 2026-05-21T13:58:58+00:00

Here’s a situation that comes up quite often that never sits too easy with

  • 0

Here’s a situation that comes up quite often that never sits too easy with me. I thought I’d ask how others handle it.

Imagine if the handling of a demo=60 command line argument was done like this:

if DemoOptionSpecified() {
  timeout = ReadInDemoTimeout();
  DoDemoVersion(timeout);
} else
  DoRealVersion();

DemoOptionSpecified() does some sort of grep on the argument string and return true or false.

ReadInDemoTimeout() also does some sort of grep, same string, and returns an integer.

Two greps doing two different things, but of course only a single grep is needed to do both. Two greps instead of one might not matter here, but in other scenarios, two database or Ajax calls might.

I don’t particularly like having DemoOptionSpecified() do anything more than seeing if the option is supplied. An additional capturing of the value would not be suggested by the method name.

I don’t particularly like the alternative of having a method called ReadInDemoTimeout() returning false if the demo option doesn’t exist, as I only want to hear about timeout values if the option is set. DoRealVersion() does not care about the timeout value.

I don’t feel there’s a good no-compromise clean-code pattern for this. Thoughts?

  • 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-21T13:58:58+00:00Added an answer on May 21, 2026 at 1:58 pm

    I don’t see any problem with having a method that does both – you just have to name it correctly:

    DemoOption demoOption = getDemoOption();
    if (demoOption.wasSpecified()) {
        int timeout = demoOption.getValue();
        doDemoVersion(timeout);
    }
    else {
        doRealVersion();
    }
    

    You could even make it simpler, and have the method return the value, or null if the option was not set:

    Integer demoTimeout = getDemoOptionIfSpecified();
    if (demoTimeout != null) {
        doDemoVersion(demoTimeout);
    }
    else {
        doRealVersion();
    }
    

    And then i’d make the method general-purpose:

    Integer demoTimeout = getOptionIfSpecified("demo", Integer.class);
    if (demoTimeout != null) {
        doDemoVersion(demoTimeout);
    }
    else {
        doRealVersion();
    }
    

    I don’t see that as being a method which does two things. The one thing it does is “get the valueof the option if there is one”. You can then ask two questions about the result – is there one, and what is its value? – but that’s happening in the calling code.

    If you insist on not bringing back the value unless it’s needed, how about injecting it?

    interface OptionHandler<T> {
        public void specified(T optionValue);
        public void notSpecified();
    }
    
    handleOptionIfSpecified("demo", new OptionHandler<Integer>() {
        public void specified(Integer timeout) {
            doDemoVersion(timeout);
        }
        public void notSpecified() {
            doRealVersion();
        }
    });
    

    But seriously, if i was reading your code and i saw anything other than the third version, i’d see overcomplication and start refactoring. The idiom of speculatively getting a value and dealing with null and non-null cases differently is very widespread (in Java, at least); no useful purpose is served by avoiding it in pursuit of some notional kind of purity.

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

Sidebar

Related Questions

Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is my problem...I have a page that loads a list of clients and
Here is the situation, I am attempting to fire a set of Gallio tests
Here is my situation: I am using telerik with winform. I have a dataset
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is my code (Say we have a single button on the page that
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here's what I'm trying to do... It's quite simple and obviously there must be
Here is another spoj problem that asks how to find the number of distinct

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.