-1

I was wondering about which type of code should I write:

public BigDecimal getItemSubTotal(BigDecimal quantity) {
        return getBasePrice().multiply(quantity).multiply(getRentalAdjustment()).add(getOtherAdjustments());
}

OR

public BigDecimal getItemSubTotal(BigDecimal quantity) {
    BigDecimal basePrice = getBasePrice();
    BigDecimal rentalAdj = getRentalAdjustment();
    BigDecimal otherAdj = getOtherAdjustments();
    return basePrice.multiply(quantity).multiply(rentalAdj).add(otherAdj);
}

Which code block is better apart from code readability & why? Which code block will take less time & memory?

Lalit Dashora
  • 467
  • 5
  • 16

3 Answers3

4

apart from code readability

The only difference between these two specific examples is that in the first case, the calls to getRentalAdjustment() and getOtherAdjustments() won't be made if - somehow - a BigDecimal.multiply call fails.

The only documented way for BigDecimal.multiply to fail is if it is passed a null operand. (And, of course, invoking multiply on a null receiver would too, if getBasePrice() returned null).

Other than that, nothing. It's simply down to readability.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • It is easier to debug the second code block since the local variables will automatically show up in your debugger context. – leftbit Jan 04 '19 at 11:49
  • @leftbit that's not a reason to write ugly code. Any remotely sane debugger will not struggle with inline method calls. – Boris the Spider Jan 05 '19 at 07:01
  • @BoristheSpider Question was "Which code block is better apart from code readability & why?", so aesthetics don't enter into this question. – leftbit Jan 07 '19 at 08:30
  • @leftbit removing the value judgment about the ugliness of the code, another way to interpret Boristhespider's comment is that you shouldn't (and shouldn't need to) change the way you write the code simply for the debugger. – Andy Turner Jan 07 '19 at 09:09
  • @AndyTurner Of course this should be the rule. Just arguing another aspect of maintainability here. – leftbit Jan 07 '19 at 13:45
0

Given the:

  1. Memory & time benefits are negligable as there is no significant difference in complexity;
  2. As @AndyTurner mentioned - functions are not executed in the same order, and failure of one function can prevent execution of subsequent calls.
  3. Sonar/Linting tools will not like local unnecessary variables;
  4. Some linting tools may see long lines as an issue:

I would opt for:

public BigDecimal getItemSubTotal(BigDecimal quantity) {
        return getBasePrice()
            .multiply(quantity)
            .multiply(getRentalAdjustment())
            .add(getOtherAdjustments());
}
Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
-1

It may depends on your requirement. But i would like to highlight some points like:

  • Second one is more readable than first one
  • In second one you will be able to perform exception handling for each call, but in first one you can't do the same.
  • Each variable creation in java takes 4 bytes (as the variable reference is an integer value, and integer takes 4 byte in java). So with respect to memory first one is better than second one
Deepak Kumar
  • 1,246
  • 14
  • 38
  • @BoristheSpider, How can you say that last point is not correct. Each reference in java takes 4 Bytes of memory. FYR [link](https://stackoverflow.com/questions/1348008/memory-allocation-how-much-space-does-a-reference-occupy-in-java) – Deepak Kumar Jan 06 '19 at 07:05
  • It's not that simple. It depends on the type of machine (32/64) and whether compressed references are being used. – Boris the Spider Jan 06 '19 at 08:45