Skip to main content
Code Review

Return to Answer

Updated as per comment from Eric
Source Link
Stephen Rauch
  • 4.3k
  • 12
  • 24
  • 36

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger, it's rather significant.

public Object peek() {
 breakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 breakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void breakIfTopIsNull() {
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger it's rather significant.

public Object peek() {
 breakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 breakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void breakIfTopIsNull() {
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger, it's rather significant.

public Object peek() {
 breakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 breakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void breakIfTopIsNull() {
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger it's rather significant.

public Object peek() {
 BreakIfTopisNullbreakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 BreakIfTopIsNullbreakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void BreakIfTopIsNullbreakIfTopIsNull(){
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger it's rather significant.

public Object peek() {
 BreakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 BreakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void BreakIfTopIsNull(){
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger it's rather significant.

public Object peek() {
 breakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 breakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void breakIfTopIsNull(){
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}
Source Link

One thing that you could try is moving the null-check for peek and pop to its own method. In this way you can eliminate the if/else blocks and reduce indentation which is better for reading. Also the name gives you a quick indication what is going to happen on the error without the need to analyse the whole statement in the if. For this code it's a very small improvement, but if the code blocks get larger it's rather significant.

public Object peek() {
 BreakIfTopisNull();
 return top.item;
}
public Object pop() {
 Node currentNode = top;
 
 BreakIfTopIsNull()
 Node nextNode = top.next;
 top = null;
 top = nextNode;
 
 count--;
 return currentNode.item;
}
private void BreakIfTopIsNull()
{
 if(top == null) 
 throw new NoSuchElementException("Underflow Exception");
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /