C
C#2y ago
Soinagie

✅ stateful versus stateless methods?

Could someone please give me a simple example of these 2 methods as I don't really get what they do
115 Replies
Relevant
Relevant2y ago
So if you think about a method that is dependent on some value of a property of a class. Let's take a car for an example. You may have a public void Start() method. But this method may be dependent on the property public EngineType EngineType { get; set; }. This would be stateful because you can't perform that action without some knowledge about the object.
Soinagie
SoinagieOP2y ago
sorry but I don't know what {get; set} do yet
Relevant
Relevant2y ago
But let's say you have a Math class, with a method public int Add(int a, int b). This method does not depend on any other properties of the object and can be performed without knowledge of anything outside of the method. Just ignore that part, then, it's just a property
Soinagie
SoinagieOP2y ago
it needs a and b
Relevant
Relevant2y ago
But those are provided when the method is called. It doesn't depend on any current state of the Math object Let's say your Math class has a property name public int MyFavoriteNumber. The value of that property does not matter. It is not needed for the Add method
Soinagie
SoinagieOP2y ago
how can something be an object and class anyway? class is a category of somethings, object is something like it doesn't make sense
Relevant
Relevant2y ago
a class is sort of like a set of rules for a type of thing. The object is the thing itself. Like the definition of a class might include something like a Color, Make, Model, and Year. That would be the class. The object would be the actual instance of Car. So like a Red 1999 Ford Escort
Soinagie
SoinagieOP2y ago
so it does not depend on other properties of that class not object or is every class an object since it's an instance of itself?
Relevant
Relevant2y ago
no, a class is not an object Though when I said "properties of the object", I mean that the properties of an object can actually have values. Properties of a class are just definitions of properties, usually don't have values But that's kind of veering off course
Soinagie
SoinagieOP2y ago
so stateful methods get other values out of their class or somewhere else, but not from themselfs stateless methods are 'self sufficient'
Relevant
Relevant2y ago
Stateful methods are dependent on the state of the object. Like my 1999 Ford Escort example. The Start() method might perform differently for that instance of Car versus a 2022 Tesla Model 3
Soinagie
SoinagieOP2y ago
what objects? Im talking about methods
Relevant
Relevant2y ago
You're talking about methods dependent on object states
Soinagie
SoinagieOP2y ago
I'm asking about stateless and stateful methods
Relevant
Relevant2y ago
Correct in other words, you're asking about methods that don't depend on object state versus methods that do depend on object state
Soinagie
SoinagieOP2y ago
what objects? inside or outside of methods?
Relevant
Relevant2y ago
objects of the class that the method is contained in
Pobiega
Pobiega2y ago
you can't randomly store state inside a method as such. you'll need either an object or a static variable to store that data in
Soinagie
SoinagieOP2y ago
what state?
Pobiega
Pobiega2y ago
ANY state
Soinagie
SoinagieOP2y ago
methods are sets of actions, not states
Pobiega
Pobiega2y ago
exactly
Relevant
Relevant2y ago
By state, we mean the state of the object. Or to word it differently, all of the values of all of the fields and properties at a given moment. That is it's state
Pobiega
Pobiega2y ago
so you cant store state (aka data that lives longer than a single execution of a method) in them
Soinagie
SoinagieOP2y ago
so object contains all of the values of their fields?
Pobiega
Pobiega2y ago
we've been over this in your previous threads, yes
Relevant
Relevant2y ago
Properties more specifically, but yes
Soinagie
SoinagieOP2y ago
I thought propeties were values?
Relevant
Relevant2y ago
properties hold values
Soinagie
SoinagieOP2y ago
could you give me an example of stateless and stateful methods please?
Relevant
Relevant2y ago
properties and fields are both just variables, but named a little different to describe how/where they're used
Soinagie
SoinagieOP2y ago
I thought they were the same both are stored in a class
Pobiega
Pobiega2y ago
Consider these two classes and methods
public static class StatelessAdder
{
public static int Add(int a, int b)
{
return a + b;
}
}

public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
public static class StatelessAdder
{
public static int Add(int a, int b)
{
return a + b;
}
}

public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
the top one is entirely stateless. any two executions given the same values in will return the exact same value out
Soinagie
SoinagieOP2y ago
Consider what?
Pobiega
Pobiega2y ago
read the code?
Soinagie
SoinagieOP2y ago
they're both classes
Pobiega
Pobiega2y ago
try to see what it does? listen to our explanations?
Relevant
Relevant2y ago
Properties are generally public and have getter/setter. Fields are usually private. Both are defined at the class level, outside of methods
Pobiega
Pobiega2y ago
Don't get caught up on the field vs property thing here @Soinagie , its not the important part
Soinagie
SoinagieOP2y ago
what does this has to do with objects?
Pobiega
Pobiega2y ago
I'm trying to answer your original question
Relevant
Relevant2y ago
Do you see why the 2nd one would be stateful?
Soinagie
SoinagieOP2y ago
I think I already said I get that and Relevant confused me with this
Relevant
Relevant2y ago
Go back to that code example, and we'll get to objects. Do you understand why the 2nd one would be considered stateful?
Pobiega
Pobiega2y ago
its (more or less) just a rephrase of your original question
Soinagie
SoinagieOP2y ago
yeah bc it uses _value from field
Relevant
Relevant2y ago
Right So you could have two objects. One with a _value of 2, and the other with a _value of 4 And each object would get a different result from Add
Soinagie
SoinagieOP2y ago
what? _value is a variable
Relevant
Relevant2y ago
correct
Soinagie
SoinagieOP2y ago
then why did you call these objects?
Relevant
Relevant2y ago
I didn't
Soinagie
SoinagieOP2y ago
.
Relevant
Relevant2y ago
When I say object, I mean an instance of the class
Pobiega
Pobiega2y ago
because each object (instance of the class), would have its own _value
Soinagie
SoinagieOP2y ago
but _value is a variable
Pobiega
Pobiega2y ago
since _value belongs to the instance Are we seriously back to that discussion, again?
Relevant
Relevant2y ago
StatefulAdder adder1 = new StatefulAdder();
StatefulAdder adder2 = new StatefulAdder();
StatefulAdder adder1 = new StatefulAdder();
StatefulAdder adder2 = new StatefulAdder();
Pobiega
Pobiega2y ago
Have you actually started visual studio at all since last we talked?
Relevant
Relevant2y ago
we have 2 StatefulAdder objects
Soinagie
SoinagieOP2y ago
yeah
Relevant
Relevant2y ago
And they both have their own separate values of _value
Soinagie
SoinagieOP2y ago
if you're just going to be mean feel free to leave
Relevant
Relevant2y ago
one of them might be 2 and the other might be 4 So if you do:
adder1.Add(2);
adder2.Add(2);
adder1.Add(2);
adder2.Add(2);
they would give you 2 different results
Pobiega
Pobiega2y ago
Its a legitimate question to be honest. You need to actually write and run code to learn this.
Soinagie
SoinagieOP2y ago
where? you didn't assign _value to them
Pobiega
Pobiega2y ago
sure we did, thats done inside .Add
Relevant
Relevant2y ago
StatefulAdder adder1 = new StatefulAdder();
StatefulAdder adder2 = new StatefulAdder();

adder1.Add(2);
adder1.Add(2);

adder2.Add(2);
StatefulAdder adder1 = new StatefulAdder();
StatefulAdder adder2 = new StatefulAdder();

adder1.Add(2);
adder1.Add(2);

adder2.Add(2);
After this code executes, what would the values of _value be for each object? assuming _value starts at 0
Soinagie
SoinagieOP2y ago
where is here _value?
Relevant
Relevant2y ago
_value as defined above is private. It can only be modified in the method
public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
Soinagie
SoinagieOP2y ago
but you didn't name it
Relevant
Relevant2y ago
didn't name what?
Soinagie
SoinagieOP2y ago
how would code know to add specifically _value?
Relevant
Relevant2y ago
Look at the Add method _value += a; every time that method is called, it adds to _value
Soinagie
SoinagieOP2y ago
and returns it where?
Relevant
Relevant2y ago
back to the caller, but that can be ignored for now here, we can just remove that to prevent confusion:
public class StatefulAdder
{
private int _value;

public void Add(int a)
{
_value += a;
}
}
public class StatefulAdder
{
private int _value;

public void Add(int a)
{
_value += a;
}
}
Pobiega
Pobiega2y ago
return always returns to the caller btw there is no special case for that keyword
Soinagie
SoinagieOP2y ago
what is StatefulAdder adder 1 = new StatefulAdder();? how can you add a whole class to a method? I mean I know it's an object
Relevant
Relevant2y ago
add a whole class to a method? I don't know what you mean by that StatefulAdder adder1 = new StatefulAdder(); This is just creating an object of a class
Pobiega
Pobiega2y ago
its just an object reference
Relevant
Relevant2y ago
The variable name is adder1. It's type is StatefulAdder
Soinagie
SoinagieOP2y ago
you're calling an object with adder1 and then adding to it?
Relevant
Relevant2y ago
creating an object named adder1 then calling it's Add method
Pobiega
Pobiega2y ago
well in that first line of code its just creating the variable and giving it the value (being the object) the second line is what calls the Add method on it
Soinagie
SoinagieOP2y ago
wait I thought adder1 was an object since you did New
Relevant
Relevant2y ago
it is, yes
Soinagie
SoinagieOP2y ago
it is what?
Relevant
Relevant2y ago
an object
Soinagie
SoinagieOP2y ago
but here you called it a variabe
Relevant
Relevant2y ago
yep it's both
Soinagie
SoinagieOP2y ago
I can feel my brain melting...
Relevant
Relevant2y ago
an object is a variable
Pobiega
Pobiega2y ago
ok this is now an exact repeat of the 1K+ posts "what is an object" thread.
Relevant
Relevant2y ago
I think there's probably exceptions to that, with dependency injection, etc, but that isn't the point right now
Pobiega
Pobiega2y ago
Soinagie, please, for your own sake. Just start visual studio and start coding
Soinagie
SoinagieOP2y ago
so stateful methods always call an object?
Pobiega
Pobiega2y ago
write code, try to make it work, run it, test it
Soinagie
SoinagieOP2y ago
and stateless don't?
Relevant
Relevant2y ago
It's challenging to understand state if you don't have a solid grasp of classes and objects
Pobiega
Pobiega2y ago
most of the time... but of course there are exceptions... static variables exists.
Soinagie
SoinagieOP2y ago
it was at the beggining of the tutorial
Relevant
Relevant2y ago
Yeah, stateless can be called without an object, but it can be stateless and also have an object stateful always has to have an object
Soinagie
SoinagieOP2y ago
what good is it if I have no idea what anything does and why?
Pobiega
Pobiega2y ago
Because you cant learn programming without actually coding most of your understanding will come from actually experimenting with the code not from reading
Soinagie
SoinagieOP2y ago
and I can't start coding without knowing the basics
Pobiega
Pobiega2y ago
oh you can. you have analysis paralysis at the moment, that much is clear
Relevant
Relevant2y ago
stateless vs stateful methods isn't a topic that matters when you're first starting
Pobiega
Pobiega2y ago
seriously, just make an empty "hello world" console app project for now and add on that like, experiment with a single int variable, add to it, subtract from it... print it every now and then
artya
artya2y ago
What you're asking isn't really a basic
Soinagie
SoinagieOP2y ago
it was in 3rd unit of $helloworld
Soinagie
SoinagieOP2y ago
in 5th module sorry for wasting your time
Pobiega
Pobiega2y ago
go ahead and copy this code
public static class Program
{
public static void Main()
{
var adder1 = new StatefulAdder();
adder1.Add(10);

var adder2 = new StatefulAdder();
adder2.Add(10);
}
}


public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
public static class Program
{
public static void Main()
{
var adder1 = new StatefulAdder();
adder1.Add(10);

var adder2 = new StatefulAdder();
adder2.Add(10);
}
}


public class StatefulAdder
{
private int _value;

public int Add(int a)
{
_value += a;
return _value;
}
}
try changing stuff around you can always copy paste again if you "break" it too much but seriously, "seeing how far things bend" is an excellent way to learn
Soinagie
SoinagieOP2y ago
ok thank you and sorry again
Pobiega
Pobiega2y ago
don't be sorry, be daring dont overthink stuff, just get your hands dirty
Relevant
Relevant2y ago
You'll get there, just don't be scared to break things 🙂 I mean, until you are doing work on a production database at a job. Then you can be very scared mweh
Want results from more Discord servers?
Add your server