What is the best way to return


User 979490:

If I have a function that returns something, that's the best, the one below is the best way. Does it follow a better format, does it have better performance?

  private int myfunc()
{
    int value=0;
    for(int i=0;i<5;i++)
    {
        if (i==2)
            value= 2;
    }

    return value;
}

or

 private int myfunc()
{

    for(int i=0;i<5;i++)
    {
        if (i==2)
            return(2);
    }

   return (0);
}
Charlie Martin:

This is actually on the verge of an old religious question in "structured programming": should you have code with only one exit, or can you have multiple exits?

The most common, at least in the C/C++/Java world, is to return as soon as the answer is known. Doing so has a small performance hit, but not as much as it was 20 years ago.

Related


What is the best way to return

User 979490: If I have a function that returns something, that's best, the one below is the best way. Does it follow a better format, does it have better performance? private int myfunc() { int value=0; for(int i=0;i<5;i++) { if (i==2)

What is the best way to return

User 979490: If I have a function that returns something, that's the best, the one below is the best way. Does it follow a better format, does it have better performance? private int myfunc() { int value=0; for(int i=0;i<5;i++) { if (i==2

What's the best way to return the computation time?

Joseph Katzman Am I looking for the best way to return the computation time of a method? For example, I have a multiplty method and the result of the expansion is an integer value, but what if my user wants to get both (result and time) or just the int result?

What's the best way to check the results of a return?

sk I set up a function in a shell script that checks if the folder exists, if it doesn't, try to create it and if the folder can't be created (for example if the user doesn't have the correct permissions) return 1 Then, I check this "return value", but I don't

What is the best way to return a result or an error

Lezak Suppose I have this method that calls an API and returns some results: public async Task<List<ClientModel>> GetClients() { var result = new List<ClientModel>(); try { results = await myHttpCLient.GetFromJsonAs

What's the best way to check the results of a return?

sk I set up a function in a shell script that checks if the folder exists, if it doesn't, try to create it and if the folder can't be created (for example if the user doesn't have the correct permissions) return 1 Then, I check this "return value", but I don't

What is the best way to return a list of data from a controller

help I have one that List<MyModel>I return from the controller toview Should I create a new class to hold it Listand return an object of the new class or I can return it Listitself.. Which is better programming practice? Abimanuel Guy It depends on what your e

What if the best way to return an option type via a WCF service

the rest I have a WCF method that searches the database for a record and returns Some(object) if the record exists and None if it doesn't. As I can see, I cannot call the method returned Type optionvia WCF (I get the exception). What's the best way to design a

What's the best way to return a pair of values in Java?

Graham: This is a small problem because I can easily take a pair of classes to do the job. I don't really want to do this, I feel like there should be some simple, built-in, Java-like way to return two values. What is the best and easiest way for you guys? arr

What's the best way to return lists and integers from a function?

Dkisiale I have a function that reads a csv that gives me a list. Then I find the max of that list and return that list and max from the function. I would like to know the best way to do this to make my code more efficient and readable. I'm still a bit new to

What is the best way to return multiple values in a promise chain

Bowes I did realize that when a non-promise is returned in a .then() handler, it is immediately passed to the next handler, and if it is a returning promise, execution is suspended to resolve the promise before it is passed under a handler. I also know that a

What's the best way to return lists and integers from a function?

Dkisiale I have a function that reads a csv that gives me a list. Then I find the max of that list and return that list and max from the function. I would like to know the best way to do this to make my code more efficient and readable. I'm still a bit new to