小编Car*_*ter的帖子

将条件扁平化为重构

考虑:

if (something) {
    // Code...
}
Run Code Online (Sandbox Code Playgroud)

安装CodeRush后,建议:

if (!something) {
    return;
}
// Code...
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下这更好吗?当然,没有任何好处.

refactoring coderush

2
推荐指数
2
解决办法
631
查看次数

如何通过检查找到返回类型的参数化类型?

我正在使用反射来获取所有类中的所有方法:

Method[] allMethods = c.getDeclaredMethods();
Run Code Online (Sandbox Code Playgroud)

之后,我正在迭代这些方法

for (Method m: allMethods){
    //I want to find out if the return is is a parameterized type or not
    m.getReturnType();
}
Run Code Online (Sandbox Code Playgroud)

例如:如果我有这样的方法:

public Set<Cat> getCats();
Run Code Online (Sandbox Code Playgroud)

如何使用反射来查找返回类型包含Cat的参数化类型?

java generics reflection

2
推荐指数
1
解决办法
5041
查看次数

如何从模态对话框中获得响应?

我目前有这个:

Builder yesandno = new AlertDialog.Builder(this);           
yesandno.setTitle("QuickResponse");
yesandno.setMessage(message);
yesandno.setPositiveButton("YES", null);
yesandno.setNegativeButton("NO", null);
yesandno.show();
Run Code Online (Sandbox Code Playgroud)

如何通过设置一个事件监听器来捕获用户单击是或否?

java android

2
推荐指数
1
解决办法
1520
查看次数

规格模式单元测试

我们最近采用了用于验证域对象的规范模式,现在想要引入域对象的单元测试以提高代码质量.

我发现的一个问题是如何最好地对下面示例中显示的验证功能进行单元测试.规范命中数据库所以我希望能够模拟它,但因为它是在线实例化我不能这样做.我可以处理接口,但这会增加代码的复杂性,因为我们可能有很多规范,我们最终会有很多接口(记住我们正在引入单元测试,并且不想给任何人找借口来拍摄它下).

鉴于这种情况,我们如何才能最好地解决单元测试域对象中规范模式的问题?

...
public void Validate()
{
    if(DuplicateUsername())
    { throw new ValidationException(); }
}

public bool DuplicateUsername()
{
    var spec = new DuplicateUsernameSpecification();
    return spec.IsSatisfiedBy(this);
}
Run Code Online (Sandbox Code Playgroud)

c# agile unit-testing design-patterns domain-driven-design

2
推荐指数
1
解决办法
1201
查看次数

我应该将返回类型重命名为更通用的类型以便重用它吗?

所以我弄错了.

在最初为API编写签名时,我创建了以下内容:

public JellyBeanResult getJellyBeanReport();
Run Code Online (Sandbox Code Playgroud)

现在,事实证明我想重新使用更具体的JellyBeanResult对象,因为它的功能,但让其他函数返回一个为不同进程命名的类型会让人感到困惑.我可以想到有几种方法可以解决这个问题.我可以将返回类型重命名为更通用的类型:

public GenericResult getJellyBeanReport();
public GenericResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)

但这会破坏使用API​​的任何代码.我可以创建一个新的,更准确的命名类,它扩展了更接近新函数的SpecificResult:

public class ChocolateBarResult extends JellyBeanResult{};

public JellyBeanResult getJellyBeanReport();
public ChocolateBarResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)

但这真的非常难看,如果我想再次使用返回类型,问题仍然存在.如何在不破坏使用它们的代码的情况下清理这些签名以减少它们的混乱?

java oop refactoring

2
推荐指数
1
解决办法
308
查看次数

为什么String类中的"length"属性是公共的?

在Java中,我想知道为什么String类的"length"属性不是私有的?根据封装原则,这不是一个坏习惯吗?为什么没有像"getLength()"这样的方法呢?

PS:对不起我的英语,我还在改进它.

java encapsulation

2
推荐指数
1
解决办法
1837
查看次数

ArrayList没有重复

我在java中使用arraylist并且我需要在10次迭代期间添加整数(整数是从名为arrint的整数数组中随机获得的)而不重复:

for (int i =0; i<10; ++i)
    array.add(integer);
Run Code Online (Sandbox Code Playgroud)

然后在20次迭代期间为相同的整数数组(arrint)添加相同的数组20其他整数,而不重复

for (int i =0; i<10; ++i)
    array.add(integer);
Run Code Online (Sandbox Code Playgroud)

但是在10个第一个整数和20个整数之间允许重复.

谢谢

java collections arraylist

2
推荐指数
2
解决办法
6144
查看次数

是否有类似ReSharper的C++项目工具?

我正在寻找像ReSharper for C++这样的工具.我希望有一个比Visual Assist更灵活的重构工具.它确实非常好,但如果存在像ReSharper for C++这样的工具,我想知道该工具的名称.

c++ refactoring

2
推荐指数
1
解决办法
4177
查看次数

使用TDD和Java开发的开源项目

我正在寻找一个项目:

1)有可用的来源,包括测试.

2)严格使用TDD开发.

3)用Java和JUnit 编写.

有任何想法吗?

java tdd open-source

2
推荐指数
1
解决办法
1484
查看次数

在java中获取组件的坐标

我有一个JLayeredPane,而JLayeredPane内部是一个JInternalFrame.我需要的是JInternalFrame中内容窗格的边界(x位置,y位置,宽度,高度).边界需要与JLayeredPane相关.我的问题是边框,标题栏,我不确定JInternalFrame的其他内容是否正在弄乱我的计算.它只有几个像素.

这是我尝试计算它的方式.此代码位于JInternalFrame中:


Rectangle area = new Rectangle(
                getX() + 2,  //The x coordinate of the JInternalFrame + 2 for the border
                getY() + ((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getHeight(), //The Y position of the JinternalFrame + theheight of the title bar of the JInternalFrame
                getContentPane().getWidth() - 2, //The width of the Jinternals content pane - the border of the frame
                getContentPane().getHeight() //the height of the JinternalFrames content pane
                );

我需要获取内部框架的内容窗格的位置. 替代文字

java swing coordinates

2
推荐指数
1
解决办法
8744
查看次数