Muddz Muddz - 1 month ago 7
Java Question

Should my Switch statment exit with break or return x in this case?

In my app the user can select a text and style it with the options as seen on the image. While looking at my code, I wondered if all my switch statement's cases statements shall

break
or
return true/false
and if it has any impact at all? I can see by using
Log.d();
that
break
goes out of the switch method and using
return
stays in the switch

So in this case does it matter what happends in the cases?

enter image description here

The method with the Switch cases:

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {


switch (item.getItemId()) {

case R.id.textcolor:

f3 = ColorPickerDialogFrag2.newInstance(3, Color.WHITE);
f3.setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.AppTheme);
f3.show(fragmentManager, "d");

f3.setListener(this);

break;


//--------------------BOLD----------------------------
case R.id.bold:

styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);

for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(styleSpans[i]);
exists = true;
}
}

if (!exists) {
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}

editText.setSelection(selectionStart, selectionEnd);

return true;
//--------------------ITALIC----------------------------
case R.id.italic:

styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);

for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.ITALIC) {
str.removeSpan(styleSpans[i]);
exists = true;
}
}

if (!exists) {
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), selectionStart, selectionEnd,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}

editText.setSelection(selectionStart, selectionEnd);
Log.d(LOG_TAG, "italic");
break;
//--------------------UNDERLINE----------------------------
case R.id.underline:

UnderlineSpan[] underSpan = str.getSpans(selectionStart, selectionEnd, UnderlineSpan.class);

for (int i = 0; i < underSpan.length; i++) {
str.removeSpan(underSpan[i]);
exists = true;
}


if (!exists) {
str.setSpan(new UnderlineSpan(), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}

editText.setSelection(selectionStart, selectionEnd);
Log.d(LOG_TAG, "underline");
return true;

//--------------------STROKE----------------------------
case R.id.stroke:
Log.d(LOG_TAG, "stroke");
android.text.style.StrikethroughSpan[] strokeSpan = str.getSpans(selectionStart, selectionEnd, android.text.style.StrikethroughSpan.class);

for (int i = 0; i < strokeSpan.length; i++) {
str.removeSpan(strokeSpan[i]);
exists = true;
}

if (!exists) {
str.setSpan(new android.text.style.StrikethroughSpan(), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}

editText.setSelection(selectionStart, selectionEnd);

return false;

case R.id.increase:
Log.d(LOG_TAG, "increase");
str.setSpan(new RelativeSizeSpan(1.1f), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
editText.setSelection(selectionStart, selectionEnd);

break;

case R.id.decrease:

str.setSpan(new RelativeSizeSpan(0.9f), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
editText.setSelection(selectionStart, selectionEnd);

break;

case android.R.id.cut:

CharSequence charSequence = editText.getText().subSequence(selectionStart, selectionEnd);
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text", charSequence);
clipboard.setPrimaryClip(clip);

editText.getText().replace(selectionStart, selectionEnd, "");
Toast.makeText(context, R.string.toastCopy, Toast.LENGTH_SHORT).show();

break;

case android.R.id.copy:

charSequence = editText.getText().subSequence(selectionStart, selectionEnd);
clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
clip = ClipData.newPlainText("simple text", charSequence);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, R.string.toastCopy, Toast.LENGTH_SHORT).show();

break;

case R.id.textfont:

FontFragment fontFragment = new FontFragment(selectionStart, selectionEnd, editText);
fontFragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
fontFragment.show(fragmentManager, "fontfragment");

break;
}
}

Log.d(LOG_TAG, "out of switch");
return true;

}

dsh dsh
Answer

using return stays in the switch

This is incorrect. A return statement most definitely does not stay in the switch statement. A return will return from the method immediately. The remainder of the code in the method will not be executed. That is why you do not see the log statement that is after your switch statement when you use return.

Either a break or a return will exit the switch statement. Either one can suffice. There is a lot of debate regarding the use of multiple return statements in a method.

Some people would prefer that you have only one return statement at the end of the method. This practice has the benefit of least surprise: as you maintain your software and need to make changes, you can add statements after the switch and they will be executed. You don't need to find each return and adjust each one.

Sometimes, however, there are good uses of multiple return statements. For example, if you test for an error condition and return immediately, this can keep your code shorter and avoid unnecessarily deep indentation. Sometimes it requires excess flags and convoluted logic to avoid extra return statements.

Ultimately, consider the clarity and ease of reading and understanding your code months and years in the future. That is when the difference will be important. Write your code in the simplest and clearest way possible. I would recommend breaking your switch statement apart and having separate methods for each block. Then the statement will be shorter and clearer. You'll be able to see each of the cases on your screen without scrolling and getting lost in the noise and be able to see each of the breaks. Or use an if-else statement so that you can't be surprised when a case falls through because you forgot a break.

Comments