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

added solution #1590

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

Conversation

Oleksii-Melashchenko
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

It looks like there are a few areas that need some attention, particularly with the way keys are being checked and compared. Using == for key comparison might lead to unexpected behavior since it doesn't check for logical equality. Extracting repeated logic into a private method is also a great idea to keep your code clean and maintainable. Keep up the good work, and with a few tweaks, your code will be even better! 👍 Remember, clean and maintainable code is just as important as working code!

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

@Override
public void put(K key, V value) {
for (int i = 0; i < currentSize; i++) {

Choose a reason for hiding this comment

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

There is repeating code for checking if the key is already in the storage. Consider extracting this logic into a private method to adhere to the DRY principle.

}

@Override
public V get(K key) {
for (int i = 0; i < currentSize; i++) {

Choose a reason for hiding this comment

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

The same repeating code for checking if the key is already in the storage is found here as well. It's a good practice to create a private method for such repeated logic.

@Override
public void put(K key, V value) {
for (int i = 0; i < currentSize; i++) {
if (key == null && keys[i] == null) {

Choose a reason for hiding this comment

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

The comparison of keys using '==' does not account for the potential logical equality of key objects. It is better to use 'equals' method with a null check.

}

for (int i = 0; i < currentSize; i++) {
if (keys[i] != null && keys[i].equals(key)) {

Choose a reason for hiding this comment

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

The null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 19-23).

}

@Override
public V get(K key) {
for (int i = 0; i < currentSize; i++) {
if (key == null && keys[i] == null) {

Choose a reason for hiding this comment

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

As mentioned before, consider using 'equals' method with a null check for comparing keys instead of '==', to ensure logical equality is considered.

}

for (int i = 0; i < currentSize; i++) {
if (keys[i] != null && keys[i].equals(key)) {

Choose a reason for hiding this comment

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

Similar to the previous comment, the null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 54-58).

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on the Pull Request! All the feedback provided is related to minor code style issues, such as removing unnecessary empty lines. These small tweaks don't impact the functionality or security of your code, so your PR is approved! 🎉 Remember to keep an eye on these details in the future for even cleaner code. Keep up the good work! 👍

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

@@ -1,19 +1,54 @@
package core.basesyntax.impl;

import core.basesyntax.Storage;
import java.util.Objects;

public class StorageImpl<K, V> implements Storage<K, V> {

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the class definition.

private final V[] values;
private int currentSize;

public StorageImpl() {

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the constructor.

values = (V[]) new Object[MAX_ARRAY_SIZE];
currentSize = 0;
}

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the put method.

currentSize++;
} else {
throw new RuntimeException("Storage is full");
}
}

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the get method.

int index = findKeyIndex(key);
if (index != -1) {
return values[index];
}
return null;
}

@Override

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the size method.

return currentSize;
}

private int findKeyIndex(K key) {

Choose a reason for hiding this comment

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

Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the findKeyIndex method.

Comment on lines 47 to 50
for (int i = 0; i < currentSize; i++) {
if ((keys[i] == null && key == null) || Objects.equals(keys[i], key)) {
return i;
}

Choose a reason for hiding this comment

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

Consider using the Objects.equals method directly without checking for null separately, as Objects.equals already handles nulls. This will make the code cleaner.

public StorageImpl() {
keys = (K[]) new Object[MAX_ARRAY_SIZE];
values = (V[]) new Object[MAX_ARRAY_SIZE];
currentSize = 0;

Choose a reason for hiding this comment

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

Suggested change
currentSize = 0;

private static final int MAX_ARRAY_SIZE = 10;
private final K[] keys;
private final V[] values;
private int currentSize;

Choose a reason for hiding this comment

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

Suggested change
private int currentSize;
private int size;


private int findKeyIndex(K key) {
for (int i = 0; i < currentSize; i++) {
if ((keys[i] == null && key == null) || Objects.equals(keys[i], key)) {

Choose a reason for hiding this comment

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

Suggested change
if ((keys[i] == null && key == null) || Objects.equals(keys[i], key)) {
if (keys[i] == key || Objects.equals(keys[i], key)) {

don't use Objects

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.

3 participants