✅ 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
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.sorry but I don't know what
{get; set}
do yetBut 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 propertyit needs a and b
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 methodhow can something be an object and class anyway? class is a category of somethings, object is something
like it doesn't make sense
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
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?
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
so stateful methods get other values out of their class or somewhere else, but not from themselfs
stateless methods are 'self sufficient'
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 3what objects?
Im talking about methods
You're talking about methods dependent on object states
I'm asking about stateless and stateful methods
Correct
in other words, you're asking about methods that don't depend on object state versus methods that do depend on object state
what objects?
inside or outside of methods?
objects of the class that the method is contained in
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
what state?
ANY state
methods are sets of actions, not states
exactly
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
so you cant store state (aka data that lives longer than a single execution of a method) in them
so object contains all of the values of their fields?
we've been over this in your previous threads, yes
Properties more specifically, but yes
I thought propeties were values?
properties hold values
could you give me an example of stateless and stateful methods please?
properties and fields are both just variables, but named a little different to describe how/where they're used
I thought they were the same
both are stored in a class
Consider these two classes and methods
the top one is entirely stateless. any two executions given the same values in will return the exact same value out
Consider what?
read the code?
they're both classes
try to see what it does? listen to our explanations?
Properties are generally public and have getter/setter. Fields are usually private. Both are defined at the class level, outside of methods
Don't get caught up on the field vs property thing here @Soinagie , its not the important part
what does this has to do with objects?
I'm trying to answer your original question
Do you see why the 2nd one would be stateful?
I think I already said I get that
and Relevant confused me with this
Go back to that code example, and we'll get to objects. Do you understand why the 2nd one would be considered stateful?
its (more or less) just a rephrase of your original question
yeah bc it uses
_value
from fieldRight
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
what?
_value
is a variablecorrect
then why did you call these objects?
I didn't
.
When I say object, I mean an instance of the class
because each object (instance of the class), would have its own
_value
but
_value
is a variablesince
_value
belongs to the instance
Are we seriously back to that discussion, again?Have you actually started visual studio at all since last we talked?
we have 2
StatefulAdder
objectsyeah
And they both have their own separate values of
_value
if you're just going to be mean feel free to leave
one of them might be 2 and the other might be 4
So if you do:
they would give you 2 different results
Its a legitimate question to be honest. You need to actually write and run code to learn this.
where? you didn't assign
_value
to themsure we did, thats done inside
.Add
After this code executes, what would the values of
_value
be for each object?
assuming _value starts at 0where is here
_value
?_value as defined above is private. It can only be modified in the method
but you didn't name it
didn't name what?
how would code know to add specifically
_value
?Look at the Add method
_value += a;
every time that method is called, it adds to _valueand returns it where?
back to the caller, but that can be ignored for now
here, we can just remove that to prevent confusion:
return
always returns to the caller btw
there is no special case for that keywordwhat is
StatefulAdder adder 1 = new StatefulAdder();
? how can you add a whole class to a method?
I mean I know it's an objectadd 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 classits just an object reference
The variable name is
adder1
. It's type is StatefulAdder
you're calling an object with
adder1
and then adding to it?creating an object named
adder1
then calling it's Add
methodwell 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 itwait I thought
adder1
was an object
since you did New
it is, yes
it is what?
an object
but here you called it a variabe
yep
it's both
I can feel my brain melting...
an object is a variable
ok this is now an exact repeat of the 1K+ posts "what is an object" thread.
I think there's probably exceptions to that, with dependency injection, etc, but that isn't the point right now
Soinagie, please, for your own sake. Just start visual studio and start coding
so stateful methods always call an object?
write code, try to make it work, run it, test it
and stateless don't?
It's challenging to understand state if you don't have a solid grasp of classes and objects
most of the time... but of course there are exceptions...
static variables exists.
it was at the beggining of the tutorial
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
what good is it if I have no idea what anything does and why?
Because you cant learn programming without actually coding
most of your understanding will come from actually experimenting with the code
not from reading
and I can't start coding without knowing the basics
oh you can.
you have analysis paralysis at the moment, that much is clear
stateless vs stateful methods isn't a topic that matters when you're first starting
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 thenWhat you're asking isn't really a basic
it was in 3rd unit of $helloworld
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
in 5th module
sorry for wasting your time
go ahead and copy this code
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
ok thank you and sorry again
don't be sorry, be daring
dont overthink stuff, just get your hands dirty
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