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

This makes more sense, but I still find it odd and disagreeable. I tend to prefer that my programs fail loudly, so I'd write string -> int conversion to raise an exception on seemingly non-numerical input like "foo", "ba6r.5", "pwn3d", etc. Then again, admittedly I know very little about web development. Is it not practical to do this?


There is nothing special about web development that makes implicit typecasting or returning an arbitrary value when a cast fails instead of raising an exception a good idea. Another reasonable option is to return NaN (Javascript does this) or null.


agree about failing loudly when bad stuff happens - but exceptions are best reserved for truly exceptional circumstances. in webdev everything arrives from the browser as a string so implicit casting is more of the norm. if your user enters something dumb into the quantity field of your shopping cart it doesn't really warrant abandoning the rest of their input and leaping back up the call stack to a "misson failed"-level error handler.

functions like is_numeric('string') exist for the rarer situations where things like this actually matter, but php's default way is simply reflective of it's nature as a loosely-typed language.


i believe these casts, or whatever the implicit coercions are called, follow the perl rules, and along with rules for truth and falsehood, behave predictably. The chart is kind of overkill




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

Search: