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

implement random words passphrase generator #50

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

agusu
Copy link

@agusu agusu commented Dec 9, 2021

No description provided.

Comment on lines 23 to 45
private static String randomWords(int n) {
try {
// Load english dictionary to memory
File dictionaryFile = new File("src/main/resources/words_alpha.txt");
Stream<String> dictionary = Files.lines(dictionaryFile.toPath());
List<String> wordList = dictionary.collect(Collectors.toList());
StringBuilder words = new StringBuilder();

int i = 0;
while (i < n) {
String currentWord = pickRandomItemFromList(wordList);
if (currentWord.length() > 3 && currentWord.length() < 8) {
words.append(currentWord);
if (i < n - 1) words.append(" ");
++i;
}
}

return words.toString();
} catch (IOException e) {
return RandomStringUtils.randomAlphanumeric(18);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you trying to do this every time you are requesting the words? generate() and generate(int) suggest that.

This is very inefficient.

Instead I would:

  1. Apply the singleton pattern to this class (https://refactoring.guru/design-patterns/singleton/java/example)
  2. Move all the methods to instance methods
  3. Ensure that we are loading the dictionary only once, when neeed
  4. Ensure that we are logging how much time does the dictionary to be loaded

@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

88.9% 88.9% Coverage
0.0% 0.0% Duplication

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.

2 participants