-1

Say that you have an web application which prints pages in a document. Suppose that after validating the page range, the application does the following:

  1. If a billing option is turned on, it first checks with the server to confirm the print.

  2. After confirming the print (or doing nothing if the billing option is not turned on), it then prints the pages.

In order to support billing being both on and off, the code looks something like this:

function printPages (...) {
    if (billing) {
        confirmPrintWithServer(..., function () {
            printPagesInternal(...);
        });
    } else {
        printPagesInternal(...);
    }
}

If the call to check the printing was synchronous, this wouldn't really be a problem. Then it would look like this:

function printPages (...) {
    if (billing) {
        if (!confirmPrintWithServer(...)) return;
    }
    // continue with rest of printPages
}

What went in a "printPagesInternal" in the first case would simply be the rest of printPages in the synchronous case.

Is there any better way to name these two things than appending "Internal" or something similar to the end of the one that the caller of the code does not see? If we had more layers of checks, would we do "InternalInternal"? Is the "more layers of checks" situation just not going to happen, so adding "Internal" is actually the right solution?

3
  • printPagesImpl, printPagesHelper, printPagesCallback, sendPageDataToPrinter, printConfirmedPages, displayPrintPreviewPopup, etc. It depends to some extent on what the "internal" method actually does. Commented Feb 24, 2016 at 0:06
  • 1
    On the troubles of naming and terminology Commented Mar 7, 2016 at 20:48
  • @Snowman I believe my question falls squarely into category 2 (Questions about "principles of naming things."). The specific example in the question is just that - an example. The question and potential answers were intended to apply to all problems similar to the title. (The title of this question is intentionally general.) I probably didn't emphasize that enough though. Thanks for the link. Commented Mar 7, 2016 at 22:35

1 Answer 1

-1

After some more thought, I realized that you can describe the function by way of what it's not doing.

In this case, that means you would have printPages and printPagesWithoutBilling. So it becomes:

function printPages (...) {
    if (billing) {
        confirmPrintWithServer(..., function () {
            printPagesWithoutBilling(...);
        });
    } else {
        printPagesWithoutBilling(...);
    }
}

This way, it wouldn't confuse the programmer calling it, as long as they know that printPages does do billing. It's a bit like compareStrings and compareStringsIgnoringCase.

Another option would be to do printPagesAfterBilling. This might be useful if you have a long function with many async steps, because then you don't have to use multiple "withoutX"s in your function name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.