Making Wrong Code Look Wrong (2005)
joelonsoftware.com
joelonsoftware.com
i_int += j_filehandle
would stick out as wrong. But luckily many languages support type checking so we don't have to do it in those languages!
I wonder is it possible to avoid mixing safe and unsafe strings (for example) using some new language feature (or a new use of an old one)?
I too initially thought "how great would it be to compile some of these prefixes into a language" but after readign through I think it would be difficult.
The intent is that the prefix describes what the variable is not what type it is. And so the prefix will vary from project to project.
However your example of specifying type is inherently possible and many languages DO do it (not with the prefix - they just check for type at compile time). The point, I think, Joel is making is that the Hungarian convention is a way for the programmer to vet the code as he browses it. Identifying errors and logic clashes etc. :)
What about logic errors - which is what this is supposed to help fix?
data UnsafeString = Unsafe String
htmlencode :: String -> String
sanitizeUnsafeStr :: UnsafeString -> String
sanitizeUnsafeStr (Unsafe cs) = htmlencode cs class UnsafeString:
def __init__(self, str):
self._str = str
self._sanitized = None
def __str__(self):
return self.sanitize()
def unsafe(self):
return self._str
def sanitize(self):
if self._sanitized != None:
return self._sanitized
else:
self._sanitized = sanitize(self._sanitized)
return self._sanitized
That way, as long as you wrap all input in the UnsafeString class, you'll have to be explicit if you want the unsafe version and you'll get the safe version by default.But, for some inexplicable reason, Joel says "Don’t use macros to create your own personal programming language."
How is using macros to encapsulate common functionality any different than using functions to encapsulate common functionality? Both lead to less mental overhead, more code reuse, and code that's easier to parse.
Obviously, macros have almost nothing in common with functions. Macros are simply shorthand for code that will be literally placed at the spot they are called.
So rather than calling functions, with all argument expressions evaluated, and a call stack, and blah blah blah, you are just copy-pasting code without it actually ending up on the screen for a developer to see.
(I do understand the difference, for what it's worth. I worked as a professional Common Lisp coder for a while.)
No one likes to see an input all cluttered with """, "&" and the like.
Of course, there are performance implications to this, but those can be dealt with. Encoding strings right away for performance reasons is definitely a premature optimization.