标签: code-duplication

避免多个 C++ 模板专业化上的代码重复

我四处寻找一个好的解决方案,以避免模板类的每个专业化上的代码重复。

这是一个示例代码:

template<class T>
class C
{
    int foo();
}
Run Code Online (Sandbox Code Playgroud)

现在默认值的定义:

template<class T>
C<T>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)

现在特殊模板的专业化

template<> C<int>::foo()    { ... do a lot of stuff and return n .... }
template<> C<double>::foo() { ... do a lot of stuff and return n .... }
template<> C<int>::foo()    { ... do a lot of stuff and return n .... }
Run Code Online (Sandbox Code Playgroud)

现在我必须复制专门化的代码。但总的来说,它是相同的代码。

我的问题是:避免代码重复的最佳解决方案是什么以及如何隐藏实现?也许通过使用 noname-namespace 或 impl-namespace ?

亲切的问候,彼得

c++ templates code-duplication

5
推荐指数
1
解决办法
1385
查看次数

如何检测多个Python源文件之间的代码重复?

我已将所有 python 项目源放在一个文件夹中。运行以下命令pylint似乎只是在每个源文件中查找和分析重复项,而不是在我想要的所有源文件中查找和分析重复项:

pylint --disable=all --enable=duplicate-code /some/source/folder/
Run Code Online (Sandbox Code Playgroud)

有没有办法在许多源文件中查找重复的代码?

python code-analysis static-analysis pylint code-duplication

5
推荐指数
0
解决办法
1285
查看次数

"开源"是否意味着"任何人都可以把我的工作带到任何地方,无论他们喜欢什么"?

当在"与程序员相关"和"明显回答"之间划清界线时,我通常倾向于后者,但我真的很想知道社区对此有何看法.我有一种感觉,这应该是CW(我从来没有创建过CW主题),但是我会让那些有更多SO经验的人来做这个电话.

摘要:

"开源"是否意味着"任何人都可以把我的工作带到任何地方,无论他们喜欢什么"?

编辑:对于那些要求,许可证是LGPL.

细节:

我的开源标题是在Codeplex上托管的,现在已经有一段时间了.今天,我看到它也在Softpedia上托管,没有明显的参考原作者的实际网站(链接在那里,一层或两层深,但不是应该如此突出).他们复制了所有相关的Codeplex内容并自己托管......就像一面镜子,但没有原作者的知识.

我根本没有被告知/问过/等等."开源"是否意味着"任何人都可以把我的工作带到任何地方,无论他们喜欢什么"?

我对这个项目没有任何经济利益.这只是一个有趣的爱好 - 满足用户需求并帮助我解决我作为开发人员的缺点(理想情况下是协作) - 也许是"回馈".如果我试图从这个项目赚钱,我会更加恼火...如果我从下载网站获得广告收入,例如(当用软件名称搜索时,你现在会发现Softpedia持有在Google的搜索结果中,该项目的Codeplex页面用于保存的位置).

无论如何,我不喜欢其他网站突然主持的气味,没有我的合作或祝福,不仅是可下载的安装程序,还有我的逐字产品描述,截图,更改日志等.

是的,他们已经显示了我的名字,但没有办法联系我.坦率地说,"我的名字"是我对他们在我不知情的情况下使用时烦恼的事情- 我不认识这些人,我也不想以任何方式与他们或他们的网站联系.

我知道了.我在线.我做了些什么.人们将链接到我 - 这在技术上也是如此(如果你能找到链接),但这个网站感觉不对.

同样,如果该网站甚至是产品的最基本的评论/评论,那将会有所不同.但是这让我看起来就像是把自己与他们联系在一起 - 在那里创建一个帐户,或者其他东西 - 而我却没有.

他们将许可证简单地列为"免费软件",与Codeplex网站上随时提供的许可相比,这是模糊的(至少可以说).就像可编辑隐藏的Codeplex链接一样,他们对"免费软件"的定义在其网站的其他地方有更详细的解释,但它并不像Codeplex那样明显.我甚至还没有考虑过他们的"免费软件"定义是否/如何与选择LGPL的"我的"意图不同.

我没有尝试联系这些人,但是我的乐观情绪并不高,因为我开始聘请那些在不涉及他们的情况下复制他人内容的人(再次,没有"提交"内容发生在这里,他们的网站不提供独特的评论/补充我的工作).

我希望有人能提供有用/有用的东西吗?我喜欢这个项目(这是我的第一次开源尝试).我不希望它对我或我的用户来说是一件令人头疼的事吗?

这只是作为一个提供免费软件的开源软件作者的一部分吗?你的工作在其他地方(以及其他地方仍然是 - heh)得到重复,你无能为力吗?你认为这是一个合法的"链接"吗?我是否因为生气而错过了标记?

hosting open-source codeplex code-duplication

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

如何减少此示例中的代码重复

我需要遍历一个数字(xx).xx始终从零开始.我的问题是,如果moveDirection变量是+1,则xx会增加,直到达到正值range.如果moveDirection为-1,则xx减小直到达到负数range.

在下面的代码中,我首先对moveDirection进行了if语句测试,然后我复制了for循环,并编辑了每个case的值.我的代码恰好在ActionScript3中,但语言并不重要.

var p:Point;
var xx:int;

if (moveDirection > 0)
{
    for (xx = 0; xx < range; xx++)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}
else 
{
    for (xx = 0; xx > range; xx--)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做到这一点,也许没有重复for循环?如果有任何其他建议,将不胜感激.

language-agnostic optimization for-loop code-duplication

4
推荐指数
1
解决办法
209
查看次数

当唯一的差异是一个变量的类型时,重构重复的代码?

我必须能够连接到两个不同版本的API(1.4和1.5),我们称之为Foo API.我连接到API并处理结果的代码基本上是重复的 - 唯一的区别是从两个API返回的数据类型.我怎样才能重构这个以消除重复?

在Foo14Connector.cs(我自己的类中调用1.4 API)

public class Foo14Connector
{
    public void GetAllCustomers() 
    {
        var _foo = new Foo14WebReference.FooService();
        Foo14WebReference.customerEntity[] customers = _foo.getCustomerList;
        foreach (Foo14WebReference.customerEntity customer in customers)
        {
            GetSingleCustomer(customer);
        }
    }
    public void GetSingleCustomer(Foo14WebReference.customerEntity customer)
    {
        var id = customer.foo_id;
        // etc 
    }
}
Run Code Online (Sandbox Code Playgroud)

在几乎完全重复的类Foo15Connector.cs(我自己的类调用1.5 API)中

public class Foo15Connector
{
    public void GetAllCustomers() 
    {
        var _foo = new Foo15WebReference.FooService();
        Foo15WebReference.customerEntity[] customers = _foo.getCustomerList;
        foreach (Foo15WebReference.customerEntity customer in customers)
        {
            GetSingleCustomer(customer);
        }
    }
    public void GetSingleCustomer(Foo15WebReference.customerEntity customer)
    {
        var id …
Run Code Online (Sandbox Code Playgroud)

c# api refactoring code-duplication

4
推荐指数
1
解决办法
1887
查看次数

如何避免重载构造函数中的代码重复?

假设我有一个接受输入的构造函数和另一个使用默认值的构造函数.然后,两个构造函数都以完全相同的方式继续处理这些数据.(以下示例.)在这种情况下,我有哪些选择可以避免代码重复?

(我已经阅读了如何减少构造函数重载代码的帖子,最佳答案建议使用关键字"this".在我的情况下,我想我需要使用"this"从第二个调用第一个构造函数输入存储之后的一个.但是这会导致编译错误:"调用this必须是构造函数中的第一个语句".)

示例代码:

public class A {
  public A(String a) {
    //process a
  }
  public A() {
    String a = "defaultString";
    //process a
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我在类构造函数中使用输入对话框调用需要大量的工作(我知道这不是很好的做法).所以我已经改变了代码示例,以引导讨论远离这个:).

java overloading code-duplication

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

如何避免具有许多实例变量的类中的getter/setter

我会尽量保持简短.

我有许多实例变量(30+)的类,因此有许多getter/setter.这些类本身很简单,但由于getter/setter,LOC刚刚爆炸(并且还有太多的代码口号).

所以我删除了属性并将它们存储在地图中,就像这样

public class MyTechnicalToolClassX
{

//...constructor

private Map<String, Object> data;

public Object getAttributeByKey(AttributeKey key)
{
    // ...doStuff, check data associated with key, etc
    // if (predicate == true) return otherData;
    return data.get(key.toString());
}

public void setAttributeByKey(AttributeKey key, Object value) throws IllegalArgumentException
{
    if(key.getType().isInstance(value))
    {
        data.put(key.toString(), value);
    }
    else
    {
        throw IllegalArgumentException("The passed value has the wrong type, we expect: "
        + key.getType().getName());
    }
}

public enum AttributeKey
{
    someKey1("key1", String.class),
    someKey2("key2", Date.class),
    //...
    someKeyN("keyN", SomeType.class);

    private String key; …
Run Code Online (Sandbox Code Playgroud)

java getter setter code-duplication

4
推荐指数
1
解决办法
2565
查看次数

多次调用相同的函数并处理组合结果集

我需要发出几个API请求,然后对组合结果集进行一些处理.在下面的示例中,您可以通过复制相同的请求代码来查看3个请求(到/创建),但我希望能够指定要生成的请求数.例如,我可能希望运行相同的API调用50次.

如何在不重复API调用函数的情况下进行n次调用?

async.parallel([
    function(callback){
        request.post('http://localhost:3000/create')
            .send(conf)
            .end(function (err, res) {
                if (err) {
                    callback(err, null);
                }
                callback(null, res.body.id);
            });
    },
    function(callback){
        request.post('http://localhost:3000/create')
            .send(conf)
            .end(function (err, res) {
                if (err) {
                    callback(err, null);
                }
                callback(null, res.body.id);
            });
    },
    function(callback){
        request.post('http://localhost:3000/api/store/create')
            .send(conf)
            .end(function (err, res) {
                if (err) {
                    callback(err, null);
                }
                callback(null, res.body.id);
            });
    }
],
function(err, results){
    if (err) {
        console.log(err);
    }
 // do stuff with results
});
Run Code Online (Sandbox Code Playgroud)

asynchronous function code-duplication node.js

4
推荐指数
1
解决办法
4817
查看次数

当唯一部分位于循环/ try-catch中时,避免重复代码

我有一个类有两个方法,有很多重复的代码但是唯一的位在整个事件的中间.根据我的研究,我认为我应该做"执行周围方法"模式,但我找不到我可以遵循的资源,因为他们似乎都使用我无法复制的代码.

我有两种方法,apiPost和apiGet,我已粘贴在下面.我已经将这些方法的独特部分包装在一起,注释显示了唯一部分的开始和结束位置:

/**
 * Class that handles authorising the connection and handles posting and getting data
 *
 * @version     %I%, %G%
 * @since       1.0
 */
public class CallHandler {
    private static PropertyLoader props = PropertyLoader.getInstance();
    final static int MAX = props.getPropertyAsInteger(props.MAX_REQUESTS);
    private final Logger log = LoggerFactory.getLogger(CallHandler.class);
    private final static String POST = "POST";
    private final static String GET = "GET";

    /**
     * Makes a POST call to the API URL provided and returns the JSON response as a string …
Run Code Online (Sandbox Code Playgroud)

java code-duplication

4
推荐指数
1
解决办法
91
查看次数

第二组功能仅在一行代码中有所不同

我有两个性能关键的功能,如下所示:

insertExpensive(Holder* holder, Element* element, int index){
    //............ do some complex thing 1 
    holder->ensureRange(index);//a little expensive
    //............ do some complex thing 2
}
insertCheap(Holder* holder, Element* element, int index){
    //............ do some complex thing 1
    //............ do some complex thing 2
}
Run Code Online (Sandbox Code Playgroud)

如何将2个功能组合在一起以提高可维护性?

我糟糕的解决方案

解决方案1.

insertExpensive(Holder* holder, Element* element, int index){
    do1();
    holder->ensureRange(index);//a little expensive
    do2();
}
insertCheap(Holder* holder, Element* element, int index){
    do1();
    do2();
}
Run Code Online (Sandbox Code Playgroud)

这将是丑陋的.如果do2想要一些局部变量,这也是不切实际的do1.

解决方案2.

insert(Holder* holder, Element* element, int index, bool check){ …
Run Code Online (Sandbox Code Playgroud)

c++ maintainability performance function code-duplication

4
推荐指数
1
解决办法
797
查看次数