I have defined a macro like that: \newcommand{\code}[1]{\texttt{#1}\xspace}
In some situations, I'd like to write the plural of some object and the trailing (plural) 's'-character should not be marked as code. So it should look like that: ClassInstances.
However, when I do \code{ClassInstance}s I get an extra space, i.e. it will look like: ClassInstance s.
What can I do?
Edit
Considering @egreg's answer, it seems that I have left out an essential part of the macro. In fact, the definition of \code also modifies the text color. Only then the use of \xspace seems to be necessary at all.
In order to reproduce the issue, run this minimal example:
\documentclass[10pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{xspace}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\colcode}[1]{\color{blue}\texttt{#1}\color{black}}
\newcommand{\colcodex}[1]{\color{blue}\texttt{#1}\color{black}\xspace}
\begin{document}
Bounds are constructed by closed \code{Curve} objects.
Bounds are constructed by closed \colcode{Curve} objects.
Bounds are constructed by closed \colcodex{Curve} objects.
Sometimes we have even two \colcodex{Curve}s.
\end{document}

\xspaceto begin with? – egreg May 27 '22 at 09:03\xspacein order to allow a space between a\codemacro and the next word, such as inThe \code{ClassInstance} is ...which would yield an output without a space between "ClassInstance" and "is" without the\xspacecommand. – Amos Egel May 27 '22 at 09:08\xspaceyou'll see that the space is preserved. – egreg May 27 '22 at 09:14\leavevmode... however that's probably what\textcoloralready does behind the scene, so no need to reinvent the wheel) – user202729 May 27 '22 at 15:38