EDIT (08/09/2021): Yes, I’ve actually started seeing while task.wait(). Just so it’s clear, the document does cover yielding functions being used as conditions as well. task.wait() is not a better replacement and also falls under discouraged practice as per the below.
Introduction
Earlier today on my usual camp-out by the Scripting Support category, I stumbled across a piece of code that set off my nitpick ringer and reminded me of a document I stumbled across long ago. Today, after being prompted about the matter, I went and dug up said document. Figured I’d share it now.
The document in question is entitled While-Wait-Do Idiom, written by cntkillme. It discuss the practice of using wait(n) in the conditional field of a while loop, or something that contains wait(n) (note that this is also applicable for wait without n, as leaving n unspecified will use the minimum value as a default).
-- The While-Wait-Do Idiom in code, through different examples
-- 1
while wait() do
print("foo bar")
end
-- 2
while wait(1) do
if aMagnitude < bMagnitude then
return cSomethingProbably
end
end
-- 3
local function foo(bar)
wait(0.5)
return foobar
end
while foo(bar2) do
print("foo bar?")
end
I can’t exactly remember how I was introduced to the document, though reading this has influenced and changed how I write while loops. I believe this person also authored more documents regarding practices by Roblox developers, however I unfortunately do not have those nor know where to access them, if they exist.
This is a years-old document from my understanding, but I still see the relevance of some of the points brought up and statements made in this document. I still see while wait being used even now. I also figure that I might as well not let something interesting rot in my document history, so here it is.
What you can expect
There are a few key points to take away from this document, through my own reading. The primary point is that the author disagrees with the use of this idiom and that one should avoid while wait do, or cut it out from codebase altogether.
Here’s about a brief rundown of what you can expect:
- An introduction to the While-Wait-Idiom
- The problems that said idiom may cause
- Three addressed points regarding the idiom and it’s supposed “benefits”:
- “Less code is cleaner [or more readable] code”
- “Less code is faster code”
- “Everyone uses it”
- An explanation of the problem of this idiom in terms of maintainability
- The bad habits this teaches (improper usage of the conditional statement)
- A tl;dr with nothing resourceful (don’t skip to it, seriously) - try reading the whole document, since the tl;dr doesn’t include any useful information aside from telling you to go read the document. It’s just 3 and a little bit of a 4th page of good knowledge.
It’s up to you, now
I’d hate to take away from the document by summarising it myself, so I would personally encourage you to read it if you take any interest. It would probably do a better job of explaining it than I ever could anyway.
I’ve outlined what you can expect to read up on through the document, so the rest is up to you.
I have personally found this document very helpful. I’m in the progress of changing a lot of my coding practices and habits - this document was one such contribution to that change.