IMHO, the best option is to return an Optional<RollingStock>, like the foillowing:
Copypublic Optional<RollingStock> getHeadPoint() {
if (!train.isEmpty()) {
// or even Optional.ofNullable, if you are not sure
// whether train.get(0) is null or not
return Optional.of(train.get(0));
} else {
return Optional.empty();
}
}
Assuming train is a collection, as an alternative to manual wrapping the value into an Optional you could use Stream API:
Copypublic Optional<RollingStock> getHeadPoint() {
return train.stream()
.findFirst();
}
In some cases using inline train.stream().findFirst() may be more preferable than wrapping it into a separate method.
Once you already modified your method getHeadPoint to return Optional<RollingStock> you can use it as follows:
Copy// ...
RollingStock headPoint = getHeadPoint().orElse(yourDefaultRollingStock);
// or
RollingStock headPoint = getHeadPoint().orElseGet(aMethodGettingYourDefaultRollingStock());
// or
RollingStock headPoint = getHeadPoint().orElseThrow(() -> new Exception("The train is empty!"));
// or
getHeadPoint().ifPresent(headPoint -> doSomethingWithHeadPoint(headPoint));
Answer from ETO on Stack OverflowIMHO, the best option is to return an Optional<RollingStock>, like the foillowing:
Copypublic Optional<RollingStock> getHeadPoint() {
if (!train.isEmpty()) {
// or even Optional.ofNullable, if you are not sure
// whether train.get(0) is null or not
return Optional.of(train.get(0));
} else {
return Optional.empty();
}
}
Assuming train is a collection, as an alternative to manual wrapping the value into an Optional you could use Stream API:
Copypublic Optional<RollingStock> getHeadPoint() {
return train.stream()
.findFirst();
}
In some cases using inline train.stream().findFirst() may be more preferable than wrapping it into a separate method.
Once you already modified your method getHeadPoint to return Optional<RollingStock> you can use it as follows:
Copy// ...
RollingStock headPoint = getHeadPoint().orElse(yourDefaultRollingStock);
// or
RollingStock headPoint = getHeadPoint().orElseGet(aMethodGettingYourDefaultRollingStock());
// or
RollingStock headPoint = getHeadPoint().orElseThrow(() -> new Exception("The train is empty!"));
// or
getHeadPoint().ifPresent(headPoint -> doSomethingWithHeadPoint(headPoint));
You could define a TrainIsEmptyException and throw that when the train is empty. Make it a checked Exception. Or you can just return null.
object oriented - Is it better to return NULL or empty values from functions/methods where the return value is not present? - Software Engineering Stack Exchange
java - Returning null in a method whose signature says return int? - Stack Overflow
Method return Optional or null?
Don’t return NULL
Videos
I am a learning java, btw why is used the return null;?
StackOverflow has a good discussion about this exact topic in this Q&A. In the top rated question, kronoz notes:
Returning null is usually the best idea if you intend to indicate that no data is available.
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned.
Additionally, returning a null will result in a null exception if you attempt to access members in the object, which can be useful for highlighting buggy code - attempting to access a member of nothing makes no sense. Accessing members of an empty object will not fail meaning bugs can go undiscovered.
Personally, I like to return empty strings for functions that return strings to minimize the amount of error handling that needs to be put in place. However, you'll need to make sure that the group that your working with will follow the same convention - otherwise the benefits of this decision won't be achieved.
However, as the poster in the SO answer noted, nulls should probably be returned if an object is expected so that there is no doubt about whether data is being returned.
In the end, there's no single best way of doing things. Building a team consensus will ultimately drive your team's best practices.
In all the code I write, I avoid returning null from a function. I read that in Clean Code.
The problem with using null is that the person using the interface doesn't know if null is a possible outcome, and whether they have to check for it, because there's no not null reference type.
In F# you can return an option type, which can be some(Person) or none, so it's obvious to the caller that they have to check.
The analogous C# (anti-)pattern is the Try... method:
public bool TryFindPerson(int personId, out Person result);
Now I know people have said they hate the Try... pattern because having an output parameter breaks the ideas of a pure function, but it's really no different than:
class FindResult<T>
{
public FindResult(bool found, T result)
{
this.Found = found;
this.Result = result;
}
public bool Found { get; private set; }
// Only valid if Found is true
public T Result { get; private set;
}
public FindResult<Person> FindPerson(int personId);
...and to be honest you can assume that every .NET programmer knows about the Try... pattern because it's used internally by the .NET framework. That means they don't have to read the documentation to understand what it does, which is more important to me than sticking to some purist's view of functions (understanding that result is an out parameter, not a ref parameter).
So I'd go with TryFindPerson because you seem to indicate it's perfectly normal to be unable to find it.
If, on the other hand, there's no logical reason that the caller would ever provide a personId that didn't exist, I would probably do this:
public Person GetPerson(int personId);
...and then I'd throw an exception if it was invalid. The Get... prefix implies that the caller knows it should succeed.
int is a primitive, null is not a value that it can take on. You could change the method return type to return java.lang.Integer and then you can return null, and existing code that returns int will get autoboxed.
Nulls are assigned only to reference types, it means the reference doesn't point to anything. Primitives are not reference types, they are values, so they are never set to null.
Using the object wrapper java.lang.Integer as the return value means you are passing back an Object and the object reference can be null.
Change your return type to java.lang.Integer . This way you can safely return null
I am currently working on a SDK/library that has various methods(a lot actually) that are not always guaranteed to return something(based on availability, case,...). I did a lot of research and i just can't decide on which design is more user friendly & stable? So Reddit, whats your opinion on this?
I’m planning on delivering a tech talk to my team on the pitfalls of explicitly returning nulls in production code, as opposed to using optionals where the language supports it or throwing exceptions when the value is expected to be present.
To make sure I’m not presenting an overly biased view, and to avoid getting blind-sided if someone raises a point I hadn’t considered, I want to hear examples of times you would actually prefer to explicitly return null.
Edit: Since some were curious and I neglected to specify, our team works predominantly in Java so we do have the Optional interface available to us. I have also worked with Go a bit and tbf I did like the ability to have multiple return values in the case of errors etc. I also don’t mind how Swift/Kotlin handle optionals and unwrapping them, I believe they handle it in a similar way.
I was trying to test it with this code:
import java.util.Objects;
public class Main {
public static void main (String args[]) {
String a = myMethod();
System.out.println(Objects.isNull(a));
}
public static String myMethod() {
}
}But a compilation error is raised before I can see the outcome.
Just out of curiosity