by kolektiv · 15y ago · 0 comments
Now you have something which can still look clean (how clean may depend on your language, type inference, etc.) without bringing in messy pseudo-Hungarian-ness, which stops me doing the most important thing with my code - reading it easily.
Obviously it's only a silly and trivial example, but I'm not sure this article has dated that well in some regards (or perhaps only really applies to certain types of languages - Visual Basic, for example, pretty much forces you to do something like Hungarian to maintain any semblance of sanity long term).
Personally, I'd rather go Hungarian.
Edit - well, yes, you may well be able to do this sort of thing nice and easily in Haskell, but I'm not sure that going with Haskell to avoid the training and maintenance overhead of complicating your Java (or whatever) is a net reduction in workload and hiring difficulty...
type UnsafeUserGenerated = UserGenerated a
instance Monad UnsafeUserGenerated where
... (monadic boilerplate skipped)...
apiFunction :: String -> String
funcOfUnsafe :: U -> UnsafeString
funcOfUnsafe unsafe = (liftM apiFunction) unsafe newtype Unsafe a = Unsafe a
instance Functor Unsafe where
fmap f (Unsafe k) = Unsafe . f $ k
addTwo :: Int -> Int
addTwo = (+2)
unsafeAddTwo :: Unsafe Int -> Unsafe Int
unsafeAddTwo = fmap addTwo
But really, I'm not sure this is the right approach. Even values generated inside my program need to be quoted for inclusion on an HTML page. What you want to avoid is double-quoting, so what you need is simpler: data Content = Quoted String | Unquoted String
output :: [Content] -> Content
output = concatMap f
where f (Unquoted x) = quote x
f (Quoted x) = x
Now the type system ensures that (output . output) == output, which is what you really want to ensure. Tainted data, I think, is a separate concern. And the solution, in that case, doesn't involve a functor, it involves making sure your library tags everything as Unsafe and that your data validation functions remove that annotation: type Params a = Map String (Unsafe a) -- keys may also be unsafe, YMMV
readHtmlForm :: Request -> Params String
validateField :: Validatable a => Unsafe a -> a
main = output . Unquoted . validateField . get "foo" . readHtmlForm <$> fakeHttpRequesthttp://perldoc.perl.org/perlsec.html#SECURITY-MECHANISMS-AND...
I gave up on perl's taint mode when I discovered this bug http://bugs.debian.org/411786 , in which perl randomly sets the taint flag due to a utf8 bug.
Anyway, I'm not sure why it would be less cool even if it were inspired by another language (which I'm sure it was; Perl, like English, elevates borrowing to an art form).
Considering how dynamic perl is, and that you can mix tainted and non-tainted values in a single collection (for instance), I don't see how a perl program could be statically analyzed for taintedness misuses.
The reality is that nobody uses taint mode, though, for whatever reason. If you look at my comment up the page, the problem that people have is not managing the safety of data, it's making sure that they present the right "view" of that data to the right component. HTML needs to be escaped, but not if it's already been escaped, and so on.
Uh? I do use it; it's extremely efficient. I'm probably not the only one :)
Right, so it's not at compile time. Thank you.
For example, I do a lot of Excel+VBA programming. In VBA, the native data type is `Variant`. And if you pass something from Excel to a VBA function, that's what you get. But you also know that legal Excel values are just a subset of what VBA allows in a `Variant`. So I find it very useful to stick a clue to myself in the variable name, so say, `f(parm)` becomes `f(xParm)`.
I have libraries that work with the `x` subset of `Variant` (and subsets of that - i.e. `xs` denotes a simple value that can go in a single Excel cell). The prefixes provide useful clues that the language can't reasonably help me with.