TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Making Wrong Code Look Wrong (2005)

112 pointsby aleyanover 5 years ago

12 comments

lalaithionover 5 years ago
Most modern typed languages support encoding this information in the type of the variable, so that the compiler catches it, instead of in the variable name. Alexis King wrote a blog post about it that reached the front page a few days ago. <a href="https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-validate&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-va...</a>
评论 #21489968 未加载
评论 #21489984 未加载
评论 #21489977 未加载
评论 #21490059 未加载
评论 #21490141 未加载
stinosover 5 years ago
Just looking at the safe&#x2F;unsafe string example I wonder if another usable approach would be to ditch plain strings and instead use thin wrappers like UnsafeString and SafeString and a bunch of operations on them. But not assignment, for instance.<p>There wouldn&#x27;t be much need to care about whether the code looks wrong or not (welll with regards to safe vs unsafe string:), because the compiler would do it for you (or runtime I guess, depending on which laguage it gets implemented in). I think all the examples Joel writes (the ones &#x27;xxx is always ok&#x27; and &#x27;xxx is not ok&#x27;) are covered by it. It does mean you need a Write which only takes SafeString I guess, and it probably doesn&#x27;t mean you can still do something wrong, but it should be much harder.
评论 #21492846 未加载
评论 #21492824 未加载
dangover 5 years ago
For the curious:<p>2009 <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=472477" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=472477</a><p>2011 <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2912218" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=2912218</a><p>Small:<p>2015 <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8987366" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8987366</a><p>2019 <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20586837" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20586837</a>
pwdisswordfish2over 5 years ago
Another way to solve the escaping problem is by some kind of interpolation mechanism that takes care of escaping on its own: like JSX or template literals in JavaScript (although you have to remember to tag the latter), or prepared statements in SQL. Why fix a coding convention when you can fix the language?
esotericnover 5 years ago
In a previous job, I wrote an autotrader which by now I believe will have handled hundreds of millions of pounds in untyped Python 2. Certainly tens of millions.<p>There were a number of approaches I used. This is a few years back now, so apologies if anything is unclear.<p>You can fake a type system to some extent in py2 by using methods and copious &#x27;isinstance&#x27; checking.<p>For example, Money&lt;EUR&gt; + Money&lt;GBP&gt; can be made illegal by overloading addition operators. Strings which require some sort of meaning can be given classes and functions which use them can use isinstance and friends to perform runtime type checks.<p>Another is indeed the use of &#x27;raw_whatever&#x27; and &#x27;whatever&#x27; in identifiers, what I now know to be &quot;apps hungarian&quot; notation. &#x27;raw_whatever&#x27; would have a similar definition to Joel&#x27;s unsafe user input. It might come from an API of some sorts that you don&#x27;t truly &quot;trust&quot;.<p>Similarly, that sort of variable naming approach applied to function parameters. Passing a &#x27;dog_id&#x27; to a &#x27;cat_id&#x27; function _may_, in certain cases, be possible if both were flat strings (and not objects that could be isinstance checked), but I favoured a variable naming approach (along with calling functions by keyword argument) that would result in this at least being visible after problems came up (e.g. you&#x27;d see myfn(dog_id=cat_id) and feel an urge to hold your nose).<p>There were tons of these sorts of things all over the codebase, tests, etc, and the system outperformed the previous ones by a significant margin. My understanding is that it still hasn&#x27;t suffered any significant losses; only some minor API issues that were outside of our control.<p>Super fun project. Nowadays I&#x27;d just use a typed language for it and interface with the py2 stuff via an API. Or at least make use of mypy. But that autotrader was what the company needed at the time.<p>Details are in my bio if anyone has further interest.
BrissyCoderover 5 years ago
This will sound simplistic but just avoid using C++. You&#x27;d have to pay me twice my current salary to go back to that language (or C).
评论 #21490605 未加载
staredover 5 years ago
I used to hate code linters. Now, when collaborating with others, I set an aggressive one.<p>They make wrong code look wrong, literally.<p>Of course, it does not catch all wrong code examples, but at least some most glaring examples that otherwise would need manual inspection.
b15h0pover 5 years ago
The checker framework (<a href="https:&#x2F;&#x2F;checkerframework.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;checkerframework.org&#x2F;</a>) can make the compiler understand about stuff like this without introducing additional types in Java.<p>I have never tried it myself, maybe someone with experience can chime in?
评论 #21491884 未加载
andy9775over 5 years ago
WRT exceptions I&#x27;m curious what his thoughts are on the go way of handling them (just return an exception, don&#x27;t throw)? Clearly this article was written pre-go.
评论 #21494067 未加载
kwhitefootover 5 years ago
Most people commenting here seem to be missing Joel&#x27;s point that the Hungarian notation makes it possible to read the code without having to continually search elsewhere for information about what it means. I&#x27;m pretty sure that he would be all in favour of declaring sub-types in languages that support it but an awful lot don&#x27;t or don&#x27;t make it convenient.
emilfihlmanover 5 years ago
This was a really good read! Highly recommended for everyone. I&#x27;ll highly likely incorporate it into my own code.
apricotover 5 years ago
Nice. It&#x27;s the most lucid explanation I&#x27;ve seen of what Hungarian notation really is, what it can do, and how different it is from sticking &quot;ul&quot; in front of every unsigned long variable.