gpt4 book ai didi

c++ - 缓解 "type-validate then use"代码重复模式

转载 作者:行者123 更新时间:2023-11-28 01:04:40 25 4
gpt4 key购买 nike

系统的一个模块处理基于 JSON 的协议(protocol),该协议(protocol)用于传输各种数据。这导致了将近一百个小代码段,如下所示:

 /*
* Data package Foo reports Fooness level
*/
if(root.isMember("foo") && root["foo"].isInt())
{
int foo = root["foo"].asInt();
// do things with foo
}


/*
* Data package Bar gives ID number and name of a newly arrived bar.
*/
if(root.isMember("bar") && root["bar"].isObject())
{
JSON::Value bar = root["bar"];

if(bar.isMember("baz") && bar["baz"].isString()
&& bar.isMember("buzz") && bar["buzz"].isInt())
{
std::string baz = bar["baz"].asString();
int buzz = bar["buzz"].asInt();
// do things with baz and buzz

}
else{ err["bar"] = argument_error; }
}

不仅每个 block 的“肉”经常是一两行大约 10 行参数验证,这导致无数的复制粘贴错误和可维护性问题(一个键更改名称,它必须在大约 6地点)。

您将如何 reshape 这些模式以在没有所有代码重复的情况下完成它们的工作?(注意,所有主键和少数子键都是可选的,大多数子键是强制性的。)

最佳答案

你没有列出像 bar 这样的东西的类型,所以我只是为那个类型使用 Bar

您可以给自己一些辅助方法,使用状态代码或异常,具体取决于您预期失败的常见程度。

bool get_string(std::string& result, const Bar& bar, const char* name)
{
if(bar.isMember(name) && bar[name].isString())
{
result = bar[name].asString();
return true;
}
return false;
}

// and similarly
bool get_int(int& result, const Bar& bar, const char* name)
{
if(bar.isMember(name) && bar[name].isInt())
{
result = bar[name].asInt();
return true;
}
return false;
}

然后你可以使用它:

JSON::Value bar;
std::string baz;
int buzz;
if(get_object(bar, root, "bar"))
{
if (get_string(baz, bar, "baz")
&& get_int(buzz, bar, "buzz"))
{
// do things with baz and buzz
}
else{ err["bar"] = argument_error; }
}

它稍微干净一些,但不是突飞猛进。如果您希望您正在寻找的东西存在,并且不太可能失败,那么我们可以使用异常。我们也可以全力以赴使用模板:

// set up template forms to check types
template<typename T> bool is_a(const Bar& b);
template<> bool is_a<std::string>(const Bar& b) { return b.isString(); }
template<> bool is_a<int> (const Bar& b) { return b.isInt(); }
template<> bool is_a<JSON::Value>(const Bar& b) { return b.isObject(); }

// templates to extract as a type
template<typename T> T as_type(const Bar& b);
template<> std::string as_type<std::string>(const Bar& b) { return b.asString(); }
template<> int as_type<int> (const Bar& b) { return b.asInt(); }
template<> JSON::Value as_type<JSON::Value>(const Bar& b) { return b.asObject(); }

// the one extraction method
template<typename T>
T get(const Bar& bar, const char* name)
{
if ( ! bar.isMember(name)) throw std::runtime_error("not a member");
if ( ! is_a<T>(bar[name])) throw std::runtime_error("wrong type");
return as_type<T>(bar[name]);
}

// and now we use it
try
{
JSON::Value bar = get<JSON::Value>(root, "bar");
std::string baz = get<std::string>(bar, "baz");
int buzz = get<int>(bar, "buzz");
// do things with baz and buzz
}
catch (const std::runtime_error& exc)
{
err["bar"] = argument_error;
}

设置需要更多方法,但现在使用它非常简单干净。

关于c++ - 缓解 "type-validate then use"代码重复模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6904804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com