How To Handle Variable Names That Are Also Keywords

59 views
Skip to first unread message

Jay C

unread,
Mar 30, 2016, 5:05:47 PM3/30/16
to Clean Code Discussion
Say I am creating and email app.  I want variables named to and from.  The problem is, to is a keyword in the programming language and from is a keyword in the database.  What is the best way to deal with this?  Right now I am just replacing to/from with recipient/sender but the new words don't seem as intuitive as to/from.  Do you have any suggestions?

Frédéric Bouquet

unread,
Mar 30, 2016, 5:15:43 PM3/30/16
to clean-code...@googlegroups.com
Hi,

I would use something like "theTo", "aTo", "theFrom". "aFrom", …
In Java, we often use "clazz" for a class which is a keyword, it would
be a solution too.

Cheers
> --
> The only way to go fast is to go well.
> ---
> You received this message because you are subscribed to the Google Groups
> "Clean Code Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clean-code-discu...@googlegroups.com.
> To post to this group, send email to clean-code...@googlegroups.com.
> Visit this group at https://groups.google.com/group/clean-code-discussion.



--
Frédéric Bouquet
Twitter/Github : bouquetf
http://gopotentiel.com
http://espacedefouille.org/

Phil Markgraf

unread,
Mar 30, 2016, 9:50:58 PM3/30/16
to clean-code...@googlegroups.com
"Sender" and "recipient" are workable synonyms. In fact they may be more clearly associated with the mail operations, while "to" and "from" have meanings in a broader range of contexts.

Namespaces are another solution. As is implicitly building a namespace into the name, as in "mailFrom" and "mailTo".

Embed the data in a larger structure (it would be a map in Clojure, or a struct in C or a class in Java). This is really a variation on the namespace idea, but it might also let you think about a more functional approach with the data externalized into a structure with carefully controlled state change (i.e. put your Clojure map in an atom, or return a new version from a pure function).

Any solution has more typing involved than just "to" and "from", but that could be okay as it sounds like the scope of these variables is not small. Your language is already telling you that these names are too readily overloaded.

Cheers,
Phil




On Mar 30, 2016, at 1:11 PM, Jay C <join....@gmail.com> wrote:

Say I am creating and email app.  I want variables named to and from.  The problem is, to is a keyword in the programming language and from is a keyword in the database.  What is the best way to deal with this?  Right now I am just replacing to/from with recipient/sender but the new words don't seem as intuitive as to/from.  Do you have any suggestions?

--

Sebastian Gozin

unread,
Apr 8, 2016, 11:44:15 AM4/8/16
to Clean Code Discussion
I don't recall having such a problem with the words to and from in Java/Groovy.

While it's possible to is seen as a keyword I can generally work around that problem.
For example, internally I can store the value in a sender variable and then simply provide a getTo-accessor.
In groovy I could use the subscript operator instead mail['to'].

As for the from keyword in the database.
You could use the recipient name instead an map it to from when you read it from the database.

In the end I can typically use those 2 in my domain model.
Reply all
Reply to author
Forward
0 new messages