编程与软件工程lexi-lambda.github.io编辑精选
解析,而非校验
Parse, Don’t Validate
2019 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章主张“解析而非校验”:与其在程序各处反复检查数据是否合法,不如在边界一次性把弱结构输入解析成更强类型的值,让类型或抽象构造器承载不变量,使非法状态难以表示。文中以 Haskell 为例讨论解析器、部分函数与 smart constructor,并指出校验只是“检查过”,解析得到的才是“已被证明可用”的数据。
为什么经久不衰:它把“边界解析、用类型承载不变量”讲得异常清楚,是跨越语言与年代仍适用的设计原则,今天读能直接改善 API 边界和错误处理。
HN 评论精选
即使类型系统表达力有限,也可用 smart constructor 把不变量封装进抽象类型,本质上仍是解析。
说得太对了!把不变量编码进数据里,而不是对数据反复执行不变量检查。我觉得这一点尤其需要强调,因为实际上并非每种语言都有 Haskell 那样的类型检查能力:有时受工具限制,让非法状态真正不可表示并不现实,比如确保整数落在某个范围。这时可以用带 smart constructor 的抽象 newtype 来“伪造”一个来自校验器的解析器。例如很少有类型系统能把素数编码成类型,所以你只能写 newtype Prime = Integer、mkPrime :: Integer -> Maybe Prime 之类,这其实就是解析!
darkkindness查看英文原文
Yes, yes yes! Encode invariants in your data, don't enforce invariants on your data. I particularly think this point needs to be stressed more, because practicably speaking, not every language has the typechecking faculties of Haskell: > Sometimes, making an illegal state truly unrepresentable is just plain impractical given the tools Haskell provides, such as ensuring an integer is in a particular range. In that case, use an abstract newtype with a smart constructor to “fake” a parser from a validator. For instance, few type systems will let you encode prime numbers as datatypes, so you'd have to do something in your language like newtype Prime = Integer mkPrime :: Integer -> Maybe Prime mkPrime | isPrime p = Just (Prime p) | otherwise = Nothing which is parsing!
质疑“解析器必然是部分函数”,认为 split 等全函数也可视为解析器。
我从这篇文章学到很多,希望再读。只有一点异议:我认为把一些全函数也算作解析器是合理的,因为有些文法生成所有字符串,此时没有失败情形。一个不太平凡的例子是许多语言里的 split();它的 Haskell 类型是 String -> [String],是全函数,能处理无分隔符、空串、只有分隔符等情况。另一个例子是文法允许对象后有任意尾随字符串(类似 C 的 atoi),解析器总可返回空对象。
schoen查看英文原文
I learned a lot from this article and hope to revisit it. I had one quarrel: > Consider: what is a parser? Really, a parser is just a function that consumes less-structured input and produces more-structured output. By its very nature, a parser is a partial function—some values in the domain do not correspond to any value in the range—so all parsers must have some notion of failure. I think it's reasonable to include some total functions as parsers as well, because some grammars -- structures that are recognized by the parser and that specify languages -- generate all strings as the corresponding language. In that case, there are no failure cases because every string is a valid input. I came up with some more-trivial examples, but I think a less-trivial example is the split() function in many languages. Its Haskell type signature is String -> [String], and it is a total function. It handles the case where the input is a nonempty string with no delimiters (the output is a list containing a single token equal to the input), as well as the case where input is an empty string (the output is an empty list), and the case where the input consists only of delimiters (in many implementations, the output is a list containing n+1 empty strings). Another trivial-ish case is where the grammar allows an arbitrary trailing string following the object of interest (similar to C's atoi). In that case, a parser will always return the empty object if there is no interesting or relevant data in the input. (For example, atoi returns 0 if the input string doesn't begin with an integer.) This could be viewed as a kind of error, but there may be applications in which this behavior is useful.
文章与 langsec 相通:形式化输入结构、拒绝非法数据而非硬处理。
读标题时我首先想到语言理论安全(langsec),很高兴看到文章末尾引用了它。http://langsec.org 的理念是形式化地规定输入结构,并拒绝非法数据,而不是试图处理它。
matheusmoreira查看英文原文
Language-theoretic security was the first thing that came to mind when I read the title. Was pleasantly surprised to see it referenced at the end of the article. http://langsec.org The idea is to formally specify the structure of inputs and reject invalid data instead of trying to process it.
在动态语言里用 pydantic 等 schema/解析层也能获得同样收益。
这个原则让 pydantic 彻底改变了我的 Python 开发体验。以前我要在 REPL 里反复测试函数、写大量校验样板,还总是遇到 TypeError、NoneTypeError、AttributeError;现在我就像正常写代码,而且它能跑通!有一次我一天写了几百行 Python,然后直接运行,它居然工作了。我坐在那里震惊地等必然出现的崩溃和 traceback,结果一直没来。在 Python 里!不可思议。
kortex查看英文原文
This principle is how pydantic[0] utterly revolutionized my python development experience. I went from constantly having to test functions in repls, writing tons of validation boilerplate, and still getting TypeErrors and NoneTypeErrors and AttributeErrors left and right to like...just writing code. And it working! Like one time I wrote a few hundred lines of python over the course of a day and then just ran it... and it worked. I just sat there shocked, waiting for the inevitable crash and traceback to dive in and fix something, but it never came. In Python! Incredible. [0] https://pydantic-docs.helpmanual.io/
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选