Avoid string concatenation.

There are clearer ways of organizing strings.

Some people like to assemble strings through a series string concatenation commands. This can get rather complex. Building an interesting string this way can well over a dozen lines of code. When a new person has to review the code, it can be hard to tell at a glance what the author’s intent is.

It’s a lot easier to read sprintf statements. With one simple statement, a complex string is easily assembled. Also, the sprintf statement separates the format of the message from the data.

Some people object to the use of sprintf because of outdated notions of a performance penalty from bringing in a standard I/O library. In reality, the benefits of having clear code almost always outweigh these trivial differences in performance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: