0

I've come across this example of an empty statement in a C# textbook.

Code:

public void empty()
{
  ;  
}

Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?

I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
benbants
  • 590
  • 7
  • 30
  • probably just as method stub (signature) or for unit testing – Rahul May 22 '17 at 13:20
  • 1
    If you found it in a textbook, there would be context to why it shows up there - why not share that context with us? – DigiFriend May 22 '17 at 13:21
  • 1
    Maybe if you have a while-loop that breaks as soon as the condition is met and all you want to do is waiting. Then `empty();` might be better than `;` – Tim Schmelter May 22 '17 at 13:21
  • 5
    See https://msdn.microsoft.com/en-us/library/aa664739(v=vs.71).aspx & https://www.dotnetperls.com/empty-statement – PaulF May 22 '17 at 13:22
  • @PaulF: yes, that's an empty statement(semicolon only), but OP is wrapping it in a method. He's asking if this method makes any sense. But the documentation also mentions use cases so the link is helpful. – Tim Schmelter May 22 '17 at 13:30
  • @TimSchmelter: By "empty statement" I assumed the OP to be referring to the semi-colon (ie the empty statement) rather than the entire method - but you may be right. – PaulF May 22 '17 at 13:35
  • @TimSchmelter I'm interested in both of your comments, apologies if the title of my question was misleading to either of you. I'm interested in the use of empty statement but the example shown was just the one I had found and I was using it for reference. – benbants May 22 '17 at 14:13
  • @PaulF please read above ^ – benbants May 22 '17 at 14:14

1 Answers1

2

In the given example it is pointless and/or cosmetic.

The empty statement is "useful" in places where a statement is required but you have nothing to do, like

 while (condition_with_side_effects) ;

Because of the side effects required, this will not match with most coding guidelines or best practices.

Consider it a leftover from C.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Thank you, this makes a lot of sense I suppose I should've been able to figure it out as it's actually quite a simple concept. I'm assuming most developers just leave the statement blank rather than using a semicolon? – benbants May 22 '17 at 13:51
  • 1
    *condition_with_side_effects* You don't need a side effect... A badly done polling for an external event is enough. – xanatos May 22 '17 at 13:53
  • @xanatos What do you mean by this? – benbants May 22 '17 at 14:09
  • 1
    @benbants There are many possible reasons to use an empty statement and not only the `while ()` suggested. Not all of them are implicitly "bad practice" (you would need to analyze them one by one). But all of them are probably quite rare. For example `while (!File.Exists("SomeFile")) ;` would be nearly acceptable. The external event is the creation of a file. You can't do this code in any other way. I would probably write it like `while (...) { Thread.Sleep(100) }`, but that is what *I* would do :-) – xanatos May 22 '17 at 14:14
  • @xanatos Would Thread.Sleep(100) simulate the same behavior as the empty statement in this case? (Apologies not very familiar with threading). – benbants May 22 '17 at 14:18
  • 1
    @benbants Without the `Thread.Sleep(...)` the program would continually poll the OS for the existence of the file. This is time-consuming for the PC. With a `Thread.Sleep(...)` there will be a "pause" of 100ms (0.1sec) between checks. The 100ms is a random number I've chosen, and must be selected to balance the need of responsiveness to the external event and the need to not do useless work (checking the existence of the file continuosly) – xanatos May 22 '17 at 14:21
  • 2
    _would be nearly acceptable_ - you can't be serious. You should never tie up a thread like that. And any form of improvement (bool flag, CancellationToken) would eliminate the empty statement. – H H May 22 '17 at 14:26
  • @HenkHolterman Yep :-) You are totally right :-) But *my* standard and *your* standard of coding are probably much higher than the standard I sadly see everyday :-) – xanatos May 22 '17 at 14:38