Irrational Exuberance
  • 金沙手机APP
  • 反对者和反射决定(转发)

    June 7, 2007.
    ruby (4), 写作(33)

    This is a transplant from the original Irrational Exuberance, and was written in mid 2007: nearly two years ago.

    Learning to program in this era is a thorough indoctrination into object orientation (unless you started with PERL, Scheme, or Haskell, but we’re talking about human beings here). The first--和only- 评估范式我在大学教授是OO,很容易看到为什么 - 如果你只有资源来教授一个范例很好 - 那就是被教导的。它擅长创建大型项目所需的抽象层,并且还促进了划分的代码部分,使得它们可以由相对自主的群体实现(如果他们符合API的商定...... HA..Haha。..我应该是一个诗人)。如果我们看看OO趋势,一种语言遍历比任何其他语言更难:Java。不可否认,Java是C ++,垃圾收集,对象方向和平庸之间的四路尝试的产卵,但是有珍贵的少数最近的语言没有对象方向。Python几乎完整的对象面向对象(一些奇怪的实施内容一旦获得低电平),Ruby是完全面向对象的,Smalltalk(OO的Sire)灵感目标c ......它很难找到用于开发大规模项目的任何语言hasn’t chosen OO as its paradigm of choice.

    OO makes intuitive sense

    The single most important asset that I see driving object orientation’s acceptance is that it just makes sense. OO allows us to map the real world onto our programs. Shaking someone’s hand becomes a.shake_hand(b). Opening a file becomes file.open(“x.txt”, ‘r’). Writing to the file becomes file.write(“out.txt”).A Person has a Head and a Heart. A Heart has an Aorta. We are used to thinking in connection hierarchies already: my car has four wheels, and an engine; my engine has 100 horsepower. These connections lay themselves out and scream “this is the right way to implement me.”

    也许有点过于直观?

    对象方向的一个巨大危险之一是随便的缓解,我们将现实世界的行为映射到我们计划的行为。很容易忘记对象方向只是我们用来降低我们解决方案的复杂性的工具。然而,最直观的选择不一定是最有效的。这是在梦幻纸中讨论的想法协作扩散:编程Antiobjects。The paper is very approachable, and a great read (if nothing else it has some pretty pictures of the pac-man game they created using their anti-object design concept).If, as37signalsargue in their excellent bookGetting Real那a program is the result of thousands of small decisions, then we need to make those decisions deliberately, and not subconsciously. The subconscious is a fantastic thing (while I’m flailing wildly with book recommendations, Malcom Gladwell’s眨is another great book, it discusses the powers and limitations of the subconscious mind), but it usually does what seems right--and this is a situation where the most natural fit is not always the best.

    Getting more concrete

    In the编程Antiobjects.他们讨论了实施Pac-Man的比赛,而是使用复杂算法来决定在哪里移动的鬼魂,大多数决策被转移到地板瓷砖。通过这样做,他们创造了一个精致的敌人,聪明地移动,但他们绕过实际上创造任何人工智能代码来指导鬼魂,而是通过最高的扩散分数移动到可访问的广场(含有Pac-Man的广场非常高diffusion score, and then a recursive algorithm is used where squares receive a fraction of surrounding squares’ scores, such that as you get further from Pac-Man the score goes down. There are a few more details in the article, but this the gist of it).Instead of the ghosts making one complex decision, they make two simple decisions: one by the floor tiles, and one by the ghost. Thus they create something advanced using a very simple system (isn’t emergent behavior great?)The past several days I have run into a (superficially) similar situation where I am able to create simpler and shorter code by reconsidering where to store the decision making code.

    Background Explanation

    At this point I haven’t done any real programming with Ruby, but I have been meaning to give it a try (哇,你会尝试一切意义吗?你太棒了!让我嘲笑一个饼干......)。幸运的是,我 - 蒙蔽了一种不合理的繁荣 - 买了务实的程序员镐那which is the (self-proclaimed) definitive guide to Ruby. So I opened the glorious tome and started reading. After a few chapters I realized that I needed to have a real project to work on instead of passively reading through the language specification and contemplating the strategy for my next game of台式塔塔防同样,旧的(和适度羞辱)爱好啊f mine was mudding, as in “Multi-User-Dungeon”ing--as in telnet with some debatably entertaining game-play mixed in (MUD : Game ; Java : Programming Language). I have always wanted to write a mud server from scratch, and this seemed like as good of a project as any to start using Ruby with.If this example is a bit too geeked out for you (and I profusely apologize for it), just remember that it is illustrating a concept, and that the actual purpose of the code is immaterial.

    Who does what?

    这个项目提出的第一个实现问题之一是决定应该存储哪种方法。有些玩家与物品互动,与玩家互动的领域,玩家与课程互动,与物品交互的项目,与项目交互的技能......它是一个混乱的初步思想是组织属于玩家的方法(这是,据我所知,到目前为止,泥浆实现采取的标准方法)。在那个优良的传统之后,玩家将包含所有功能,并且物品只包含关于自己的数据。实施看起来像这样的东西:

    班级Player景点:name那:objectsdefinitialize(name)@name=name@objects=[]@body={“挥ed"=&GT.;零那“躯干”=&GT.;零}结尾defget(物品)@objects<<物品结尾def挥(物品)如果@objects。指数(物品)和物品。能够?(“挥”)@body[“挥ed"]=物品别的raise“Cannot wield #item}"结尾结尾def穿(物品)如果@objects。指数(物品)和物品。能够?(“穿“)穿_location=物品。景点ibutes[“穿location"]@body[穿_location]=物品别的raise“不能穿#{物品}“结尾结尾结尾

    班级物品attr_reader.:name那:属性definitialize(name)@name=name@attributes={“穿“=&GT.;true}结尾def能够?(行动)@attributes[行动]结尾结尾

    班级武器<物品definitialize(name)极好的(name)@attributes[“挥”]=true结尾结尾

    班级盔甲<物品definitialize(name)极好的(name)@attributes[“穿“]=true结尾结尾

    班级CARCHARMOR.<盔甲definitialize(name)极好的(name)@attributes[“穿location"]=“chest"结尾结尾

    班级LegArmor<盔甲definitialize(name)极好的(name)@attributes[“穿location"]=“腿”结尾结尾

    然而,正如我坐在那样的那样,我认为这是一种非常复杂的方法。当然,它完全是直觉的,但它将作为重读的娱乐追逐基础第五次。作为一个奖励,做出改变是一个真正的痛苦,因为行为和数据行为的行为被分开储存了。这次这次反对者物品漂移到我的意识,我决定我会考虑不同的实施。我决定采取行动物品的方法应该由物品本身包含:如果我将所有复杂性转移到物品中,我会看到事情可能会解决问题。以下是使用此方法的实现:

    班级Player景点:name那:inventory那:bodydefinitialize(name)@name=name@存货=[]@body={“挥ed"=>零那“躯干”=>零那“legs"=>;零}结尾结尾

    班级物品attr_reader.:namedefinitialize(name)@name=name结尾defget(actor)actor。inventory<self结尾结尾

    班级武器<物品def挥(actor)actor。body[“挥ed"]=self结尾结尾

    班级CARCHARMOR.<物品def穿(actor)actor。body[“躯干”]=self结尾结尾

    班级LegArmor<物品def穿(actor)actor。body[“legs"]=self结尾结尾

    在前面的实施方面存在各种各样的好处:我们现在正在跟踪是否可以佩戴/挥舞/得到的东西,以及如何在同一位置佩戴/挥杆/得到它。这意味着仅需要查看一个代码部分,而不是较早的实现需要两个部分。它还允许我们利用多态性(Chartarmor和Legarmor以不同于相同名称的方法来响应)。此外,默认项目初始化足以满足所有子类,因为关于对象的所有其他数据都将存储在其内方法(漂亮和摇晃)。不必连连少数超级呼叫让我成为一个愉快的改善(随机问题:你认为如果你有一个足够的深层层次结构,你可以溢出红宝石堆栈?我在想是的。)。I have some concerns about how the second design appears to depend too much upon the implementation of the Player class; the Java programmer in me wants to build a copious API that completely encapsulates the implementation details. My slightly saner half thinks it is cleaner to leave it as it is. If necessary I can alter the implementation by creating a hashmap-like API over whatever datastructure I would replace the @body hashmap with.Although I’m not one to consider lines of code as a metric for quality, the second version is 41 lines to the first version’s 68 (I am, however, apparently one to have their cake and eat it too). In addition to being shorter, it also strikes me as being simpler and more understandable (no flow control, and no need to explicitly raise any exceptions, we’ll simply use Ruby’s reflection capabilities to ask item.respond_to?(“wield”) and have a simple error message we return to the player if the item has no method corresponding to their command).

    Ending 思绪带回家的信息

    After operating in the object orientation paradigm for a while, grouping functionality into classes often becomes more reflexive than intentional, and this is a danger we have to be aware of. The most obvious solution is usually sufficient, but that doesn't mean it is good. Hopefully in the future I will do a better job of remembering that programs are built of dream-stuff, and that I don't have to succumb to reality’s preconceptions.

    Hi folks. I'm将要aka@lethain。
    If you're looking to reach out to me, here are我提供帮助的方式。如果您想从我这里收到电子邮件,请订阅my weekly newsletter。

    I wroteAn Elegant Puzzle。

    As well as,员工工程师。

    受欢迎的

    • 写五,然后合成:良好的工程战略很无聊。
    • Managing technical quality in a codebase.
    • Work on what matters.
    • Your first 90 days as CTO or VP Engineering.
    • How the Digg team was acquihired.

    最近的

    • Mailbag: Should we just call them architects?
    • RSS饲料更换!接下来几天迁移博客。
    • sands金沙直营赌场
    • 工程造成措施。
    • Digital gardening at Exuberant Sketches.

    有关的

    • 一个comp。SCI学位是你所做的(重新发布)
    • Is the JVM the "next assembly"? I hope not. (repost)
    • 善良,保罗格雷厄姆和艾恩兰德(转发)
    • Reflections on Django (repost)
    • 找到我们的编程流(转发)
    © Will Larson 2021 标签 金沙手机APP rss. About