Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
arrowPress Releases







If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

WTF?

by Andrey Karpov on 10/03/13 06:44:00 am

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

I'm currently experiencing a strong cognitive dissonance, and it won't let me go. You see, I visit various programmers' forums and see topics where people discuss noble ideas about how to write super-reliable classes; somebody tells he has his project built with the switches -Wall -Wextra -pedantic -Weffc++, and so on. But, God, where are all these scientific and technological achievements? Why do I come across most silly mistakes again and again? Perhaps something is wrong with me?

Well, there are actually wonderful projects too. Such is, for instance, the library ALGLIB. Source code of this library is very interesting. Developers write code in Pascal and then code translating into C++ and C# automatically. Besides a number of other advantages, this approach allows them to catch quite many diverse bugs, since one and the same program is built by compilers supporting different languages. But this is quite another story, and perhaps we will tell it someday in a joint article by the library's author and me.

Such wonderful exceptions to the common state of things only enhance my cognitive dissonance. Now try to imagine what I feel. Say, I take a complex package of computer simulation software and see not a single bug there. I'm glad about the high-quality code and just a bit sad because the package's author will never buy PVS-Studio. Well, never mind. Then I take the project OpenCOLLADA and check it. WTF? I have no other words to express my feelings. What do you think about constructors like the ones below?

struct short2
{
  short values[2];
  short2(short s1, short s2)
  {
    values[0] = s1;
    values[2] = s2;
  }
  ....
};

struct double2
{
  double values[2];
  double2( double d1, double d2)
  {
    values[0]=d1;
    values[0]=d2;
  }
  ....
}

The programmer missed the array in the first constructor and forgot to change the index in the copied-and-pasted line in the second constructor.

I'm sorry for posting this picture, guys, but it shows quite exactly what I feel.

Other constructors are also a source of much wonder and fun. For example, these ones are very nice:

struct ParserString : public UnionString
{
  ParserString()
  {
    UnionString::str = 0;
    UnionString::length = 0;
  }

  ParserString(const int& val)
  {
    ParserString();
  }
};

Instead of calling another constructor, a temporary object is created and gets destroyed at once, whereas the class members are left uninitialized. More about it.

Oh my God, where are all those people who with so much zeal write about C++11, lambdas, Boost.Asio, shared_ptr, constexpr, LINQ? How could the following code have been written:

struct ObjectGroups{
  componentList objectGrpCompList;
  int objectGroupId;
  short objectGrpColor;
  void write(FILE* file) const;
}* objectGroups;

void write(FILE* file) const
{
  size_t size = sizeof(objectGroups)/sizeof(ObjectGroups);
  for(size_t i=0; i

The programmer just divided the pointer size by the structure's size and got 0. What the hell did he mean to do? WTF?

Well, even when you can guess what and how the programmer wanted to write into a file, you feel no better.

void write(FILE* file) const
{
  fprintf(file,"%i %i %i %i ",
    sDivisionCount, tDivisionCount, uDivisionCount, pointCount);
  size_t size = pointCount*3;
  for(size_t i; i

If you don't see the bug, I'll prompt. The variable 'i' is not initialized: for(size_t i; i

Sorry for sharing all this with you - it just makes me feel better somehow. And I also use this opportunity to remind you that all these bugs were found by the PVS-Studio static code analyzer. The locations of the above mentioned and some other notable bugs are listed in this text file. As usual, if somebody wants to check this project more thoroughly, ask me for a key.

Good luck and may your code stay bugless!


Related Jobs

Insomniac Games
Insomniac Games — Burbank, California, United States
[07.23.21]

Technical Artist - Pipeline
Insomniac Games
Insomniac Games — Burbank, California, United States
[07.23.21]

Engine Programmer
Legends of Learning
Legends of Learning — Baltimore, Maryland, United States
[07.23.21]

Senior Gameplay Engineer - $160k - Remote OK
Bytro Labs GmbH
Bytro Labs GmbH — Hamburg, Germany
[07.23.21]

Senior Product Owner / Live-Ops Owner (f/m/x)





Loading Comments

loader image