SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Bug Cor 111

Inspired by

Original Description

“The return value of this method should be checked. One common cause of this warning is to invoke a method on an immutable object, thinking that it updates the object.” – 2013-10-14

Detailed Description

When a method that returns a result is invoked but the result is not used at the call site the programmer has likely not understood the method's behaviour.

Often, she might expect that the method modifies the receiver object. However, that is not possible if the receiver object is immutable. Thus the method must return a modified copy of the object.

In the scenario below, the programmer expects that the trim() method will update the String referenced by dateString. But strings are immutable and thus the trim method returns a new string object, which is ignored in the following scenario.

Sample Problem Scenario

There is a problem whenever a method that returns a result is not part of an enclosing expression but used as a statement:

<code Java> public class MethodIgnoresReturnValue {

static String date = "18/11/2013";

void StringOperation()

{
	   String dateString = date;
	   dateString.trim();  
}

} </Code>

Sample Fix

We can reassign the return value of the trim method to the dateString variable. <code Java>

public class MethodIgnoresReturnValue {

static String date = "18/11/2013";

void StringOperation1()

{
	   String dateString = date;
	   dateString = dateString.trim();
}

} </Code>

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor111.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023