Avoiding the classic C quoting bug in your languageTo summarize an earlier entry, the classic C
quoting bug that I'm talking about here is writing ' To start with, let's turn the issue around: what is it about these functions that causes the bug? The answer is that the bug happens because all of these functions do two things; they do something useful and in the process of doing it they format their arguments for you. The bug happens when you (the programmer) just want to do the useful thing and you overlook the fact that you're also getting the formatting for free. The conclusion is straightforward. To make this bug impossible, we need to make functions like this do only one thing; they should take a plain string and not format it. But people still need string formatting, so we need some easy way for people to do it in order to make these single purpose functions both feasible and convenient, a way that involves as close to no extra work as possible. In short, we need effortless generic string formatting. (The need for no extra work is why we can't do this in C. We need a language where you don't need to explicitly manage string lifetimes, since the result of string formatting is another string.) In theory you can implement generic string formatting as a function call
(ideally with a very short function name). In practice I think that
it isn't going to work the way you want because of perception issues;
if string formatting is just a function call, it's still tempting to
create convenience functions that bundle the two function calls together
for you (one to format the arguments then one to do the useful thing).
Doing generic string formatting as an operator (such as Python's ' (The third approach to effortless generic string formatting is string interpolation in certain sorts of strings. This has the benefit of sidestepping the entire problem, although it has problems of its own.) PS: another approach in an OO language is to give strings an explicit
formatting or interpolation method, so that you might write ' |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |