Mostly taste, like I don’t like ActiveRecord, database naming defaults, yaml, etc. But here is something from a What’s new in Rails 2 slideshow:
validates_numericality_of :salary, :greater_than => 49_000
Now I’m not talking about the amount. Although I’m aware as a RoR developer, you run the risk of failing that particular validation check, something you’d never worry about in Java or C++ land.
What I mean is, if this is an example of a dynamically typed language, I don’t want any of it. Here is the same code in Java or C:
int salary;
salary > 49_000;
Much more concise, and more better checked too. This will catch it at compile time (unless you have a cast from a hash — but that’s an argument in favor of generics.)
Of course, Rails has a bunch of code behind the scenes that is probably doing Javascript and serverside validation and displaying a pleasant and ambiguous error message to the user and log, but you can do all that with an annotation as well. A more realistic comparison might use an annotation to tie your salary integer to your view and/or model and require a try/catch block for type cast errors. Or it could wrap all that up in a static function that starts with “validates” that does all that.
I’m arguing for the syntax. And I don’t think it takes a statically typed, pre-compiled language to get that. I hope it doesn’t take operator overloading to achieve a > comparison. Any decent language should be able to cast, throw a runtime error on type mismatch, and return false. How 49_000 is cast is dependent on the language, and it doesn’t really matter as long as it is consistent and documented. So guess I’m aguing against needing to use is_numeric() style functions as well.
With enough scaffolding and inheritance, you can do anything, even in C++. As a matter of fact, with a hypothetical C++ framework like Rails (with other goodies like boost GC) you could get pretty close to achieving the ideal conciseness above. Maybe we should all switch to C++ web frameworks once the DHH of C++ comes along. Then our numerical validation really would be:
salary > 49_000
Recent Comments