在这种情况下,我的Switch语句应该以break退出还是返回x?

用户名

在我的应用程序中,用户可以选择文本并使用图像上显示的选项设置文本的样式。在查看代码时,我想知道我所有switch语句的case语句是否应该,break或者return true/false是否有任何影响?我可以看到,通过使用Log.d();该方法break可以切换开关,而在开关中使用return停留

因此,在这种情况下,发生的情况是否重要?

在此处输入图片说明

Switch情况下的方法:

@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

使用return停留在开关中

这是不正确的。return言最肯定不会留在switch声明。Areturn将立即从方法中返回。该方法中的其余代码将不会执行。因此,使用时,您看不到switch语句之后的log语句return

abreak或areturn将退出switch语句。任一个就足够了。关于在方法中使用多个return语句的争论很多

某些人希望return方法末尾只有一个语句。这种做法的好处是毫不奇怪:在您维护软件并需要进行更改时,可以在切换后添加语句,这些语句将被执行。您不需要找到每个return调整每个。

但是,有时可以很好地使用多个return语句。例如,如果您return立即测试错误情况,这可以使代码更短并避免不必要的深度缩进。有时,它需要多余的标志和复杂的逻辑,以避免额外的return语句。

最终,请考虑在未来数月和数年内阅读和理解代码的清晰性和便捷性。那就是区别很重要的时候。以最简单,最清晰的方式编写代码。我建议分开您的switch语句,并为每个块使用单独的方法。然后,该声明将变得更短,更清晰。您将能够在屏幕上看到每种情况,而不会滚动并迷失在噪声中,并且能够看到每种情况breaks或使用if-else语句,这样当您失败时不会感到惊讶,case因为您忘记了break

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在这种情况下,我应该使用System.in.close()?

在这种情况下,对于我的UML图,我的依赖关系应该从类到接口还是从类到具体类型?

在这种情况下我应该使用哪种设计?

在这种情况下,我应该使用隔离范围吗?

.NET异步/等待:在这种情况下,我应该返回一些东西吗?

在这种简单情况下,我应该坚持使用LINQ吗?

在这种情况下我应该如何使用git

在这种情况下,我应该在哪里叫删除?

在这种情况下我应该使用智能指针吗?

我应该避免在现代C ++中引用指针还是在这种特殊情况下还可以

在这种情况下,我应该使用reduceLeft方法吗?

在这种情况下,我应该使用应用内购买吗?

在这种情况下,我应该如何使用diff?

在这种简单情况下,我应该使用同步块吗?

在这种情况下,我需要“默认”语句吗?

在这种情况下,我需要Web服务还是Hessian?

在这种情况下,我该如何缩短if语句?

在这种情况下是INNER JOIN还是嵌套SELECT?

laravel在这种情况下我应该使用哪个路由或链接

在这种情况下,应该使用大括号{}还是方括号[]?

在这种情况下,我真的应该实现IDisposable吗?

在这种情况下,我应该如何处理Clojure中的可变状态?

在这种情况下,我应该使用哪个分类器或ML SDK?

iOS:在这种情况下我应该使用委托还是 NSNotification?

Selenium remotewebdriver timeout 应该(在这种情况下)返回 true

在这种情况下我应该使用哪个 sqlite 查询

在这种情况下我应该检查指针吗?

在这种情况下,什么更好:扩展还是功能?

在这种情况下如何退出android中的活动?