Don’t use function names with “empty”

The fragment is taken from WinMerge project. The code contains an error that analyzer diagnoses in the following way: V530 The return value of function ’empty’ is required to be utilized.

void CDirView::GetItemFileNames(
  int sel, String& strLeft, String& strRight) const
  UINT_PTR diffpos = GetItemKey(sel);
  if (diffpos == (UINT_PTR)SPECIAL_ITEM_POS)


A programmer wanted to clean the strLeft and strRight strings. They have String type, which is nothing else than std::wstring.

For this purpose he called the empty() function. And this is not correct. The empty() function doesn’t change the object, but returns the information if the string is empty or not.

Correct code

To correct this error you should replace the empty() function with clear() or erase (). WinMerge developers preferred erase() and now the code looks like this:

if (diffpos == (UINT_PTR)SPECIAL_ITEM_POS)


In this case the name “empty()” is really inappropriate. The thing is that in different libraries, this function can mean two different actions.

In some libraries the emply() function clears the object. In other ones, it returns the information if the object is empty or not.

скачанные файлы (10)

We would say that the word “empty” is lame in general, because everybody understands it differently. Some think it’s an “action”, others that it’s “information inquiry”. That’s the reason for the mess we can see.

There is just one way out. Do not use “empty” in the class names.

  • Name the function for cleaning as “erase” or “clear”. We would rather use “erase”, because “clear” can be quite ambiguous.
  • Choose another name for the function which gets information, “isEmpty” for instance.

If you for some reason think that it’s not a big deal, then have a look here. It’s quite a widespread error pattern. Of course it’s slightly late to change such classes as std::string, but at least let’s try not to spread the evil any longer.

Written by Andrey Karpov. This error was found with PVS-Studio static analysis tool.

2 thoughts on “Don’t use function names with “empty”

  1. There is the [[nodiscard] attribute, which will issue a compiler warning if you use the empty() member function as shown:
    [[nodiscard]] bool empty() const noexcept;
    Since all standard library containers follow the same convention, calling empty() to clear() a string simply shows you did not spend time to learn the basics of the C++ standard library or read the doc IMHO.


    • Yes, I mostly agree with you. But there are at least two problems.

      First, this attribute was introduced only in C++17. Not everyone uses this C++ standard in his daily work and not every company can upgrate their projects of old standards (C++98, C++03, C++11, C++14) to the latest one. A compiler will help you only if you’re using the version that supports latest standard and if standard library developers marked up those functions with [[nodiscard]] attribute.

      Second, a compiler gives you only a warning that you’ve made mistake. But, many developers like to ignore warnings from the compiler 🙂 So, it doesn’t help and the error will remain in the code base.


Leave a Reply

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

You are commenting using your 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.