Is a bad practice to use "?" in a Racket method name?

Viewed 151

I had a racket exam last week and I named some of my methods for the exam using the question mark symbol(e.g. "belongs?", "has-fever?", etc...). But when they gave me the results, one of my professors told me that it's a bad practice to use the "?" in methods in the way that I used it and I don't get it because a lot of methods in racket are originally called like that.

So my question is: is it really a bad practice or is it not? and if it is a bad practice, why racket use it?

2 Answers

It is idiomatic in both Scheme and Racket for predicate procedures to end with ?. Because there are certain expectations due to history and convention that are associated with such procedures, it is considered bad practice to diverge from that convention. The Scheme standards even have language formalizing this practice (R6RS 6.7 Naming Conventions):

By convention, the names of predicates—procedures that always return a boolean value—end in “?” when the name contains any letters; otherwise, the predicate's name does not end with a question mark.

Now, in both Scheme and Racket, a predicate is a procedure that always returns a boolean value, that is, #t or #f. If the procedures under critique did not always return either #t or #f, then they did not fit the model of naming conventions for predicates. If that is not the case, it is difficult to know how they may have come up short without seeing the actual definitions.

member is an example of a Racket procedure that may be used as a "predicate", but is not actually a predicate by convention, or by the language of a specification. This procedure returns #f if the sought value is not a member of a list, but it returns the tail of the list beginning with the sought value if it is a member of the list (this list tail is a true value, but not boolean #t). Note that member does not end with a question mark. If OP belongs? behaves similarly to member, then the professor was certainly correct to classify this naming as a bad practice.

Note that other lisps have different conventions around predicates. For example, in Common Lisp it is not conventional (and not considered a good practice) to use ? in a predicate name.

According to the Racket codebase's own Style Guide predicates and boolean-valued functions should end in ?. belongs? and has-fever? seem to fit that bill, so I suspect this is down to your professor's taste.

Related