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

Handle possible duplicates in random tags generated by tag_torrent #6820

Merged
merged 1 commit into from
Mar 21, 2022

Conversation

kozlovsky
Copy link
Contributor

Fixes #6819

@kozlovsky kozlovsky requested review from a team and xoriole and removed request for a team March 21, 2022 08:43
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot E 1 Security Hotspot
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.1% 0.1% Duplication

@kozlovsky
Copy link
Contributor Author

retest this please

@kozlovsky kozlovsky requested review from drew2a and removed request for xoriole March 21, 2022 09:17
Comment on lines +44 to +58
if tags is None:
tags_count = random.randint(2, 6)
tags = []
while len(tags) < tags_count:
tag = get_random_word(min_length=MIN_TAG_LENGTH)
if tag not in tags:
tags.append(tag)

if suggested_tags is None:
suggested_tags_count = random.randint(1, 3)
suggested_tags = []
while len(suggested_tags) < suggested_tags_count:
tag = get_random_word(min_length=MIN_TAG_LENGTH)
if tag not in suggested_tags:
suggested_tags.append(tag)
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT, noncritical: you use the same pattern twice. It is possible to extract this pattern:

def generate_sentence_with_fixed_word_length(words_count: int, min_word_length) -> List[str]:
    sentence = []
    while len(sentence) < words_count:
        word = get_random_word(min_length=min_word_length)
        if word not in sentence:
            sentence.append(word)

    return sentence

and refactor the code block to:

Suggested change
if tags is None:
tags_count = random.randint(2, 6)
tags = []
while len(tags) < tags_count:
tag = get_random_word(min_length=MIN_TAG_LENGTH)
if tag not in tags:
tags.append(tag)
if suggested_tags is None:
suggested_tags_count = random.randint(1, 3)
suggested_tags = []
while len(suggested_tags) < suggested_tags_count:
tag = get_random_word(min_length=MIN_TAG_LENGTH)
if tag not in suggested_tags:
suggested_tags.append(tag)
if tags is None:
tags_count = random.randint(2, 6)
tags = generate_sentence_with_fixed_word_length(tags_count, MIN_TAG_LENGTH)
if suggested_tags is None:
suggested_tags_count = random.randint(1, 3)
suggested_tags = generate_sentence_with_fixed_word_length(suggested_tags_count, MIN_TAG_LENGTH)

Copy link
Contributor Author

@kozlovsky kozlovsky Mar 21, 2022

Choose a reason for hiding this comment

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

Thank you for the suggestion, but I'm not persuaded we should do it, for the following reasons:

  1. A usual "rule of thumb" is "if the same code is repeated three or more times, consider to extract it into a function". Here the same pattern is repeated only twice.

  2. The version with function does not make the code shorter.

  3. It is not obvious that the function will be reusable in other places. In other situations it may be totally ok to have repeated words in a sentence.

  4. Right now all repeated patterns are inside the same function, extracting the code to a second function makes the code "less local" and so a bit more complex to analyze (as the reader cannot assume that the new function is not used from other places).

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, but the rationale behind referenced "rule of thumb" is the following:

Attempting premature refactoring risks selecting a wrong abstraction, which can result in worse code as new requirements emerge and will eventually need to be refactored again.

In the code above suggested abstraction is clear and correct. It extracts exactly the same logic to the function with a very clear intention.

The version with function does not make the code shorter.

Agree, but shorter doesn't mean better.
The suggested refactoring code is easier to read because it doesn't require from reader to analyze the code blocs and to determine the pattern.

It is not obvious that the function will be reusable in other places. In other situations it may be totally ok to have repeated words in a sentence.

Agree, you can make it local.

Right now all repeated patterns are inside the same function, extracting the code to a second function makes the code "less local" and so a bit more complex to analyze (as the reader cannot assume that the new function is not used from other places).

Agree, you can make it local.

@kozlovsky kozlovsky requested a review from drew2a March 21, 2022 12:45
@kozlovsky kozlovsky merged commit 10f203c into Tribler:main Mar 21, 2022
@kozlovsky kozlovsky deleted the fix/test_get_my_channel_tags branch March 21, 2022 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Unstability in test_get_my_channel_tags
2 participants