fullobi.blogg.se

Java basic data structures review
Java basic data structures review








  1. #JAVA BASIC DATA STRUCTURES REVIEW UPDATE#
  2. #JAVA BASIC DATA STRUCTURES REVIEW CODE#

Java 8 has added a number of very useful methods to the Map interface. As a reviewer, you should check these methods on the key and value classes to ensure you’re getting the expected behaviour. In Java, map behaviour usually relies on your implementation of equals and hashCode for the key and the value.

#JAVA BASIC DATA STRUCTURES REVIEW CODE#

Other Anti-Patterns: Iteration & ReorderingĪs with lists, if a code author has introduced a lot of sorting of, or iterating over, a map, you might want to suggest an alternative data structure. Note that this is exactly the sort of issue that should be found during a code review – hiding global constants is hard to do once their use has propagated throughout the system, but it’s easy to catch this when they’re first introduced. Or you can choose to have this method return the new Optional type. This is the principle of Information Hiding.Īlthough the updated code isn’t much shorter, you have standardised and centralised core functions – for example, you know that getting a customer who doesn’t exist is always going to give you an Exception. In addition, if later you change the customers data structure, or you move to using a distributed cache or some other technology, the changes associated with that will be restricted to the CustomerRepository class and not ripple throughout the system. That will make these other classes easier to understand, and hide any complexity of managing the map in the CustomerRepository, where it belongs. As the reviewer of this code, you’ll want to suggest hiding this data structure away and providing suitable access methods. In fact, in the code above, the author has made an error – they don’t check to see if the customer is null, so line 12 could cause a NullPointerException. Here, the order service is aware of the data structure used to store customers. The issue comes when other classes, particularly those from other parts of the system, need access to the data.

#JAVA BASIC DATA STRUCTURES REVIEW UPDATE#

This might not seem too terrible, since the CustomerUpdateService is responsible for add and update operations, and these have to ultimately change the map. The CustomerUpdateService therefore uses this map directly when adding or updating customers.

java basic data structures review java basic data structures review

In the above code, the author has chosen to simply expose the CUSTOMERS map as a global constant. The map is such a good general purpose data structure that it can be tempting to use globally accessible maps to let any class get to the data. Upsource’s side-by-side diff with the changeĪ versatile data structure that provide O(1) access to individual elements, if you’ve picked the right key. Once again, Java 8 makes this operation look so easy it might be tempting to ignore the signs: In the code above, on line 16 the twitterUsers list is always re-sorted before being returned. Lists are great if you want to stick to their default order, but if as a reviewer you see code that’s re-sorting the list, question whether a list is the correct choice. Remember that in Java 8, and languages which support more expressive searches, this might not be as obvious as a for-loop, but the problem still remains. In our case, because we always want to find a particular item by ID, it might be better to create a map of ID to Customer. But if iteration is required for a very common operation (like the example above of finding a customer by ID), there might be a better data structure to use. Because it is the most common choice, it’s sometimes used in situations it shouldn’t be.Īnti-Pattern: Too Much Searching Iterating over a list is not, in itself, a bad thing of course. Probably the most common choice for a data structure. In this post, we’re going to guide you, the code reviewer, on what to look out for – we’re going to look at examples of code and talk about “smells” that might indicate the wrong data structure was chosen or that it’s being used in an incorrect fashion. And yet it’s surprisingly easy to misuse them or select the wrong one. See previous posts from the series.ĭata structures are a fundamental part of programming – so much so it’s actually one of the areas that’s consistently taught in Computer Science courses. This is part 4 of 6 posts on what to look for in a code review.










Java basic data structures review