Project

General

Profile

Actions

Feature #16244

open

Add a Time#before? and Time#after? method

Added by stevendaniels (Steven Daniels) over 4 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:95260]

Description

Comparing times using > and< can be pretty confusing.

I'd like to propose Time#before? and Time#after? methods for aliasing Time#< and Time#>

These method aliases would make the code more readable. :)

Current syntax:

if current_time < expiration_at
  # do stuff
end

if birthday > thirteen_years_ago
  # you need a parent's permission to sign up
end

What I'd like to see added:

if current_time.before? expiration_at
  # do stuff
end


if birthday.after? thirteen_years_ago
  # you need a parent's permission to sign up
end

Thanks for your consideration!

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

stevendaniels (Steven Daniels) wrote:

Comparing times using > and< can be pretty confusing.

This is subjective. I don't find such Time comparisons confusing. I would guess the > and < symbols are probably more understandable to people whose native language is not English, at least.

I'd like to propose Time#before? and Time#after? methods for aliasing Time#< and Time#>

The argument you are making for adding them could be made for any objects implementing > and <. Maybe your need could be solved with?:

module Comparable
  alias before? <
  alias after? >
end

One of the great things about Ruby is that it is simple to add these aliases yourself.

Updated by stevendaniels (Steven Daniels) over 4 years ago

jeremyevans0 (Jeremy Evans) wrote:

I would guess the > and < symbols are probably more understandable to people whose native language is not English, at least.

It's hard to speak for all non-native English speakers one way or the other, but in the languages non-English languages I speak, using greater than and less than to compare times isn't a natural way to frame the comparison; by comparison, before and after is a natural way to compare times in most languages.

Over the years I've seen this confusion manifested by developers of all levels of experience leading to real life bugs. Adding aliases that help to alleviate that confusion would be a nice thing to provide to all Ruby developers (which is why I'm making this feature request).

Subjectively speaking, comparisons likeif blog.published_at.before? Time.now feel more natural than if blog.published_at < Time.now.

(Big fan of your work, BTW!)

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

Jeremy Evans is right about this.

But I agree that your second example

birthday > thirteen_years_ago

is a bit hard to comprehend. However,

birthday.after? thirteen_years_ago

is as hard to comprehend. You should not be writing like that. You should either do somthing like

birthday + thirteen_years > now

or

now - birthday < thirteen_years

Updated by stevendaniels (Steven Daniels) over 4 years ago

sawa (Tsuyoshi Sawada) wrote:

You should not be writing like that. You should either do somthing like

birthday + thirteen_years > now

or

now - birthday < thirteen_years

In your example, your coercing the dates to Floats and then comparing integers. Part of the reason I think before?/after? would be a good addition is because they remove the need to coerce a Time in order to do a comparison. I think your solution illustrates part of the problem

I agree that my example in the beginning isn't perfect. A better example might be this:

steven.birthday.before? john.birthday

instead of

steven.birthday < john.birthday

I have a more basic question: are there specific criteria for accepting alias methods for core objects? Is Ruby's preference to avoid adding alias methods?

Updated by keithrbennett (Keith Bennett) over 4 years ago

I think the answer to the question 'which is clearer, more obvious, etc.' cannot be answered objectively. Sawa's example is evidence; he sees his preferred expressions as clearer; I find them quite a bit less clear. It all depends on how the person is in the habit of thinking.

Some people will think of the comparison in terms of mathematical language and would need to mentally transform a 'before' into '<', requiring more effort. Others are the opposite.

I will refer to '<' and '>' as the symbol approach and mathematical language, and 'before' and 'after' as the word approach and natural language. Here are some arguments on both sides (all have merit for at least some people):

  1. Time is like any other scalar value, so the symbol approach is natural, while the word approach makes this less self evident, as words can define relationships far wider than scalar relationships.

  2. The symbol approach uses only 1 character and is easier to miss visually.
    vs.
    The symbol approach is visually unique and easier to spot than a word.

  3. Adding too many aliases to the language degrades the language, requiring more to learn in order to understand preexisting Ruby code; also, using multiple ways to refer to the same thing hides the fact that it is the same thing underneath.
    vs.
    Adding certain aliases improves the language, enabling expressing intent more clearly

  4. Adding these aliases are a slippery slope; once we add them, will we want to add many more Rails-y aliases?
    vs.
    Adding these aliases is not a slippery slope; these are targeted aliases for mathematical symbols and not just alternate human language.


Also, Jeremy, I agree with everything you said except that your suggestion to add the aliases oneself only very slightly addresses Steven's need; unless these aliases are part of the language, he's likely to encounter a lot of resistance to using them in a shared code base.

Updated by anastasiastowers (Anastasia Stowers) over 4 years ago

Using the > and < symbols makes sense to compare Time only with the knowledge of the underlying unix time system being used.

Although this is the way that we choose to store values of it in our systems, Time is special because it has direction in the way that we perceive it. The words “before” and “after” the only words we use to compare it when considering infinitesimally close event values.

I think the promotion of “natural language” is the direction that Ruby has been going.

Some examples of preferring “natural language”:
https://bugs.ruby-lang.org/issues/12746
https://bugs.ruby-lang.org/issues/13784
https://bugs.ruby-lang.org/issues/14043
https://bugs.ruby-lang.org/issues/14697

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

anastasiastowers (Anastasia Stowers) wrote:

Using the > and < symbols makes sense to compare Time only with the knowledge of the underlying unix time system being used.

I don’t think that knowledge necessary.
Rather, the time is comparable physical quantity.

Some examples of preferring “natural language”:
https://bugs.ruby-lang.org/issues/12746
https://bugs.ruby-lang.org/issues/13784

These are selections of more proper names.

https://bugs.ruby-lang.org/issues/14043

This is a deprecation of too magical special variables.

https://bugs.ruby-lang.org/issues/14697

This is opposite, preferring a symbol over “natural language”.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0