编程与软件工程programming.guide
史上被复制最多的 StackOverflow 代码片段存在缺陷(2019)
The most copied StackOverflow snippet of all time is flawed (2019)
2019 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章剖析了 Stack Overflow 上被复制次数最多的 Java 代码片段之一:一个把字节数格式化成“人类可读”大小(如 1.2 MB)的方法。作者指出这个流传极广的片段在舍入、本地化和边界处理等方面存在缺陷,并给出了修正版本。
为什么经久不衰:它用一个被无数人复制过的“简单”片段说明:看似琐碎的代码也可能暗藏 bug,复制粘贴会放大问题,而公开讨论和持续维护才能真正提高代码质量。
HN 评论精选
Stack Overflow 的署名和评分机制阻碍了答案持续改进,排序应更看重质量而非累计票数。
我是同一榜单上第 6 名的作者。看到它在 GitHub 上被用了数千次,专有代码里还不知道有多少次,确实很有意思。我不认为它有 bug,但现在觉得它肯定可以改进。这也体现了 Stack Overflow 与其最初愿景相比的一个大问题:原本希望像维基百科那样让答案随时间不断改进,但大多数答案并没有。把作者名字和可见分数挂在答案上会带来所有权感,别人就不太敢来修改完善;而一旦答案排到顶部,就更容易获得赞,其他改进答案很难冒上来。按总票数排序是很多网站的通病,也许应该看票数除以浏览量。
ddlatham查看英文原文
I'm the author of #6 on the same list. It's definitely interesting to see it has been used thousands of times on GitHub, and who knows how many more in proprietary code. I don't think it's buggy, but I now think it could definitely be improved. I think this shows an example of a big problem with StackOverflow compared to its initial vision. I remember listening to Jeff and Joel's podcast, and hearing the vision of applying the Wikipedia model to tech Q&A. The idea was that answers would continue to improve over time. For the most part, they don't. I'm not quite sure if it's an issue of incentives or culture. Probably some of both. I think that having a person's name attached to their answer, along with a visible score really gives a sense of ownership. As a result, other people don't feel enabled to come along and tweak the answer to improve it. Then, once an answer is listed at the top, it is given more opportunity for upvotes, so other improved answers don't seem to bubble up. This is a larger issue with most websites that sort by ratings. Generally they sort items based on the total number of votes, including hacker news itself. Instead, to measure the quality of an item, we should look at the number of votes, divided by the number of views. It may be tough to measure the number of views of an item, but we should be able to get a rough estimate based on the position on a page, for example. If the top comment on a HN discussion is getting 100 views in a minute and 10 upvotes, but the 10th comment down gets 20 views and 5 upvotes, the 10th comment is likely a better quality comment. It should be sorted above the top ranked comment! There would still need to be some smoothing and promotion of new comments to get them enough views to measure their quality as well. Such a policy on StackOverflow would also help newer, but better answers sort to the top.
质疑为了无分支而牺牲可读性,实际 IO 开销更大,且修复版仍有分支。
简单的循环(就像文章里靠近顶部那个)有什么问题?为什么非要无分支?IO 难道不比分支预测失败、流水线刷新更耗时吗?更别说修复后的版本现在也有分支了……
colejohnson66查看英文原文
What’s wrong with a simple loop (like the one near the top)? Why does it have to branchless? Wouldn’t the IO take longer than missed branches/pipeline flushes? Not to mention that the fixed version now has branches as well…
批评该格式化方案有效数字不一致,且未处理本地化(小数分隔符、语言环境)。
作为人类,我讨厌这种“人类可读”格式的第一点是有效数字位数不一致。小数点后一位就是错的:从 999.9 MB 跳到 1.0 GB,有效数字从 4 位变成 2 位;应该用 1.000 GB、10.00 GB 这样。第二点,缺少 locale 参数,不同文化的小数分隔符不同,云环境中机器的 locale 常和显示信息的 locale 不一致。第三点,对使用非拉丁字母文化的人来说,问题也很明显。
anonymfus查看英文原文
> return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); As a human, the first thing that I hate about this interpretation of "human readable" format is inconsistency in the number of significant digits. One digit after decimal separator is simply wrong, as when you jump from 999.9 MB to 1.0 GB you go from 4 significant digits to 2, instead it should be 1.000 GB, 10.00 GB and so on. This annoys me enormously when I upload things to Google Drive from Android phone and look at the number of data transferred as as soon it becomes bigger than 1 GB digits stop changing and I become anxious that it stopped the transfer and my Windows Phone nostalgia jumps over the roof (as WP was never infected with this problem by virtue of not using Java, and OneDrive on WP explicitly showed current connection speed, and frozen connection never caused any strange problems with uploaded files like it does on Google Drive on Android). As a human not from US, the second thing I hate here is lack of locale parameter to pass to formatter as decimal separator is different in different cultures, and in the world of cloud computing the locale of the machine where the code is run is often different from the one where the message is displayed. As a human from a culture using non latin alphabet, the third thing I hate here should be obvious for a reader.
复制粘贴后片段失去维护,应把这类代码集中到小型包中持续改进并通知更新。
我觉得这很好地支持了我们在 npm 上有时看到的小范围包。很多时候,像这样看似简单的代码片段其实并不简单。我的意思是,复制粘贴后你就和片段失去了所有联系。如果把它的开发集中起来,利用集体智慧来加固它,并在发现边界情况时收到更新通知,明显是有好处的。
janpot查看英文原文
I feel like this would be a good argument in favour for small scoped packages like we sometimes see on npm. Often enough it turns out that a trivial code snippet like this turns out to be not so trivial after all. edit: The point being that you lose all connection with a snippet after you copy+paste it. I can clearly see benefits when you centralize its development, make use of the collective mind to harden it, and get notified about possible updates whenever an edge-case is found.
历次讨论
同类还值得读
开源应用架构
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编辑精选