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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:09:16+00:00 2026-05-14T15:09:16+00:00

Something like this (yes, this doesn’t deal with some edge cases – that’s not

  • 0

Something like this (yes, this doesn’t deal with some edge cases – that’s not the point):

int CountDigits(int num) {
    int count = 1;
    while (num >= 10) {
        count++;
        num /= 10;
    }
    return count;
}

What’s your opinion about this? That is, using function arguments as local variables.
Both are placed on the stack, and pretty much identical performance wise, I’m wondering about the best-practices aspects of this.
I feel like an idiot when I add an additional and quite redundant line to that function consisting of int numCopy = num, however it does bug me.
What do you think? Should this be avoided?

  • 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-14T15:09:16+00:00Added an answer on May 14, 2026 at 3:09 pm
    1. As a general rule, I wouldn’t use a function parameter as a local processing variable, i.e. I treat function parameters as read-only.

      In my mind, intuitively understandabie code is paramount for maintainability, and modifying a function parameter to use as a local processing variable tends to run counter to that goal. I have come to expect that a parameter will have the same value in the middle and bottom of a method as it does at the top. Plus, an aptly-named local processing variable may improve understandability.

      Still, as @Stewart says, this rule is more or less important depending on the length and complexity of the function. For short simple functions like the one you show, simply using the parameter itself may be easier to understand than introducing a new local variable (very subjective).

      Nevertheless, if I were to write something as simple as countDigits(), I’d tend to use a remainingBalance local processing variable in lieu of modifying the num parameter as part of local processing – just seems clearer to me.

    2. Sometimes, I will modify a local parameter at the beginning of a method to normalize the parameter:

      void saveName(String name) {
        name = (name != null ? name.trim() : "");
        ...
      }
      

      I rationalize that this is okay because:

      a. it is easy to see at the top of the method,

      b. the parameter maintains its the original conceptual intent, and

      c. the parameter is stable for the rest of the method

      Then again, half the time, I’m just as apt to use a local variable anyway, just to get a couple of extra finals in there (okay, that’s a bad reason, but I like final):

      void saveName(final String name) {
        final String normalizedName = (name != null ? name.trim() : "");
        ...
      }
      
    3. If, 99% of the time, the code leaves function parameters unmodified (i.e. mutating parameters are unintuitive or unexpected for this code base) , then, during that other 1% of the time, dropping a quick comment about a mutating parameter at the top of a long/complex function could be a big boon to understandability:

      int CountDigits(int num) {
          // num is consumed
          int count = 1;
          while (num >= 10) {
              count++;
              num /= 10;
          }
          return count;
      }
      

    P.S. 🙂
    parameters vs arguments
    http://en.wikipedia.org/wiki/Parameter_(computer_science)#Parameters_and_arguments

    These two terms are sometimes loosely used interchangeably; in particular, “argument” is sometimes used in place of “parameter”. Nevertheless, there is a difference. Properly, parameters appear in procedure definitions; arguments appear in procedure calls.

    So,

    int foo(int bar)
    

    bar is a parameter.

    int x = 5
    int y = foo(x)
    

    The value of x is the argument for the bar parameter.

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

Sidebar

Related Questions

No related questions found

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.