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

Refactor find_answer #686

Merged
merged 3 commits into from
Aug 8, 2023
Merged

Refactor find_answer #686

merged 3 commits into from
Aug 8, 2023

Conversation

imobachgs
Copy link
Contributor

@imobachgs imobachgs commented Aug 3, 2023

Problem

While checking the answers module, I discovered we were using a labeled for. That's perfectly fine, but coming from Ruby I felt that we could use a higher-level approach.

I took it as a refactoring exercise given that the original implementation was OK.

Solution

  • Move the logic to determine whether an answer responds to a question (just moving that code to a different function allows us to avoid the nested for). Moreover, I think the logic belongs to Question or Answer.
  • Replace the for structs with calls to find and all, as they express the intention better.
  • Import (use) the GenericQuestion to avoid spelling the whole module name (agama_lib::questions::GenericQuestion vs GenericQuestion). If tomorrow we decide to change the GenericQuestion to a different place, we need to update all the references. Not to mention that the lines are rather long and harder to read.

Testing

  • The module has quite some tests (thanks!) and they pass.

/// * `question`: question to compare with.
pub fn responds(&self, question: &GenericQuestion) -> bool {
if let Some(class) = &self.class {
if !question.class.eq(class) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively, we can write:

Suggested change
if !question.class.eq(class) {
if question.class != *class {

It looks more natural to me, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

in that case, also do it for question.text below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, an even for e_val.eq(value) below.

@imobachgs imobachgs changed the title Refactor find answer Refactor find_answer Aug 3, 2023
@coveralls
Copy link

coveralls commented Aug 3, 2023

Coverage Status

coverage: 72.07% (-0.009%) from 72.079% when pulling 7848121 on imobachgs:refactor-find_answer into 57d328b on openSUSE:master.

Copy link
Contributor

@mvidner mvidner left a comment

Choose a reason for hiding this comment

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

Makes sense, thanks!

Copy link
Contributor

@teclator teclator left a comment

Choose a reason for hiding this comment

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

LGTM

@imobachgs imobachgs merged commit 3ad614a into agama-project:master Aug 8, 2023
7 checks passed
@imobachgs imobachgs deleted the refactor-find_answer branch August 8, 2023 08:44
@imobachgs imobachgs mentioned this pull request Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants