命名联合或提升::变体类型定义

Naming union or boost::variant typedefs

本文关键字:类型 定义      更新时间:2023-10-16

我在一个变体中保存数据库值。在您看来,以下哪种类型定义最有意义:

typedef boost::variant<int, bool, std::string> Value;
typedef boost::variant<int, bool, std::string> Values;
typedef boost::variant<int, bool, std::string> Type;
typedef boost::variant<int, bool, std::string> Types;

它们都没有意义。值或类型是什么意思?只是抽象的名字。最好用更具体的名字来命名它们,例如ObjectState或PacketField。像Type这样的名称没有任何意义,当你命名你的变量时,认为其他人可以阅读和理解它的含义,当你忘记你做了什么时,你必须记住它是什么,不要阅读一堆代码来理解这种类型的用途。

有关变量命名和软件工程的更多信息,您可以购买由Steve McConnell撰写的精彩书籍Code Complete。