Is it bad practice to change state inside of an if statement?
Posted
by Benjamin
on Stack Overflow
See other posts from Stack Overflow
or by Benjamin
Published on 2009-11-25T22:31:16Z
Indexed on
2010/05/09
5:18 UTC
Read the original article
Hit count: 246
I wrote some code that looks similar to the following:
String SKIP_FIRST = "foo";
String SKIP_SECOND = "foo/bar";
int skipFooBarIndex(String[] list){
int index;
if (list.length >= (index = 1) && list[0].equals(SKIP_FIRST) ||
list.length >= (index = 2) &&
(list[0] + "/" + list[1]).equals(SKIP_SECOND)){
return index;
}
return 0;
}
String[] myArray = "foo/bar/apples/peaches/cherries".split("/");
print(skipFooBarIndex(myArray);
This changes state inside of the if statement by assigning index. However, my coworkers disliked this very much.
Is this a harmful practice? Is there any reason to do it?
© Stack Overflow or respective owner