布尔变量与更具体的变量

发布于 2024-11-15 06:29:20 字数 572 浏览 0 评论 0 原文

在尝试创建更好、更一致的约定时,我希望获得有关以下选项的反馈。我使用的场景涉及记录一件物品是运送到现有地址还是新地址。

这两种设置都能说明问题,但我没有考虑到它们的额外优点和缺点,或者哪种约定更好?

    field name: ship_to
       option 1: new_address
       option 2: existing_address

Pro: 
- Allows for new options down the road if needed.
- Easier to grasp what's going on when looking just a the database

Cons: 
- Not easier to grasp in the code - have to remember the options



    field name: ship_to_new_address
       option 1: true
       option 2: false

Pros / Cons - Pretty much the opposite of what I listed above.

In trying to create better, more consistent conventions I wanted to get feedback on the following options. The scenarios I'm using involved recording whether an item is shipped to an existing address or a new one.

Both of these setups would get the point across, but are their additional pros and cons I'm not thinking of, or conventions on which is better?

    field name: ship_to
       option 1: new_address
       option 2: existing_address

Pro: 
- Allows for new options down the road if needed.
- Easier to grasp what's going on when looking just a the database

Cons: 
- Not easier to grasp in the code - have to remember the options



    field name: ship_to_new_address
       option 1: true
       option 2: false

Pros / Cons - Pretty much the opposite of what I listed above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-11-22 06:29:20

答案是否定的。

您提到的数据库我假设是指关系数据库管理器。您的设计不遵循一零无穷规则,需要修改以支持另一艘船-在尽可能方便的时间(例如当顾客尖叫时)解决问题。

正确的数据结构是:

  1. 客户有无限个订单
  2. 订单有送货地址
  3. 有无数个送货地址

其中客户、订单和送货地址都是单独的数据库表。这被称为第三范式,并且至少在 1970 年之前就已经成为标准做法。

如果您需要要注意订单具有非标准送货地址,请在订单中记录该布尔值。

The answer is neither.

You mentioned database which I will assume means Relational Data Base Manager. Your design does not follow the one-zero-infinity rule and will require modification to support another ship-to address at the least possible convenient time (like when the customer is screaming).

A proper data structure is:

  1. a Customer has infinity orders
  2. an Order has a Ship-To address
  3. there are an infinite number of Ship-To addresses

Where Customer, Order, and Ship-To are all separate database tables. This is known as Third Normal Form and has been standard practice since at least before 1970.

If you need to note that an order has a non-standard Ship-To address, record that boolean in the Order.

沫离伤花 2024-11-22 06:29:20

业务需求是什么?您是否试图表明送货地址已更改?您是否想区分新地址或现有地址?

那么,你为什么关心呢?您必须指出以便将地址保存到数据库(如果是新的)吗?您需要报告有多少人运送到不存在的地址?

最后,你最终能有两个以上的选择(新的、现有的)吗?

问题的答案将指明正确的方向。如果只有两个选择并且我认为没有必要扩展,我个人偏好使用布尔值。但是,我经常处理外部 API,因此与仅内部选项相比,更改需要更多的思考。枚举(通常将作为“类型表”存储在数据库中)相当高效、性能良好,并且存储量不太大,因此它不是一个坏选择。

What is the business need? Are you trying to indicate the shipping address has changed? Are you trying to differentiate between a new address or existing address?

Then, why do you care? You have to indicate so you save the address to a database if new? You need reporting on how many people shipped to a non-existing address?

Finally, can you ever end up with more than 2 options (new, existing)?

The answers to the questions will indicate the proper direction. My personal preference is to use Boolean if there are only two choices and I see no need to ever expand. But, I deal a lot with external APIs, so change requires a lot more thought than an option that is internal only. An enumeration (which generally will be stored as a "type table" in a database) is fairly efficient, perf wise, and not too heavy in storage, so it is not a bad option.

翻身的咸鱼 2024-11-22 06:29:20

还有第三种选择:

  • 将地址放在自己的表中(可能有一个所有者列链接回您的用户/帐户表)。
  • 使用通过外键指向地址表的 ship_to 列。

优点:

  • 每个人可以根据需要拥有任意多个地址(家庭、办公室、别墅、朋友……)。
  • 很好地标准化。

我能想到的唯一缺点是您必须进行更多连接,但连接并不是一件坏事。您可能需要进行更多的健全性检查,以确保所有所有权都很好地排列在一起,但这也没什么大不了的。

There is a third option:

  • Put addresses in a table of their own (probably with an owner column linking back to your user/account table).
  • Use a ship_to column that points to the address table with a foreign key.

Pros:

  • Each person can have as many addresses as they need (home, office, cottage, friends, ...).
  • Nicely normalized.

The only con I can think of is that you'll have to do more joins but joins aren't really a bad thing. You may need a bit more sanity checking to make sure all the ownerships line up nicely but that's not a big deal either.

九命猫 2024-11-22 06:29:20

这完全取决于您想要实现的目标。如果您只想知道该商品是否运送到新地址,那么布尔值就可以了。

我认为您需要考虑如何使您的架构可扩展。就像您在第一个“专业版”中所说的那样,您可能希望有更多选项,例如“temporary_address”。如果您想要更多确定性变量,您可以随时使用枚举。

It all depends on what you're trying to accomplish. If all you want is to know if the item is shipped to a new address or not than the boolean is OK.

I think you need to think how to make your architecture scalable. Like you said in the first "pro", you might want to have more options like "temporary_address". If you want more deterministic variables you can always use an enum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文