数据和代码有什么区别? [英] What's the difference between data and code?

查看:315
本文介绍了数据和代码有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以一个例子,考虑一个超市购物者可以享受的折扣。



我们可以将这些规则定义为某些标准方式的数据(合格项目列表,适用日期,优惠券代码),并编写通用代码来处理这些规则。或者,我们可以将每个代码写入一个代码块,它可以检查客户购物清单的相应内容,并返回任何适用的折扣。



您可以合理地将规则存储为对象,序列化为Blob或存储在代码文件中,以便每个规则可以选择自己在数据和代码之间划分,以允许未来的规则将不符合以上考虑的通用处理器的类型。



通过if语句来检查6种不同的东西,通常很容易批评混合数据的代码这应该在文件或数据库中,但是有没有一个有助于边缘案例的规则?



或者这是面向对象设计的要点,以阻止我们担心数据和代码之间的界限?



要澄清,根本的问题是这样的:你如何编写上面的例子?有没有一个经验法则让你决定什么是数据,什么是代码?



(注意:我知道,代码可以编译,但在一个动态的世界语言和JIT编译,即使这是一个模糊的概念。)

解决方案

从根本上说,数据和代码之间当然没有区别,但是对于真正的软件基础设施,可能会有很大的区别。除了您所提及的汇编之外,除了明显的事情,最大的问题是这样的:



大多数足够大的项目旨在产生一个大捆绑的发布产生在3个月(或更长)的循环中,广泛测试,以后不能改变,除了严格控制的方式。 代码绝对不能改变,所以需要改变的任何东西都必须被考虑在内,并将其作为配置数据,以便改变它,使其成为可以确保发布工作的人。



当然,在大多数情况下,糟糕的配置数据可能会破坏一个发布,就像坏代码一样彻底,所以整个事情在很大程度上是一个错觉 - 在现实中,无论是代码或配置数据发生变化,重要的是主系统与部件之间的接口变化范围狭窄,界定明确,足以让您有机会让变更人了解他所做的所有后果。



这已经比大多数人认为的更困难,当它真的只是几个字符串和配置的数字(我亲自目睹了一个生产大型机系统崩溃,因为它有一个布尔值的设置与另一个系统所说的不同)。当您的配置数据包含复杂的逻辑时,几乎不可能实现。但是,由于您使用严格设计的特别规则配置语言而不是真实代码,所以情况不会更好。


To take an example, consider a set of discounts available to a supermarket shopper.

We could define these rules as data in some standard fashion (lists of qualifying items, applicable dates, coupon codes) and write generic code to handle these. Or, we could write each as a chunk of code, which checks for the appropriate things given the customer's shopping list and returns any applicable discounts.

You could reasonably store the rules as objects, serialised into Blobs or stored in code files, so that each rule could choose its own division between data and code, to allow for future rules that wouldn't fit the type of generic processor considered above.

It's often easy to criticise code that mixes data in, via if statements that check for 6 different things that should be in a file or a database, but is there a rule that helps in the edge cases?

Or is this the point of Object Oriented design, to stop us worrying about the line between data and code?

To clarify, the underlying question is this: How would you code the above example? Is there a rule of thumb that made you decide what is data and what is code?

(Note: I know, code can be compiled, but in a world of dynamic languages and JIT compilation, even that is a blurry concept.)

解决方案

Fundamentally, there is of course no difference between data and code, but for real software infrastructures, there can be a big difference. Apart from obvious things like, as you mentioned, compilation, the biggest issue is this:

Most sufficiently large projects are designed to produce "releases" that are one big bundle, produced in 3-month (or longer) cycles, tested extensively and cannot be changed afterwards except in tightly controlled ways. "Code" most definitely cannot be changed, so anything that does need to be changed has to be factored out and made "configuration data" so that changing it becomes palatable those whose job it is to ensure that a release works.

Of course, in most cases bad configuration data can break a release just as thoroughly as bad code, so the whole thing is largely an illusion - in reality it doesn't matter whether it's code or "configuration data" that changes, what matters is that the interface between the main system and the parts that change is narrow and well-defined enough to give you a good chance that the person who does the change understands all consequences of what he's doing.

This is already harder than most people think when it's really just a few strings and numbers that are configured (I've personally witnessed a production mainframe system crash because it had one boolean value set differently than another system it was talking to). When your "configuration data" contains complex logic, it's almost impossible to achieve. But the situation isn't going to be any better ust because you use a badly-designed ad hoc "rules configuration" language instead of "real" code.

这篇关于数据和代码有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆