[WIP] Useless code removal and code cleanup #5

Manually merged
regar42 merged 5 commits from fixUnneededCode into lastDijsktraClasses 2017-12-10 16:48:34 +00:00
Collaborator
No description provided.
antograb was assigned by regar42 2017-12-10 10:00:20 +00:00
Author
Collaborator

From Box.java#L14, I don't get why you create a List from an ArrayList :
private List neighbourList = new ArrayList() ;

Fixed in fda73a20.

From [Box.java#L14](https://git.regar42.fr/antograb/TpMazeJava/src/branch/fixUnneededCode/src/maze/Box.java#L14), I don't get why you create a List from an ArrayList : private List<Box> neighbourList = new ArrayList<Box>() ; Fixed in fda73a20.
Owner

Here I create an ArrayList from the List class. ArrayList inherits from List, and this is indeed useless since I could just write :
private ArrayList neighbourList = new ArrayList() ;

This would also prevent from importing both List and ArrayList packages, since we only need ArrayList.

Here I create an ArrayList from the List class. ArrayList inherits from List, and this is indeed useless since I could just write : private ArrayList<Box> neighbourList = new ArrayList<Box>() ; This would also prevent from importing both List and ArrayList packages, since we only need ArrayList.
Author
Collaborator

Replaced

this.hashPi = hashPi ;
:
public Pi(Hashtable<VertexInterface, Integer> hashPi) {
this.hashPi = hashPi ;
}
by
this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
:
public Pi(Hashtable<VertexInterface, Integer> hashPi) {
this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
}

In

this.hashPi = hashPi ;
you take the object hashPi passed as an argument and put its reference in this.hashPi. It doesn't create a new object. This could be problematic if hashPi, for some reasons, is changed and we don't want to affect hashPi in the Pi instance.

I don't use the clone() method because it only does a shallow copy of the object : it creates a new object and for each of its arguments, sets the reference of the corresponding argument from the original object. Moreover, the implementation of clone() changes depending of the class, making it sometimes do a shallow copy, sometimes a deep copy. For such reasons, clone() is considered as a bad practice in Java programming.

This change, however, doesn't affect

this.maze = maze ;
since in this we case we really want to keep a reference on the passed object (and not to create a new independant maze).

Replaced https://git.regar42.fr/antograb/TpMazeJava/src/commit/5f44829371d94c84e03ddce09fb077f059e8f608/src/dijkstra/Pi.java#L11 : public Pi(Hashtable<VertexInterface, Integer> hashPi) { this.hashPi = hashPi ; } by https://git.regar42.fr/antograb/TpMazeJava/src/commit/ebb91b01b388f6c07f29f2d5fcb3a48402011faf/src/dijkstra/Pi.java#L11 : public Pi(Hashtable<VertexInterface, Integer> hashPi) { this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi); } In https://git.regar42.fr/antograb/TpMazeJava/src/commit/5f44829371d94c84e03ddce09fb077f059e8f608/src/dijkstra/Pi.java#L11 you take the object `hashPi` passed as an argument and put its reference in `this.hashPi`. It doesn't create a new object. This could be problematic if `hashPi`, for some reasons, is changed and we don't want to affect `hashPi` in the `Pi` instance. I don't use the `clone()` method because it only does a shallow copy of the object : it creates a new object and for each of its arguments, sets the reference of the corresponding argument from the original object. Moreover, the implementation of `clone()` changes depending of the class, making it sometimes do a shallow copy, sometimes a deep copy. For such reasons, `clone()` is considered as a bad practice in Java programming. This change, however, doesn't affect https://git.regar42.fr/antograb/TpMazeJava/src/commit/5f44829371d94c84e03ddce09fb077f059e8f608/src/maze/Box.java#L21 since in this we case we really want to keep a reference on the passed object (and not to create a new independant maze).
regar42 changed title from WIP: Useless code removal and code cleanup to [WIP] Useless code removal and code cleanup 2017-12-10 11:34:37 +00:00
regar42 closed this pull request 2017-12-10 16:48:47 +00:00
regar42 self-assigned this 2017-12-10 16:49:08 +00:00
regar42 deleted branch fixUnneededCode 2017-12-10 16:49:14 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: antograb/TpMazeJava#5