- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题重复了一个,但我仍然在问,因为通过使用解决方案中建议的方法,我无法显着降低复杂性。函数复杂度是 28,我必须将其降低到 10 以下。
private void adjustViewport(IEditorPart editorPart, LineRange range,
TextSelection selection) {
ITextViewer viewer = EditorAPI.getViewer(editorPart);
if (viewer == null) +1
return; +1
IDocument document = viewer.getDocument();
LineRange viewportOfViewer = EditorAPI.getViewport(viewer);
if (viewportOfViewer == null || document == null) +1 +1
return; +1
int lines = document.getNumberOfLines();
int rangeTop = 0;
int rangeBottom = 0;
int selectionTop = 0;
int selectionBottom = 0;
if (selection != null) { +1
try {
selectionTop = document.getLineOfOffset(selection.getOffset());
selectionBottom = document.getLineOfOffset(selection
.getOffset() + selection.getLength());
} catch (BadLocationException e) { +1
// should never be reached
LOG.error("Invalid line selection: offset: "
+ selection.getOffset() + ", length: "
+ selection.getLength());
selection = null;
}
}
if (range != null) { +1
if (range.getStartLine() == -1) { +1
range = null;
} else {
rangeTop = Math.min(lines - 1, range.getStartLine());
rangeBottom = Math.min(lines - 1,
rangeTop + range.getNumberOfLines());
}
}
if (range == null && selection == null) +1 +1
return; +1
// top line of the new viewport
int topPosition;
int localLines = viewportOfViewer.getNumberOfLines();
int remoteLines = rangeBottom - rangeTop;
int sizeDiff = remoteLines - localLines;
// initializations finished
if (range == null || selection == null) { +1 +1
topPosition = (rangeTop + rangeBottom + selectionTop + selectionBottom) / 2;
viewer.setTopIndex(topPosition);
return; +1
}
/*
* usually the viewport of the follower and the viewport of the followed
* user will have the same center (this calculation). Exceptions may be
* made below.
*/
int center = (rangeTop + rangeBottom) / 2;
topPosition = center - localLines / 2;
if (sizeDiff <= 0) { +1
// no further examination necessary when the local viewport is the
// larger one
viewer.setTopIndex(Math.max(0, Math.min(topPosition, lines)));
return; +1
}
boolean selectionTopInvisible = (selectionTop < rangeTop + sizeDiff / 2);
boolean selectionBottomInvisible = (selectionBottom > rangeBottom
- sizeDiff / 2 - 1);
if (rangeTop == 0 +1
&& !(selectionTop <= rangeBottom && selectionTop > rangeBottom +1 +1
- sizeDiff)) {
// scrolled to the top and no selection at the bottom of range
topPosition = 0;
} else if (rangeBottom == lines - 1 +1
&& !(selectionBottom >= rangeTop && selectionBottom < rangeTop +1 +1
+ sizeDiff)) {
// scrolled to the bottom and no selection at the top of range
topPosition = lines - localLines;
} else if (selectionTopInvisible && selectionBottom >= rangeTop) { +1 +1
// making selection at top of range visible
topPosition = Math.max(rangeTop, selectionTop);
} else if (selectionBottomInvisible && selectionTop <= rangeBottom) { +1 +1
// making selection at bottom of range visible
topPosition = Math.min(rangeBottom, selectionBottom) - localLines
+ 1;
}
viewer.setTopIndex(Math.max(0, Math.min(topPosition, lines)));
}
编辑:我已将复杂性降低到 11。我应该如何进一步降低它?
private int setTopPositionUtil(int sizeDiff, int rangeTop, int rangeBottom, int selectionTop, int selectionBottom) {
boolean selectionTopInvisible = (selectionTop < rangeTop + sizeDiff / 2);
boolean selectionBottomInvisible = (selectionBottom > rangeBottom - sizeDiff / 2 - 1);
if (rangeTop == 0 && !(selectionTop <= rangeBottom && selectionTop > rangeBottom - sizeDiff)) { // +1 +1 +1
// scrolled to the top and no selection at the bottom of range
topPosition = 0;
} else if (rangeBottom == lines - 1 && !(selectionBottom >= rangeTop && selectionBottom < rangeTop + sizeDiff)) { // +1 +1 +1
// scrolled to the bottom and no selection at the top of range
topPosition = lines - localLines;
} else if (selectionTopInvisible && selectionBottom >= rangeTop) { // +1 +1
// making selection at top of range visible
topPosition = Math.max(rangeTop, selectionTop);
} else if (selectionBottomInvisible && selectionTop <= rangeBottom) { // +1 +1
// making selection at bottom of range visible
topPosition = Math.min(rangeBottom, selectionBottom) - localLines + 1;
}
return topPosition;
}
private int setTopPosition(int localLines, int rangeTop, int rangeBottom,int selectionTop, int selectionBottom) {
// top line of the new viewport
int topPosition;
int remoteLines = rangeBottom - rangeTop;
int sizeDiff = remoteLines - localLines;
// initializations finished
/*
* usually the viewport of the follower and the viewport of the followed
* user will have the same center (this calculation). Exceptions may be
* made below.
*/
int center = (rangeTop + rangeBottom) / 2;
topPosition = center - localLines / 2;
if (sizeDiff > 0) { // +1
setTopPositionUtil(sizeDiff, rangeTop, rangeBottom, selectionTop, selec);
}
return Math.max(0, Math.min(topPosition, lines)); // +1
}
private void adjustViewport(IEditorPart editorPart, LineRange range,TextSelection selection) {
ITextViewer viewer = EditorAPI.getViewer(editorPart);
if (viewer != null) { // +1
IDocument document = viewer.getDocument();
LineRange viewportOfViewer = EditorAPI.getViewport(viewer);
if (viewportOfViewer != null && document != null) { // +1 +1
int lines = document.getNumberOfLines();
int rangeTop = 0;
int rangeBottom = 0;
int selectionTop = 0;
int selectionBottom = 0;
if (selection != null) { // +1
try {
selectionTop = document.getLineOfOffset(selection.getOffset());
selectionBottom = document.getLineOfOffset(selection
.getOffset() + selection.getLength());
} catch (BadLocationException e) { // +1
// should never be reached
LOG.error("Invalid line selection: offset: " +
selection.getOffset() + ", length: " +
selection.getLength());
selection = null;
}
}
if (range != null) { // +1
if (range.getStartLine() == -1) { // +1
range = null;
} else {
rangeTop = Math.min(lines - 1, range.getStartLine());
rangeBottom = Math.min(lines - 1,
rangeTop + range.getNumberOfLines());
}
}
if (range != null && selection != null) { // +1 +1
viewer.setTopIndex(setTopPosition(viewportOfViewer.getNumberOfLines(),
rangeTop, rangeBottom, selectionTop, selectionBottom));
} else {
viewer.setTopIndex((rangeTop + rangeBottom + selectionTop + selectionBottom) / 2);
}
}
}
}
注意:代码现在由 3 个方法组成。第二个和第三个的复杂度低于 10,但是 setTopPositionUtil 的复杂度仍然是 11。有什么帮助吗?
抱歉缩进。
最佳答案
您的问题非常广泛,但为了让您继续下去,您应该执行以下操作:
换句话说:您编写了如此多的测试用例,以至于您完全确信您对生产代码中的每个和任何方面都有测试。
然后你开始重构。然后你继续运行你的测试套件;以确保您不会破坏任何东西。并且您继续运行复杂性度量工具;确保您朝着正确的方向前进。
当问题是:我该如何重构时,那就转向Refactoring这样的经典作者:Fowler 或 Clean Code作者:马丁。
总的来说,我认为您也有点关注错误的主题:您的主要目标应该是创建可读代码很容易遵循。我的建议是将这个巨大的方法进一步分割成一些较小的方法。
关于java - 如何通过重构代码来降低环复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218530/
是否有任何特定于 CoffeeScript 的技巧可以使这看起来更整洁: index = (-> if segment == 'index' return
我正在试验 C# 的不同领域并重构最佳实践/模式。 可以看出,下面的 Validate 方法有 3 个子验证方法。 有没有办法重新设计/重构此方法,以便删除 if 语句? (可能使用委托(delega
我正在制作一个简单的 Rails 站点,它将存储一些日期并执行基本的条件检查。我在下面写了一些方法,并被告知我可以使它们更有效率。我一直挠头,我不知道该怎么做。我应该让 entry.find 全局化吗
有没有更好的方法来编写这个函数?我继承了一些 javascript 代码,如果可能的话,我想让它更简洁。此外,我可能会添加更多“主题”元素,并且不想一遍又一遍地复制和粘贴。 function imag
1. 效果展示 在线查看 2. 开始前说明 效果实现参考源码: Logo 聚集与散开 原效果代码基于 react jsx 类组件实现。依赖旧,代码冗余。
我似乎缺乏足够的咖啡来让我清楚地看到以下问题。 假设我有一个包含两个构造函数和多个字段的类。一个构造函数是无参数构造函数,一个字段依赖于另一个字段。另一个构造函数为其其中一个字段获取注入(inject
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我有一个枚举,里面有一些状态: enum State { A, B, C, D } 以及具有相应状态的对象: class MyObject { State st
我的 build.xml 中有这段代码:
在Delphi XE中,我经常使用重命名变量重构(Ctrl+Shift+E),通过给出更有意义的变量名称来使我的代码更容易理解,例如: 这一切都很好,但是当我使用它时,我在工作空间方面遇到了一个小问题
我实现了一个逻辑来通过data变量计算剩余数量和成本。它循环遍历每个产品,并通过计算已返回数量状态的数量来计算剩余数量,并减去产品数量。 有没有办法重构这段代码,使其看起来更干净、易于理解/可维护?我
我正在学习 Haskell,所以这可能是一些非常微不足道的事情,但我希望得到一些关于如何重写它以及它如何工作的指示。 我有以下工作代码(使用的包: HTF 、 Parsec 和 Flow ): {-#
我有以下代码: switch(equipmentAttachment.AttachmentPosition) { case 'AttachFront': { if(
我正在尝试将代码从 Java Utility Logging 更改为 Log4J2。要更改代码,我想在 Eclipse 中使用代码重构。例如更改:导入 java.util.logging.Logger
我有一个处理 Excel 文件中的行的函数。在这个函数中,我有一个 for 循环。现在,一旦提取一行,我们就会检查各种条件。如果任何条件为假,我们继续下一步row.可以使用模式使这段代码更加结构化吗?
我正在重构一个有很多嵌套调用的程序,例如 ServiceManagement.getGlobalizationService() .createExportCo
我在 JTabbedPane 上重构了许多字段以减少冗余。但是,当我为字段数量设置常量大小时,出现空指针异常。我不太确定为什么会发生这种情况。我做错了什么,更重要的是有人可以解释发生了什么事吗? pu
我试图通过删除 map.setOnPolygonClickListener 和 map.setOnMarkerClickListener 中的重复项来重构以下方法。 两个监听器执行完全相同的操作,我想
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
当我在这张照片中重构 Storyboard时 link . 我找不到在哪里可以交换标签栏项目的位置。 例如,我想将主菜单更改为索引 0。 这是我的storyboard . 最佳答案 您可以通过拖放标签
我是一名优秀的程序员,十分优秀!