Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a StatementRank class #562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add a StatementRank class #562

wants to merge 1 commit into from

Conversation

thiemowmde
Copy link
Contributor

@thiemowmde thiemowmde commented Oct 8, 2015

No description provided.

@JonasKress
Copy link
Contributor

+1

@JeroenDeDauw
Copy link
Contributor

Did you consider having an actual Rank object, and if so, what made you go with the static approach?

*/
const RANK_0 = self::DEPRECATED;
const RANK_1 = self::NORMAL;
const RANK_2 = self::PREFERRED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a suggestion for a possible enhancement. You can use these alternative constants in a context where it is critical to know that ranks are integers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In those cases just use 0 instead of Rank::RANK_0 as you hardcode the zero anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, what? What is "hardcoded anyway"? I'm not sure you get the point of all this. Constants are for easy tracking and refactoring. A rank should never be expresses via an integer constant.

@Benestar
Copy link
Contributor

Benestar commented Oct 9, 2015

Thanks for sharing your proposal, we definitely need an improvement how we handle ranks in our data model. However, I think we could avoid introducing so many methods by just relying on the fact that the constant integers of the rank values replicate the order of the ranks (which is in fact what the class does).

What I think would be more appropriate would be a static method Statement::getAvailableRanks which lets us check if some value is a valid rank. All the comparison stuff can be done using the integer values of the constants.

@JeroenDeDauw
Copy link
Contributor

we definitely need an improvement how we handle ranks in our data model.

Can you elaborate on what you currently find inadequate? The same goes for Thiemos proposal here. It's a lot easier to comment on this if its clear what the goal is.

@thiemowmde
Copy link
Contributor Author

Did you consider having an actual Rank object

Yes, and I find the idea of an object wrapping nothing but an integer pointless and scary. It also doesn't help solving the issue I want to solve here: The semantics of how ranks are compared is currently duplicated across our code base. This would only be solved if we ditch the idea of ranks being integers completely, across all components. This would be a major breaking change that's not worth it, in my opinion. Therefore: Ranks stay integers and a helper class with some comparison methods is good enough.

just relying on the fact that the constant integers of the rank values replicate the order of the ranks

That's exactly the issue we have: This knowledge is not in a single place but duplicated across the code base. This caused heavy confusion yesterday, but nobody was able to point to that single place that says "ranks are always, now and in the future, guaranteed to be in increasing order from least to most preferred".

which is in fact what the class does

Yes, it does create an explicit place for this knowledge. That's the point.

getAvailableRanks

That's a possible enhancement, but doesn't solve any issue.

All the comparison stuff can be done using the integer values of the constants.

As said above, yes, it could, but no, it shouldn't.

@JeroenDeDauw
Copy link
Contributor

This caused heavy confusion yesterday, but nobody was able to point to that single place that says "ranks are always, now and in the future, guaranteed to be in increasing order from least to most preferred".

That is somewhat surprising to me. Ranks are parts of statements. So if there is no Rank enum, then that is where I'd first look. And if that is too unclear, I'd just add a Rank enum.

@thiemowmde
Copy link
Contributor Author

How would you do that? There are no enums in PHP.

@JeroenDeDauw
Copy link
Contributor

I meant having a Dedicated class to hold the constants rather than putting them into Statement.

@JeroenDeDauw
Copy link
Contributor

Thinking about it, if we create a dedicated class for ranks, regardless of what we put in there, Rank(s) seems like a bad name. The concept here is not disjoint from statements, so the name ought to reflect that, by being like StatementRank(s).

@thiemowmde
Copy link
Contributor Author

StatementRank is indeed better, will change.

@Benestar
Copy link
Contributor

Benestar commented Oct 9, 2015

This caused heavy confusion yesterday, but nobody was able to point to that single place that says "ranks are always, now and in the future, guaranteed to be in increasing order from least to most preferred".

I wasn't part of that discussion or meeting when this arised so could you summarize what happened? The rank to integer mapping seems to be quite clear to me as there is only one place where the integer values are defined (inside Statement). The only thing that is missing imo is a static place where one can get the list of all available ranks, which can easily be solved by adding a method to Statement. That should btw also answer Jeroen's question.

Can you elaborate on what you currently find inadequate?

We have several places where we hardcode array(deprecated, normal, preferred) in our code and it would be nice if that could be centralized. Beyond that, I don't see the need for another class and therefore more complexity.

@thiemowmde
Copy link
Contributor Author

could you summarize what happened?

What I just explained: There is no single place that encodes the knowledge what "best" means, when a rank is "better" and so on.

The only thing that is missing imo is a static place where one can get the list of all available ranks

I'm not sure how often I should repeat myself. Please read #562 (comment) again.

@thiemowmde
Copy link
Contributor Author

We have several places where we hardcode array(deprecated, normal, preferred)

I can not find this array anywhere outside the Statement class. Some tests use arrays of ranks in data providers, but I do not understand how this is a mistake. Tests should be independent from implementations they are not testing.

@thiemowmde thiemowmde force-pushed the rankClass branch 4 times, most recently from 8f22d3c to fbc9bcf Compare October 15, 2015 13:40
@thiemowmde thiemowmde changed the title [WIP] Add a Rank class Add a StatementRank class Oct 15, 2015
*
* @return int|null Best rank in the array or list of arguments, or null if none given.
*/
public static function findBestRank( $ranks = array() /*...*/ ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid a method with this signature is of not much use. How would you call it when you have a StatementList?

@JeroenDeDauw
Copy link
Contributor

No changes since 2015. Please reopen if needed for current goals

@JeroenDeDauw JeroenDeDauw deleted the rankClass branch August 8, 2018 15:05
@thiemowmde thiemowmde restored the rankClass branch August 13, 2018 11:28
@thiemowmde thiemowmde reopened this Aug 13, 2018
@JeroenDeDauw
Copy link
Contributor

Please rebase and find reviewers if this is change relevant to your goals. The PR just sitting here with merge conflicts and no one looking at it for years is not helpful.

@JeroenDeDauw JeroenDeDauw deleted the rankClass branch September 17, 2018 13:24
@thiemowmde thiemowmde restored the rankClass branch September 17, 2018 13:43
@thiemowmde thiemowmde reopened this Sep 17, 2018
Copy link
Contributor

@JeroenDeDauw JeroenDeDauw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this provides one place where rank comparisons are defined, though do not think global functions are a good approach. Instead we could encapsulate the primitive, something like

class StatementRank {
    public function isPrefferedOver( self $rank ): bool {}
}

That gets rid of the global functions and follows the primitive encapsulation practice.

@JeroenDeDauw
Copy link
Contributor

Also beware that the @SInCE tags need updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants