简体   繁体   English

如何优化/重构此类代码?

[英]How to optimize/refactor such code?

In the current project, there are lots of GetData() method, which get different kind of data from hand written database at run time, and set them in different fields in a class. 在当前项目中,有很多GetData()方法,它们在运行时从手写数据库中获取不同种类的数据,并将它们设置在类的不同字段中。 The projects then have such methods. 这样,项目就有了这样的方法。

void GetData(Datatype type, int& value);
void GetData(Datatype type, double& value);
void GetData(Datatype type, long& value);
void GetData(Datatype type, longlong& value);
....

There are lots of data types, so, these method are often called with a switch with lots of branches. 数据类型很多,因此,这些方法通常在具有许多分支的开关中调用。

void GetData(Datatype type, int& value)
{
     switch(type)
     {
       Type1:
        value = GetDataFromDB1(TYPE1);
        Type2:
              value = .. //get from different source
        ...

     }   

} }

void GetData(Datatype type, double& value)
....

As you see, the the GetData()s are classified according to the second param. 如您所见,GetData()是根据第二个参数分类的。 And within each single GetData(), there are lots of branches. 在每个单独的GetData()中,都有很多分支。 Is this a reasonable method to get data? 这是获取数据的合理方法吗?

To answer "best way to refactor this" would take more context. 回答“重构此问题的最佳方法”将需要更多上下文。 For example maybe it's appropriate to change how the data is stored in addition to how it's fetched like you are showing. 例如,除了像您显示的那样获取数据之外,也许还应该更改数据的存储方式。 I doubt that this code structure needs optimization though. 我怀疑此代码结构是否需要优化。

Sorry, Java programmer coming:) 抱歉,Java程序员来了:)
I have 2 solutions here: 我在这里有2个解决方案:

  1. If you want change code not much, you could put Datatypes and their relevant operations into a Map . 如果您不希望更改代码太多,可以将Datatypes及其相关的操作放入Map中 So long switch(type) statement could be one statement map.get(type) . 因此,长switch(type)语句可能是一个语句map.get(type)

  2. If more decent way, use Polymorphism . 如果更体面的方式,请使用多态 You could define interface for DBOperations, each specific DBOperation could be defined in its implemented class. 您可以为DBOperation定义接口,每个特定的DBOperation都可以在其实现的类中定义。 So GetData just needs for invoke DBOperation interface. 因此, GetData仅需要调用DBOperation接口。 It's rough idea. 这是一个粗略的主意。

It looks like you're outgrowing a custom SQL interface. 似乎您已不再使用自定义SQL接口。 Look into existing C++ SQL libraries. 查看现有的C ++ SQL库。 Unfortunately I can't recommend any (last time I needed one, I wrote a new one at foolish great expense), but see the related question C++ SQL database library comparison . 不幸的是,我什么都不推荐(上一次我需要一个,我花了大笔费用写了一个新的),但是看到了相关的问题C ++ SQL数据库库比较

Also, libpqxx , the official C++ PostgreSQL client, looks very nice. 另外,官方的C ++ PostgreSQL客户端libpqxx看起来非常不错。

Hmm, looking around, I can't seem to find a library with generic support for struct s like what I wrote. 嗯,环顾四周,我似乎找不到像我编写的那样对struct具有通用支持的库。 There must be some out there… you should be able to plug in your tables and headers (with a little modification, maybe) and get automatic interface code. 那里一定有一些东西……您应该能够插入表和标题(可能会稍作修改)并获得自动接口代码。

You might want to refactor it to make it more maintainable. 您可能需要对其进行重构,以使其更易于维护。

Refactoring it for optimization will likely have no benefit, or negative, unless you've shown that it is a performance problem, by profiling. 对其进行重构以进行优化可能不会有任何好处,也不会带来负面影响,除非您通过性能分析证明它是性能问题。

Not sure, if I understood it right, but this might help 不确定,如果我理解正确,但这可能会有所帮助

typedef int Datatype;

template <Datatype d, class T> struct Wrap{
    Wrap(T (*p)(Datatype)) : mp(p){}
    template<class T> void GetData(T &value){
        value = (*mp)(d);
    };
    T (*mp)(Datatype);
};

int GetDataFromDB(Datatype){
    return 0;
}

int main(){
    Wrap<0, int> wi0(GetDataFromDB);
    int val;
    wi0.GetData(val);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM