7 ms·
Code should read like clear code. English should read like clear English. These are not the same things, and efforts to make them the same usually make a mess o
by matthewmacleod 13y ago
Code should read like clear code. English should read like clear English. These are not the same things, and efforts to make them the same usually make a mess of both.
This stinks of principle over pragmatism.
Using the sugar offered by expressions like this requires a bit of knowledge of the development environment. I accept that it's a bit of a different syntax from some other languages, but that doesn't make it unclear at all.
Speaking to Ruby (where ActiveSupport is from), we all know that it's a language that focuses on providing flexible syntax and "multiple ways of doing things", in contrast to some other languages which restrict flexibility to provide more consistently structured code. I don't think it's a bad thing at all to have these options available, and I often find my intentions are much clearer when writing, say:
if post.created_at < 5.minutes.ago
as opposed to Python:
if(post.created_at < (datetime.now() - timedelta(minutes=5)))
or Objective C:
NSDate *now = [NSDate date];
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setMinute:-5];
NSDate *fiveMinutesAgo = [[NSCalendar currentCalendar] dateByAddingComponents:delta toDate:now options:0];
if ([[post created_at] compare: fiveMinutesAgo] == NSOrderedAscending)
That's where the magic comes in, and it makes Ruby ideal for things like web templating, while perhaps making it less suitable for other uses.
TLDR: various constructs like this have their place, they're not inherently bad, but do require knowledge of the tools.
- robbles 13y agoI know this is a little nitpicky, but your Python example has way more brackets than are strictly necessary. if post.created_at < datetime.now() - timedelta(minutes=5): I agree with your argument in principle though. Just wanted to point out if you're trying to make a point about readability / ease of use with code examples, you should try as hard as possible to make each example optimized to avoid accidentally creating a straw man. Also, one criticism of the Ruby version is the invisibility of the types involved. What type is returned by 5.minutes vs 5.minutes.ago? How do I know I can compare the second to a date attribute from ActiveRecord? The Python version is more verbose, but I think it's more clear that I'm using two datetime's and a timedelta.
- jshen 13y agoIn both cases you have to either remember a lot of details, play with it in the reply, or look at the docs as you are writing it. Once it is written the ruby code is far easier to read.
- zaroth 13y agowhat am I missing... if post.create_at < 5.minutes.ago if post.created_at < minutes_ago(5) if ([post created_at] compare: [minutes_ago 5])
- Camillo 13y agoif post.created_at < 5.minutes.ago That reads like "if post was created less than five minutes ago", i.e. if the post is more recent than five minutes. However, if "5.minutes.ago" gives you a timestamp, as is reasonable, then that actually means "if post was created more than five minutes ago", i.e. the very opposite of what it would mean in English. Whoops! A more sensible syntax might be: if post.creation_date < now - 5*minutes "ago", "from_now", "since_now" etc. are especially despicable in a language where you can just use + and -.
- mattgreenrocks 13y agoI'd prefer: if post.editable? to everything I've seen. There's a concept behind the if statement that should go in the model. I realize this is sort of off-topic, but it kind of negates the whole discussion since you can wrap up the cute/ugly and test it alone. :)
- Camillo 13y agoA capital idea, my friend! But I'll do you one better: why not just wrap up the entire program inside a function called "main"? Clearly that would make all syntax discussions moot, now and forever.
- klodolph 13y agoAnd what would the definition of `editable?` look like? fn editable(&self) -> bool { self.created_at < 5.minutes().ago() } Yes, it's wrong. No amount of unit testing or isolation is going to change the fact that whenever you read code like that, your brain will come up with two conflicting meanings, because these cutesy methods make it look too much like English, and English has different rules. English parse tree is: ((less than (five minutes)) ago), whereas code parse tree is (less than ((five minutes) ago)).