Skip to main content
Code Review

Return to Answer

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here here.


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here.


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here.


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

added 153 characters in body
Source Link
user34073
user34073

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here .


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK.


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK. You can read more about this here .


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

Source Link
user34073
user34073

To answer your question, yes, early returns are very good. Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK.


if(list.size() == 0){
 return 0;
}
if(list.size() == 1){
 return 1;
}

You can combine these into a single statement. Look at what you are returning - you are returning the value of list.size() in both of these conditions.

if(list.size() == 0 || list.size() == 1){
 return list.size();
}

if(elementValue < min){
min = elementValue;
}

For the most part, your indentation is very good. Just try to be a bit more consistent.

Other than these little things, this looks like very good code.

lang-java

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