Answer the question
In order to leave comments, you need to log in
A little about the correctness of writing code. Which option to choose?
Let's say we have a click handler like this:
@Override
public void onButtonClick() {
if (isRequested()) {
if (rulesField.isChecked()) {
register(numberField.getText().toString(), codeField.getText().toString());
} else {
showMessage(R.string.error_rules);
}
} else {
requestRegister(numberField.getText().toString());
}
}
@Override
public void onButtonClick() {
if (isRequested() && rulesField.isChecked()) {
register(numberField.getText().toString(), codeField.getText().toString());
return;
}
if (isRequested() && !rulesField.isChecked()) {
showMessage(R.string.error_rules);
return;
}
if (!isRequested()) {
requestRegister(numberField.getText().toString());
}
}
Answer the question
In order to leave comments, you need to log in
Many levels of nesting are hard to read. Try adding conditions inside
if (rulesField.isChecked()) {
register(numberField.getText().toString(), codeField.getText().toString());
}
@Override
public void onButtonClick() {
if (isRequested()) {
if (rulesField.isChecked()) {
if (canProceed()) {
register(numberField.getText().toString(), codeField.getText().toString());
} else if (isExtraRequestRequired()) {
doSomething();
} else if (oneMoreCondition()) {
doSmomethingOnExtraCondition();
} else {
Log.e("Unexpected condition");
}
} else {
showMessage(R.string.error_rules);
}
} else {
requestRegister(numberField.getText().toString());
}
}
what they suggested is worse
isRequested() = false -> if twitches 3 times
in your case once
well, the second option is poorly readable
if there are a lot of switch investments to help
Answer option Ivan Sokolov
if (!isRequested()) {
requestRegister(numberField.getText().toString());
return;
}
if (rulesField.isChecked()) {
register(numberField.getText().toString(), codeField.getText().toString());
} else {
showMessage(R.string.error_rules);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question