University Assignment: Code Review

Welcome back to another code review! In this episode, we’ll be taking a look at a university assignment involving writing a hashmap. This assignment was submitted by a student named Tim. While the requirements for this assignment are a bit unusual, it presents a unique opportunity to explore the implementation of a real-world university assignment. So, let’s dive in and review Tim’s code.

University Assignment: Code Review
University Assignment: Code Review

Hashmap Implementation

The code begins with the implementation of a hashmap, which is used to store key-value pairs. The hashmap class is defined with an array of 26 objects, each representing a slot in the hashmap. Each slot has two properties: object to store the value and status to track if the slot is occupied, never used, or marked as tombstone.

To improve the code readability, I would suggest making a few changes to the implementation of the hashmap class. Instead of using PascalCase for function names like CommandGet and CommandAdd, it would be better to use camelCase. Also, the constructor can be simplified by initializing the status property in the declaration itself.

struct HashTable {
    string object;
    string status = "never used";
};

Additionally, I would recommend renaming the place variable to index for better clarity.

Adding and Deleting from the Hashmap

The code goes on to define the CommandGet and CommandAdd functions, which are responsible for adding and deleting values in the hashmap, respectively. The CommandGet function extracts the first character from the user input and returns it as the command type. It then proceeds to look for the key in the hashmap using the provided hash function.

Further reading:  Techal: Revolutionize Your Game Engine with Window Abstraction and GLFW

The hash function takes the last character of the key and converts it into an index value by subtracting the ASCII value of zero and performing modulo 26. This index value is used to check if the slot is already occupied. If it is, the code recursively looks for an empty slot to resolve the collision. The code then adds the key-value pair to the hashmap by updating the status and copying the value into the corresponding object.

On the other hand, the CommandDelete function follows a similar process of extracting the first character and searching for the key in the hashmap. If found, the code updates the status of the slot to “tombstone” indicating that it can be reused.

While the code works as expected, I would suggest simplifying the implementation by combining the search and delete operations into a single function. This function can return an index if the key is found, allowing for a more efficient implementation.

Testing and Output

To test the hashmap implementation, the code provides an Output function that prints the values stored in the hashmap. It iterates over all the slots and checks if the status is “occupied”. If it is, the object value is printed with a space separating each value.

I would recommend refactoring the Output function to use a more modern range-based for loop to iterate over the hashmap slots.

Conclusion

In conclusion, Tim’s implementation of the hashmap assignment is functional and meets the requirements. However, there are a few areas where the code could be improved for readability and efficiency. By simplifying the code and making some small changes, the implementation could be more concise and maintainable. Overall, I commend Tim on completing the assignment and look forward to seeing future improvements in their coding journey.

Further reading:  Beginner Game Programming: Leveraging Variables in C++

FAQs

Q: Can you simplify the implementation of the hashmap class?

Yes, by using camelCase for function names and initializing the status property in the declaration itself, the implementation can be made more concise and readable.

Q: How can the CommandGet and CommandDelete functions be improved?

By combining the search and delete operations into a single function that returns the index of the found key, the implementation can be simplified and made more efficient.

Q: What suggestions do you have for improving the Output function?

Using a range-based for loop instead of a traditional for loop would make the code more readable and modern.

Q: Can you provide an overview of the code?

Certainly! The code implements a hashmap class to store key-value pairs. It provides functions for adding and deleting values, as well as an output function to print the stored values. The implementation could be improved by simplifying the code, using more modern features like range-based for loops, and combining certain functions for better efficiency.


I hope you found this code review insightful. If you have any further questions or need clarification on any aspects of the code, feel free to ask. Remember, coding is a continuous learning process, and every opportunity to review and improve your code contributes to your growth as a developer. Keep up the great work, and happy coding!

Visit Techal to explore more informative articles.