Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is also an error which cannot, as far as I know, be detected by any coding convention. I'd suggest that if you are trying to prevent such errors with a coding convention rather than tests, that's a process smell.

In practice, I find it best to avoid non-commutative functions with two arguments of the same type. The obvious choice (for a DB I'm writing):

    addRecord :: ByteString -> ByteString -> IO Bool
    addRecord key value = do ...
But this is safer:

    addRecord :: DBKey -> DBValue -> IO Bool
Now `addRecord value key` won't compile.


> This is also an error which cannot, as far as I know, be detected by any coding convention. I'd suggest that if you are trying to prevent such errors with a coding convention rather than tests, that's a process smell.

Coding conventions don't detect errors, they just help to make them more obvious. For instance, if the variables had been named "cake" and "slice", it probably would have been a bit more obvious which side of the '-' sign each one should be on.

In any case, it's not an either-or choice between coding conventions and tests. A sensible developer will have both.

> In practice, I find it best to avoid non-commutative functions with two arguments of the same type.

I guess there's a silent "..where possible" attached to that? :-) There are many times where it's not possible, or at least not sensible; and having too many type conversions in your code can be just as bad for readability as having too few.


It is arguably a coding convention to say "make new types and appropriate conversions to help ferret out errors, even when they are just aliases to existing types". I see what you are doing here as fundamentally the same trick that the OP article does, albeit with some nice static analysis thrown in to help.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: