Questions tagged [return-type]
The return-type tag has no usage guidance.
49
questions
10
votes
8
answers
4k
views
What is the purpose of enclosing all return values and arguments of a method in separate classes?
I've seen such a convention. Whenever a public method is declared, two classes are also defined that enclose its return value and parameters like this:
public MethodNameReturnDTO MethodName(...
2
votes
2
answers
260
views
"Returns true if changed" pattern [closed]
The Collection interface in Java has, among others, these methods:
boolean add(E e)
boolean addAll(Collection<? extends E> c)
boolean remove(Object o)
boolean removeAll(Collection<?> c)
...
1
vote
2
answers
104
views
Good OO design pattern for variant return type needs
I've got a validation function something like this (pseudo-code):
public function validate(Thing thing) {
if (thing.a != "a") {
return false; // most of the time
}
if (...
8
votes
9
answers
8k
views
Returning true/nothing instead of true/false?
Is it OK to have a function that returns true/nothing instead of true/false?
My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language.
The subjects ...
1
vote
1
answer
1k
views
UML - Overload operations with different parameters and a different return type
I would like to know if it is allowed in the UML to specify a different return type for overloaded operations. This is possible for methods in Java:
public class C
{
public int addOne(int a) {
...
0
votes
4
answers
142
views
Is there a pattern or best practice for returning dynamically typed data?
I have a method that returns the percentage change of a data in a certain period. After calling that method, i need to know if in the current period the data increased, decreased or is the same when ...
5
votes
2
answers
478
views
Class that returns multiple unrelated values, are there better way to accomplish this?
I have a "tracker" class. This class tracks objects from a given input image. But in addition to this, there is another parameter that corresponds to a value used in generating the image. The ...
0
votes
1
answer
1k
views
C# Unit Testing: Mock a Return Type
What is the best way to Mock a return type if we don't care about the actual state of the object.
For example, I am Mocking a method whose return type is: IEnumerable<Document>
Now, I can add ...
3
votes
2
answers
231
views
Callback pattern - return value confusion
I have a ConstraintsResolver class which resolves a queue of Constraints. This is done when a ConstraintsResolver object calls meetConstraint() on a Constraint one.
Most of meetConstraint() ...
4
votes
3
answers
1k
views
In languages without exception-handling, should error codes be returned from the function or in function parameters?
I'm writing a CSV parsing library in C and am considering whether to express error codes as return values or as parameters passed by reference.
For example, here are the signatures for the function ...
8
votes
3
answers
4k
views
What do you need Covariant Return Types for?
In this article, it is written that in C# 8.0 new feature of convariant return type will be included as the community asking for this feature for quite a long time.
Can anybody explain why and when ...
59
votes
6
answers
37k
views
Why do many functions that return structures in C, actually return pointers to structures?
What is the advantage of returning a pointer to a structure as opposed to returning the whole structure in the return statement of the function?
I am talking about functions like fopen and other low ...
50
votes
9
answers
23k
views
Why does Java have `void` methods?
Does / why does Java need to have void methods? Reference:
Any method declared void doesn't return a value.
As far as I can think, every use of void would be better served by returning a status ...
2
votes
3
answers
21k
views
Converting Method to Return Generic Type
I would like to ask how to convert a method so that it can return any type depending on what happens within it.
What I have is the start of an application which will visit a number of webpages (and ...
1
vote
2
answers
187
views
Should I return true in a method that casts null from one object type to another?
I'm writing a very simple extension method that attempts to cast objects from one type to another. The intent of having this method is very similar to Int32.TryParse(string, out int), which allows the ...
2
votes
1
answer
596
views
In a JavaScript method signature what is meant by a return type of `typeof blahBlahBlah`?
I've seen an API list methods with both of the following signatures:
methodA(...) : ReturnType
methodB(...) : typeof ReturnType
I understand the first but not the second.
My question comes ...
2
votes
3
answers
2k
views
Difference between passing by values/address and returning a values through functions?
According to a book I read you can pass variables from one function to another by passing by value/address. When you pass by address it will no longer preserve the variable if it is changed in a ...
27
votes
7
answers
5k
views
How to return warnings alongside result of calculation to caller of a Java method? [duplicate]
I have a class, lets call it Calculator, with a method like this:
public double[] performCalculation(double[] someInData)
This method can generate a number of non fatal warnings (represented as an ...
-3
votes
1
answer
3k
views
Is it possible for $this to return a __construct?
I am currently working on my PHP skills and at the moment I'm working with classes. What I want to do is to create a class for which I can echo the object. I have the following example:
class ...
3
votes
1
answer
138
views
Within an interface specified by a core component, should it request IReadOnlyCollection or IEnumerable?
In thinking about the principle of "be conservative in what you send and liberal in what you accept", I generally try to make my methods receive IEnumerable parameters, but emit a IReadOnlyCollection (...
0
votes
2
answers
320
views
"Separate Query from Modifier" and return values [closed]
I've heard it said that you should keep query and modifier functions separate from one another, and I agree with the sentiment. When trying to apply it, I often run into two issues/questions:
What, ...
4
votes
1
answer
2k
views
Lambda Return Type Inference
Writing my own JVM compiler, I am facing a giant problem that I am desperately unable to solve:
Lambda Return Type Inference
1. Overview of the compiler lifecycle
More specifically, the order in ...
2
votes
2
answers
501
views
Lesser of two evils regarding return types
I'm implementing a system in PHP where values are represented with objects that implement an Amount interface. I'm building two different implementations of this interface, one using a simple integer ...
4
votes
4
answers
4k
views
Quit a program in main or in function?
Lets say I have a void method/function to check the arguments of the program:
void check(int argc, String argv){
//some irrelevant code
}
In this function I have some checks to validate the ...
31
votes
7
answers
8k
views
Should I validate a method call's return value even if I know that the method can't return bad input?
I'm wondering if I should defend against a method call's return value by validating that they meet my expectations even if I know that the method I'm calling will meet such expectations.
GIVEN
User ...
7
votes
2
answers
7k
views
Using subroutines to return values?
I'm currently learning FORTRAN (I am familiar with MatLab) and I am very confused about the point of subroutines. Why would anyone use them as opposed to functions. Also, how is it that they can ...
-1
votes
1
answer
453
views
What's the returning a value in Java?
public void warning(){
System.out.println("Watch out!");
}
public void warning(){
System.out.println("Watch out!");
return;
}
We must use "return" statement with void methods. Bot of ...
6
votes
1
answer
3k
views
Should I declare the most specific type as return type of method?
In Android I can put only ArrayList of Parcelable objects in Intent:
Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value);
I can't put in there a List<? ...
6
votes
2
answers
32k
views
Java: how can I make the return type, of an inherited method in a subclass, the same as the subclass?
I am rather inexperience in Java, and I'm having a problem in forming a subclass of a class I have created. The class I have made, called Vector2D, contains methods, such as add(Vector2D addend), that ...
11
votes
4
answers
26k
views
When should a method of a class return the same instance after modifying itself?
I have a class that has three methods A(), B() and C(). Those methods modify the own instance.
While the methods have to return an instance when the instance is a separate copy (just as Clone()), I ...
1
vote
3
answers
16k
views
Make Return Type an Interface - Problem with Initialization
I would like to make the return type of my method an interface rather than a class for similar reasons stated in c# List or IList, however I am having trouble figuring out how to initialize the ...
1
vote
2
answers
250
views
Pros and cons for choosing between different return types
This is in context of a client-server architecture, although I don't think the architectural setting is needed to put up the problem.
I have a function whose output can be classified into different ...
36
votes
9
answers
12k
views
How to name a method that both performs a task and returns a boolean as a status?
If there is a method
bool DoStuff() {
try {
// doing stuff...
return true;
}
catch (SomeSpecificException ex) {
return false;
}
}
should it rather be called ...
3
votes
1
answer
281
views
For instance methods, would always returning self or this, instead of using a void return type, be a clean coding style? [closed]
I just came across this article, and in particular, this answer. Essentially they're talking about returning self from instance methods to allow for method chaining. That being said, one of the ...
0
votes
1
answer
220
views
If a variable containing a function gave the function's return instead of a reference to the function, what would an array of functions return? [closed]
I'm creating a prototype scripting language similar to something like Javascript. In my language, every single reference to a variable is actually a function call (a getter function), whether it has ...
3
votes
3
answers
4k
views
What's the best way to retrieve a value and a status [closed]
Given that all else is equal, and there are no coding standards defining the best approach, what would be the recommended way in C++ to check that a value exists and return it if it does?
For ...
6
votes
2
answers
8k
views
Why a function returning by address can not be a lvalue?
Why is it not possible to set a function returning an address while it is possible to set a function that returns a reference.
int* returnByAddress()
{
int x = 20;
return &x;
}
int& ...
7
votes
1
answer
14k
views
Return values for CRUD methods?
I'm writing a basic web API in Java that does what almost all others do: take input, validate it, then do CRUD operations on the DB. I've written several APIs before, and I've pretty much already ...
6
votes
2
answers
5k
views
Should I return Collection or ImmutableCollection from a method?
When writing an API in Java, returning an immutable collection of some sort, I've got the option of returning Collection (or List, Map, etc) from the method, or guava's ImmutableCollection (or ...
5
votes
3
answers
2k
views
Intersection of geometric entities
I was trying to design a small C++ geometric API for learning purposes, but came across a problem when dealing with intersections of geometric entities. For example, the intersection of a line and a ...
3
votes
6
answers
2k
views
use areFoo or isFoo?
I have never seen the use of "are" for boolean methods, but the use of "is" is very common.
When I want to use "are" is usually because I am passing multiple variables, or a list of objects.
I ...
2
votes
1
answer
139
views
php return values
I have a codeigniter app and in my model, I always return true or false for all functions, and if I have data that needs to be passed, I also set a property that contains my data.
The only trouble ...
1
vote
1
answer
886
views
Wrapping Primitives to Enable Returning null -- Bad Practice?
I am frequently tempted to wrap integers, etc, solely for the purpose of writing methods that can return null. Negative 1 can work in many cases, but too often (especially in sound) it's a valid ...
1
vote
3
answers
2k
views
When should one use "out" parameters?
In Objective-C, there are several methods like initWithContentsOfFile:encoding:error: where one passes in a reference to an NSError object for the error: parameter. In this example, the value of the ...
4
votes
2
answers
5k
views
Naming methods that do the same thing but return different types
Let's assume that I'm extending a graphical file chooser class (JFileChooser).
This class has methods which display the file chooser dialog and return a status signature in the form of an int: ...
3
votes
4
answers
2k
views
Is conditional return type ever a good idea?
So I have a method that's something like this:
-(BOOL)isSingleValueRecord
And another method like this:
-(Type)typeOfSingleValueRecord
And it occurred to me that I could combine them into ...
-1
votes
3
answers
15k
views
function works fine without return value in c++ [closed]
I forgot to write return 'a'; in the show function and return 0; in main function but it works fine in Code::Blocks.
#include <iostream>
using namespace std;
char show()
{
cout<<"...
20
votes
4
answers
7k
views
Declaring return variables in c# methods vs returning the value directly
In a debate regarding return variables, some members of the team prefer a method to return the result directly to the caller, whereas others prefer to declare a return variable that is then returned ...
17
votes
9
answers
4k
views
Which statically typed languages support intersection types for function return values?
Initial note:
This question got closed after several edits because I lacked the proper terminology to state accurately what I was looking for. Sam Tobin-Hochstadt then posted a comment which made ...