Naming convention regarding question mark #140
Replies: 2 comments 1 reply
-
I call into question that questionable statement. 😋 |
Beta Was this translation helpful? Give feedback.
-
Example of use the naming form
|
Beta Was this translation helpful? Give feedback.
-
?foo
naming formUsually this form is used for a word that does something or does nothing depending on some condition.
The standard words that share this naming form are:
?dup ( 0 -- 0 | x -- x x )
, which does nothing if the top item is 0.?do ( u1 u2 -- )
, which (in run-time) does not perform the body loop even once if u1=u2NB: for each of them there is a corresponding word without a question mark that does its action unconditionally:
dup
, anddo
.Other known words are:
?comp
— do nothing if compilation, raise an error otherwise.?stack
— raise an error if the stack is underflow, otherwise do nothing.Advice: don't use the naming form
?foo
for a word that unconditionally returns a flag or other value.The summary: a question mark prefix in a word name (like
?foo
) makes stress that its action is conditional, usually in presence of an unconditional word variant (likefoo
).foo?
naming formAny word for which this form is used returns a flag at the top (but not vice versa).
The standard words that share this naming form are:
key? ( -- flag )
ekey? ( -- flag )
xkey? ( -- flag )
emit? ( -- flag )
For each of these words there is a counterpart word without a question mark:
key ( -- char )
,ekey ( -- x )
,xkey ( -- xchar )
,emit ( x -- )
.A general form of these use cases is following: you have words
foo
andfoo?
, and behavior offoo
somehow depends on whatfoo?
returns.Another standard word that has this naming form is
environment? ( c-addr u -- false | i*x true )
; but it doesn't have any counterpart word. It's unclear why this name ends in the question mark.There are a lot of standard words without a question mark that return a flag (comparisons, search substring, converting, check availability of a word). Therefore, if a word returns a flag, it still is not enough ground to add a question mark suffix. Otherwise we would have too much question marks in code.
Advice: don't use the naming form
foo?
for a word if you don't have a counterpart word without a question markfoo
.Beta Was this translation helpful? Give feedback.
All reactions