Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to do Inner assignments?

We were having this discussion wiht my colleagues about Inner assignments such as:

return result = myObject.doSomething();

or

if ( null == (point = field.getPoint()) )

Are these acceptable or should they be replaced by the following and why?

int result = myObject.doSomething();
return result;

or

Point point = field.getPoint();
if ( null == point)
like image 854
Adel Boutros Avatar asked Jun 27 '12 08:06

Adel Boutros


1 Answers

The inner assignment is harder to read and easier to miss. In a complex condition it can even be missed, and can cause error.

Eg. this will be a hard to find error, if the condition evaluation prevent to assign a value to the variable:

if (i == 2 && null == (point = field.getPoint())) ...

If i == 2 is false, the point variable will not have value later on.

like image 193
Matzi Avatar answered Sep 19 '22 14:09

Matzi