C
C#3mo ago
Weenie

Trying to learn how to use return variables, is this a good practice exercise?

class Program { static int MyMethod(int x, int y) { int xy = x + y; return xy; } public static void Main(string[] args) { int answer = MyMethod(3, 4); Console.WriteLine(answer); } }
6 Replies
Angius
Angius3mo ago
Well, it does make use of the return So... sure
Buddy
Buddy3mo ago
Although I personally wouldn't suggest to make methods like Add without it being special. also way shorter to just write 3 + 4 instead of Add(3, 4)
oke
oke3mo ago
they only used that because theyre trying to learn how to use return all you need is return with the correct type that is required for the method if its void, then just return with no variable what you did is correct, but logically not best practice (for the reason to make a method that just does addition) something a little more complex, but might help with visualizing how return works:
internal class Program
{
public static void Main()
{
MyClass myClassInstance = MyClass.GetNewInstance();
Console.WriteLine(myClassInstance.RandomInt());
}
}

internal class MyClass
{
// This method is non-static, so you have to define an instance of MyClass to use it
public int RandomInt()
{
Random myRandom = GetNewRandom();
return myRandom.Next(); // Get a random int
}

public Random GetNewRandom()
{
// Get a new random number generator
return new Random();
}

// Static means that this method can be accessed without a class instance (you do not have to use `new MyClass()` to access it, just call it with `MyClass.GetNewInstance()`
public static void MyClass GetNewInstance()
{
return new MyClass();
}
}
internal class Program
{
public static void Main()
{
MyClass myClassInstance = MyClass.GetNewInstance();
Console.WriteLine(myClassInstance.RandomInt());
}
}

internal class MyClass
{
// This method is non-static, so you have to define an instance of MyClass to use it
public int RandomInt()
{
Random myRandom = GetNewRandom();
return myRandom.Next(); // Get a random int
}

public Random GetNewRandom()
{
// Get a new random number generator
return new Random();
}

// Static means that this method can be accessed without a class instance (you do not have to use `new MyClass()` to access it, just call it with `MyClass.GetNewInstance()`
public static void MyClass GetNewInstance()
{
return new MyClass();
}
}
DΣX
DΣX3mo ago
i try to keep my methods short. but when i have the need for a result, i call that variable 'result' in fact when i start writing a method it became a standard pattern to write this
... FunctionName()
{
var result = <default result>;

return result;
}
... FunctionName()
{
var result = <default result>;

return result;
}
and then just add modifications to result between the initialization and the return this way you will have a valid result no matter what in fact it also helps when adding strange if else blocks that are sometimes making the methods harder to understand so instead of
... FunctionName()
{
if (<condition>)
{
return <custom result>;
}
else
{
return <default result>;
}
}
... FunctionName()
{
if (<condition>)
{
return <custom result>;
}
else
{
return <default result>;
}
}
you can write
... FunctionName()
{
var result = <default result>;

if (<condition>)
{
result = <custom result>
}

return result;
}
... FunctionName()
{
var result = <default result>;

if (<condition>)
{
result = <custom result>
}

return result;
}
ps: i know these are simple cases and especially the first one can be written soooo much shorter. these are just meant to be an example
Angius
Angius3mo ago
That said, there's a case to be made for Fail Fast, which I personally subscribe to
if (user is null) return NotFound();
// user .... do stuff
return Ok(user);
if (user is null) return NotFound();
// user .... do stuff
return Ok(user);
OkOk
OkOk3mo ago
Seems like multiple very hardened responses to a beginner-question 😁