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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:17:36+00:00 2026-05-17T19:17:36+00:00

Compare this method: void doStuff(String val) { if (val == null) { val =

  • 0

Compare this method:

void doStuff(String val) {
    if (val == null) {
        val = DEFAULT_VALUE;
    }

    // lots of complex processing on val
}

… to this method:

void doStuff(String origVal) {
    String val = origVal;
    if (val == null) {
        val = DEFAULT_VALUE;
    }

    // lots of complex processing on val
}

For the former method, Eclipse emits the warning “The parameter ‘val’ should not be assigned”. Why?

To my eye, the former is cleaner. For one thing, it doesn’t force me to come up with two good names for val (coming up with one good one is hard enough).

(Note: Assume there is no field named val in the enclosing class.)

  • 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-17T19:17:36+00:00Added an answer on May 17, 2026 at 7:17 pm

    It doesn’t look like anyone’s made the curmudgeon’s case here.

    I would generally not mutate parameters, and in fact I tend to mark my parameters final in order to explicitly forbid it. A few reasons:

    • Assignment to a parameter could be confused with an attempt to use it as an “output parameter”, ref: javapractices.com, and clarity is everything

    • Favor Immutability, and that goes for parameter values as much as anything else. Primitives are just a degenerate case of the same thing, it’s (generally) easier to reason about immutable variables. Ref, Effective Java Item 13, or javapractices.com

    • And finally (NPI), Use final liberally, javapractices.com. However ugly it may be in parameter signatures, I believe it tends to identify unexpected errors and it highlights mutable variables, which generally should be the exception. Most mutable variables in most code are there either for laziness or a perception that it has some effect on performance, when judiciously chosen, immutable, and well-named intermediate computations are clearer, easier to read and verify, and can be cleanly optimized for performance with no help from you.

    I can’t speak intelligently to your specific case in the abstract, but barring all the other things I might do differently, I’d favor:

    void doStuff(final String origVal)
    {
        final String valOrDefault = (origVal == null) ? DEFAULT_VALUE : origVal;
        //lots of complex processing on valOrDefault 
    }
    

    or even (assuming you wouldn’t cope with a null value in a real method with only one argument, it must be part of something more complex)… Also, in general, methods which accept null as a parameter should be explicitly documented as doing so, if only to reinforce the assumption that null parameters ought to be the exception. In the second method, you might even make use of the @NonNull annotation.

    /**
      * @param origVal string giving value, possibly null, in which case DEFAULT_VALUE is assigned
      */
    void doStuff(final String origVal, ... )
    {
        final String valOrDefault = (origVal == null) ? DEFAULT_VALUE : origVal; 
        // similar mucking about to make all the parameters behave, separate from
        // actually operating on them...
        ...
        reallyDoStuff(valOrDefault,...);
    }
    
    private void reallyDoStuff(final String value, ...)
    {
       assert (value != null);
       // do your complex processing
    }
    

    Related questions (and associated argument) on StackOverflow: “Using final modifier whenever applicable in Java…”, “final keyword in method parameters”, “Do you final-ize local variables and method parameters in Java”.

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

Sidebar

Related Questions

I am using a custom validator to compare value in two text box. This
Compare String.Format(Hello {0}, World); with Hello {0}.Format(World); Why did the .Net designers choose a
I am doing some experimenting with threads, and made a 'control' method to compare
HashMapList keeps its elements inside a HashMap) and when I call add method this
For this assignment I had to create my own string class. I initially wrote
I'm learning VS Unit test and tried this: [TestMethod()] public void calcTest() { double
Background this post explains how one can consume extension methods in Powershell http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx Compare
Compared to most people on this site I am admittedly a novice. I wanted
I would like to compare a screenshot of one application (could be a Web
I would like to compare the contents of a couple of collections in my

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.