`
softstone
  • 浏览: 472802 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Why Ruby is Simple

阅读更多

下面这段话原文摘录自《The Ruby Way》一书,大意是“为什么说Ruby是一种简单的语言”。摘录它的原因有二:(1)myan刚才走到我旁边说:“我感觉Ruby还是一个相当复杂的语言,它的语法并不像C那么简练。”(2)这段话引用了大量的名人名言,算得上技术传播的一个好范本。

So one of Ruby's virtues is simplicity. Shall I quote other thinkers on the subject? According to Antoine de St. Exupéry, "Perfection is achieved, not when there is nothing left to add, but when there is nothing left to take away."

But Ruby is a complex language. How can I say that it is simple?

If we understood the universe better, we might find a "law of conservation of complexity"a fact of reality that disturbs our lives like entropy so that we cannot avoid it but can only redistribute it.

And that is the key. We can't avoid complexity, but we can push it around. We can bury it out of sight. This is the old "black box" principle at work; a black box performs a very complex task, but it possesses simplicity on the outside.

If you haven't already lost patience with my quotations, a word from Albert Einstein is appropriate here: "Everything should be as simple as possible, but no simpler."

So, in Ruby we see simplicity embodied from the programmer's view (if not from the view of those maintaining the interpreter). Yet we also see the capacity for compromise. In the real world, we must bend a little. For example, every entity in a Ruby program should be a true object, but certain values such as integers are stored as immediate values. In a tradeoff familiar to computer science students for decades, we have traded elegance of design for practicality of implementation. In effect, we have traded one kind of simplicity for another.

What Larry Wall said about Perl holds true: "When you say something in a small language, it comes out big. When you say something in a big language, it comes out small." The same is true for English. The reason that biologist Ernst Haeckel could say "Ontogeny recapitulates phylogeny" in only three words was that he had these powerful words with highly specific meanings at his disposal. We allow inner complexity of the language because it enables us to shift the complexity away from the individual utterance.

I would state this guideline this way: Don't write 200 lines of code when 10 will do.

I'm taking it for granted that brevity is generally a good thing. A short program fragment will take up less space in the programmer's brain; it will be easier to grasp as a single entity. As a happy side effect, fewer bugs will be injected while the code is written.

Of course, we must remember Einstein's warning about simplicity. If we put it too high on our list of priorities, we will end up with code that is hopelessly obfuscated. Information theory teaches us that compressed data is statistically similar to random noise; if you have looked at C or APL or regular expression notationespecially badly writtenyou have experienced this truth firsthand. Simple, but not too simple; that is the key. Embrace brevity, but do not sacrifice readability.

分享到:
评论

相关推荐

    Ruby Performance Optimization, Why Ruby is Slow, and How to Fix It

    Ruby Performance Optimization: Why Ruby is Slow, and How to Fix It By 作者: Alexander Dymo ISBN-10 书号: 1680500694 ISBN-13 书号: 9781680500691 Edition 版本: 1 出版日期: 2015-11-29 pages 页数: (202) ...

    why’s (poignant) guide to ruby pdf高清版

    作者“Why the Lucky Stiff”(笔名)以一种非传统的教学手法,打破了传统编程教材的沉闷,使得读者在轻松愉快的氛围中掌握Ruby的核心概念。 Ruby是一种面向对象的、动态类型的编程语言,由日本人松本行弘于1995年...

    为什么是(Poignant)Ruby指南Why's (Poignant) Guide to Ruby

    《为什么是(Poignant)Ruby指南Why's (Poignant) Guide to Ruby》是一本非常独特且内容丰富的Ruby编程语言教程。这本书由Why所编写,并以其幽默的语言风格和深入浅出的解释方式闻名于Ruby社区。它不仅仅是一本技术...

    Why's (Poignant) Guide to Ruby

    《Why's (Poignant) Guide to Ruby》是一本以幽默和不同寻常的方式向读者介绍Ruby编程语言的书籍。本书的独特之处在于,它不仅包括了编程教程的内容,还出人意料地附带了一颗洋葱作为礼物,这种做法着实令人感到惊喜...

    why.s.poignant.guide.to.ruby.pdf en

    根据提供的文件信息,我们可以推断出这是一份关于Ruby编程语言的指南——《Why's (Poignant) Guide to Ruby》的部分内容。虽然实际文本部分并未给出具体的技术细节,但基于标题、描述和部分可见的内容,我们可以围绕...

    Optical Lithography: Here is Why

    This book is aimed at new and experienced engineers, technology managers, and senior technicians who want to enrich their understanding of the image formation physics of a lithographic system....

    Node.js-why-is-node-running-Node.js的运行但你不知道为什么吗?

    在"why-is-node-running"这个主题中,我们探讨的是Node.js应用程序在后台持续运行,而用户可能对其运行的原因和机制感到困惑的问题。下面将详细解释Node.js的运行原理、为何会持续运行以及如何进行调试和剖析。 ...

    Why Python is Slow: Looking Under the Hood

    Why Python is Slow: Looking Under the Hood # Why Python is Slow Python is slower than Fortran and C for a variety of reasons # Python meta-hacking: Don't take my word for it # Just for fun: a few ...

    Ruby.Performance.Optimization

    And finally, you'll dive into the Ruby interpreter internals to really understand why garbage collection makes Ruby so slow, and how you can tune it up. What You Need: Some version of Ruby. The ...

    DC3-3-Why Tokyo Is Insanely Well Designed.srt

    DC3-3-Why Tokyo Is Insanely Well Designed.srt

    What Is ChatGPT Doing ... and Why Does It Work.pdf

    《ChatGPT在做什么...以及为什么它有效》 这本书由斯蒂芬·沃尔夫勒姆(Stephen Wolfram)撰写,探讨了ChatGPT的工作原理及其为何能成功运作。沃尔夫勒姆是计算科学领域的权威,他的公司Wolfram Media出版了这本书...

    Optical Lithography Here is Why

    ISBN-10: 0819475602 作者:伯恩 林, Senior director of the micropatterning division at Taiwan Semiconductor Manufacturing Co. Ltd. 就是大名鼎鼎的TSMC 的光刻部门的老大 这是第二部分,第一部分请用同样...

    ruby 经典教程从新手到专家

    《Ruby经典教程:从新手到专家》是一本深入浅出介绍Ruby编程语言的权威指南,由Peter Cooper编写,why the lucky stiff作序推荐,专为Ruby爱好者和希望掌握Ruby编程技术的学习者设计。本书全面覆盖了Ruby编程的基础...

    Why software is eating the world

    Software is eating the world. We believe that many of the prominent new Internet companies are building real, high-growth, high-margin, highly defensible businesses.

    why-is-node-running:节点正在运行,但您不知道为什么? 为什么节点运行在这里可以为您提供帮助

    why-is-node-running在这里可以为您提供帮助。 安装 节点8及以上: npm i why-is-node-running -g 较早的节点版本(不再受支持): npm i why-is-node-running@v1.x -g 用法 const log = require ( 'why-is-node...

    What Is ChatGPT Doing ... and Why Does It Work(2023)

    《ChatGPT是如何工作的...以及为何它有效(2023)》这本书深入浅出地探讨了人工智能领域的一项新突破——ChatGPT的工作原理。ChatGPT是一个能够以人类水平生成自然语言的AI系统,其出现让业界大为震惊,甚至连它的创建...

Global site tag (gtag.js) - Google Analytics