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");
}
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");
}