Into the Void: Why void is a bad idea
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
Interesting. Not sure I totally agree. For instance (no pun intended), what about a class that abstracts out logging? Would those methods really need to return the class instance? I don't really see what there is to gain there.
Gryphons Are Awesome! Gryphons Are Awesome!
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
I didn't read it, but it seems to be something I agree with. There are many cases where I'd like to chain calls together but can't because the method is void.
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
Terrence wrote:
To void or not to void... what do you think?
It depends. Based on the example provided, it makes sense to apply Fluent Interface and to avoid void. However there are numerous cases which don't need an instance to be returned. And hence a void method is not a bad idea.
Wonde Tadesse
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
Khalid Abuhakmeh wrote:
Now what happens when I change void to return this, or the instance of the object that I am using?
If I'm not using fluent interface, I won't win a thing... I'm with Brisingr Aerowing[^] like the idea (I like fluent interfaces :)), but it depends...
(yes|no|maybe)*
-
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
Unless you really, really think your code is going to be chained together, YAGNI.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
I've been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void.... To state it simply, void is useful when I want to perform an action on an object and don't expect a result returned. In theory it sounds great, but if I don't expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object.
To void or not to void... what do you think?
Terrence Dorsey wrote:
but if I don't expect a single result back, I am most likely going to want to inspect the object itself
Why? To ensure that the state of the object is correct? That's the job of the object itself.