标签: refactoring

这些空数组String.Split方法调用有什么区别?

这两行代码之间有什么区别吗?

1) someString.Split(new[] { ';' });

2) someString.Split(';');

我看到我正在编写的代码中的第一个,并且想知道我是否可以安全地将其更改为第二个或者为什么他们选择以第一种方式执行此操作.

谢谢.

c# string refactoring split

0
推荐指数
1
解决办法
183
查看次数

长的javascript if else语句

我有一系列if-else语句.我知道有一种更有效的方法可以做到这一点,但我对javascript不够熟悉.有人可以提供一些指导吗?

$('#webform-component-primary-credential--0').change (function() {
    if ($('#edit-submitted-primary-credential-0').val() == 35) {
      $('#edit-submitted-additional-credentials-0-47').attr('checked', false);
      $("#edit-submitted-additional-credentials-0-47").attr("disabled", "disabled");
    } else {
      $("#edit-submitted-additional-credentials-0-47").removeAttr("disabled");
    }
    if ($('#edit-submitted-primary-credential-0').val() == 41) {
      $('#edit-submitted-additional-credentials-0-53').attr('checked', false);
      $("#edit-submitted-additional-credentials-0-53").attr("disabled", "disabled");
    } else {
      $("#edit-submitted-additional-credentials-0-53").removeAttr("disabled");
    }
    if ($('#edit-submitted-primary-credential-0').val() == 13) {
      $('#edit-submitted-additional-credentials-0-29').attr('checked', false);
      $("#edit-submitted-additional-credentials-0-29").attr("disabled", "disabled");
    } else {
      $("#edit-submitted-additional-credentials-0-29").removeAttr("disabled");
    }   

});
Run Code Online (Sandbox Code Playgroud)

本系列中还有大约12个if-else语句,但这给出了一般的想法.

javascript jquery refactoring if-statement

0
推荐指数
1
解决办法
596
查看次数

重构简单的功能

我需要以人类可读的格式显示当前的记录状态.在我的数据库中,我有一个这个字段的int,所以记录可能有状态1,2,3,4等

我写了一些代码来向用户显示当前状态:

<?php 
// code to retrieve current status from DB
// ...


if ($status == '1') {
    echo "Current status: Active";
}
if ($status == '2') {
    echo "Current status: Pending";
}
    if ($status == '3') {
    echo "Current status: Inactive";
}

// etc..

?>
Run Code Online (Sandbox Code Playgroud)

这段代码看起来非常难看,但我无法弄清楚如何重构它以使其更有效并且没有无限的一系列条件.

php refactoring

0
推荐指数
1
解决办法
103
查看次数

如何重构嵌套的case语句(在这种情况下)?

我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3.我有以下case声明:

case
when private?
  case
  when not_active? then [:a, :b, :c, :d]
  when active?     then raise "private cannot be active"
  else raise "not recognized"
  end
when shared?
  case
  when not_active? then [:a, :b, :c]
  when active?     then raise "shared cannot be active"
  else raise "not recognized"
  end
when public?
  case
  when not_active? then [:a, :b]
  when active?     then [:a]
  else raise "not recognized"
  end
else raise "not recognized"
end
Run Code Online (Sandbox Code Playgroud)

如何重构上面的代码?

ruby refactoring ruby-on-rails switch-statement

0
推荐指数
1
解决办法
2481
查看次数

使用NSArray的"for x in"语法

我有这样的指示:

[self someMethod:CGPointMake(50, 50)];
[self someMethod:CGPointMake(270, 50)];
[self someMethod:CGPointMake(50, 360)];
[self someMethod:CGPointMake(270, 360)];
...
Run Code Online (Sandbox Code Playgroud)

我想使用NSArray重构代码,如下所示:

NSArray items = [NSArray initWithObjects:
                  CGPointMake(50, 50),
                  CGPointMake(270, 50),
                  CGPointMake(50, 360),
                  CGPointMake(270, 360),
                  ...
                  nil];
Run Code Online (Sandbox Code Playgroud)

我不知道正确的语法,有人可以帮助我吗?我试过这个,但XCode告诉我"Selector元素类型CGPoint不是一个有效的对象":

CGPoint point = [CGPoint alloc];

for (point in items) {
    [self someMethod:point];
}
Run Code Online (Sandbox Code Playgroud)

refactoring objective-c nsarray

0
推荐指数
1
解决办法
1227
查看次数

是否可以在不访问项目文件的情况下重构ASP.NET应用程序?

场景:我可以访问在远程Web服务器上运行的已发布的ASP.NET应用程序.目录结构如下所示:

在此输入图像描述

据我所知,我无法访问项目/解决方案文件.具体来说,我找不到需要修改的特定C#代码隐藏文件.

是否可以仅使用已发布的代码轻松地重建此项目?

c# asp.net refactoring code-behind

0
推荐指数
1
解决办法
349
查看次数

我的重构不正确和明确吗?

这是原始源代码,其中包含一个非常常见的代码模式,我认为它是反模式或最多不必要的冗长:

private bool SymbolDevice;
. . .
    if((oemInfo.IndexOf("SYMBOL") > -1) || (oemInfo.IndexOf("MOTOROLA") > -1))
                            SymbolDevice = true;
                        else
                        {
                            SymbolDevice = false;
                        }
Run Code Online (Sandbox Code Playgroud)

我会这样重构它:

SymbolDevice = ((oemInfo.IndexOf("SYMBOL") > -1) || (oemInfo.IndexOf("MOTOROLA") > -1));
Run Code Online (Sandbox Code Playgroud)

Resharper(版本2.0,Visual Studio 2003/.NET 1.1的最新版本,这个项目是这样的)以这种方式重构它:

SymbolDevice = (oemInfo.IndexOf("SYMBOL") > -1) || (oemInfo.IndexOf("MOTOROLA") > -1) ? true : false;
Run Code Online (Sandbox Code Playgroud)

我同意Resharper的重构改进了遗留代码,但是我有什么理由选择它而不是我的版本?

c# resharper refactoring

0
推荐指数
1
解决办法
98
查看次数

重构 - 查看是否存在记录然后获取记录

我试图避免两次调用数据库.我需要检查记录是否存在,如果是,那么用数据填充我的视图.我有以下代码:

        if (Presenters.PayeePresenter.GetByID(id) != null)
        {
            view = BLL.Presenters.PayeePresenter.GetByID(id);

            msg.Success = true;
            msg.Text = "Record Found";
        }
Run Code Online (Sandbox Code Playgroud)

我怎样才能对数据库进行最少量的调用?

c# refactoring database-connection

0
推荐指数
1
解决办法
67
查看次数

我可以在Java类中减少这种重复出现的模式吗?

我有以下界面:

public interface Gravy {

     public List<Giblet> getGiblets();
     public Giblet getGiblet(String id);
     public int getNumGiblets();
     public void addGiblet();
     public void removeGiblet(Giblet giblet);

     public List<Carrot> getCarrots();
     public Carrot getCarrot(String id);
     public int getNumCarrots();
     public void addCarrot();
     public void removeCarrot(Carrot carrot);

     public List<Gravy> getGravies();
     public Gravy getGravy(String id);
     public int getNumGravies();
     public void addGravy();
     public void removeGravy(Gravy gravy);

}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我的作品中有一个重复出现的模式Gravy.甲Gravy对象可以包含内脏,胡萝卜,和其他(较小)肉汁.所有这些都可以添加,删除或查询.

有两点需要注意:

  1. Carrots和Giblets彼此有点共同之处,但两者都与Gravys 有很大不同.

  2. 我可能需要稍后添加更多项目(因此需要重构)...

是否可以减少上面的代码,以便"动词"只写一次?

java refactoring design-patterns interface

0
推荐指数
1
解决办法
61
查看次数

减少c ++中的代码重复

我可以将以下代码减少为一个功能吗?他们中的大多数都是一样的.谢谢

void info(StreamLog &streamLog)
{
    streamLog.ss << "info:";
    streamLog.mFilter->setLogLevel("info");
}
void debug(StreamLog &streamLog)
{
    streamLog.ss << "debug:";
    streamLog.mFilter->setLogLevel("debug");
}
void warning(StreamLog &streamLog)
{
    streamLog.ss << "warning:";
    streamLog.mFilter->setLogLevel("warning");
}
void error(StreamLog &streamLog)
{
    streamLog.ss << "error:";
    streamLog.mFilter->setLogLevel("error");
}
void critical(StreamLog &streamLog)
{
    streamLog.ss << "critical:";
    streamLog.mFilter->setLogLevel("critical");
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请告诉我

第1编辑:抱歉!我没有清楚地解释我的情况.我使用那些函数作为操纵器.因此,我能做到

clog << info << ...

clog << warning << ...

我不想用

clog << log(info)<< ...

有更好的方法吗?谢谢

c++ refactoring

0
推荐指数
1
解决办法
177
查看次数