views:

1281

answers:

16

I'm curious to know, what is the most abrasive, caustic, abusive but perfectly legal and correct C++ you've ever seen ship out the door?

This question isn't meant to be a C++ bashing session, but a humorous introspection into the world of C++ we allow ourselves (or others) to get away with.

+6  A: 

;

OscarRyz
for a second there I thought you hadn't written anything...
mezoid
Huh? Just a semicolon? What? Why?
Rasmus Kaj
It's valid C++, isn't it?
Phoshi
It isn't a whole program, but we're posting snippits anyway. That's a valid C++ statement. It's sort of useful sometimes, but easy to misread.
David Thornley
+17  A: 
if (!this) return;
280Z28
I don't get it, *feels dumb*, this type of statement is quite typical in Objective-C, `if (!self) return nil;`.
dreamlax
Sadly I've seen plenty of "assert(NULL != this);"
akent
Regarding the differences between `this` and `self`: http://stackoverflow.com/questions/1341734/assigning-to-self-in-objective-c
fbrereto
I'm probably missing something here but what is wrong with asserting that this != NULL?
John Burton
@JB: Nothing, IMHO. Useful for catching null pointer dereferences at runtime.
JBRWilkinson
+2  A: 

I once did templates the C (not C++) way: using #includes, #defines, #ifdefs and recursion at compilation time.

Eduardo León
+6  A: 

On a project at work I see people go:

if(false){

//Old code here

}

instead of using:

/*

*/

or just using cvs, like normal people.

windfinder
Even worse is `while (false) ...`
Kristopher Johnson
/* */ can be interrupted by other comments. I'll occasionally use a #ifdef 0, but if(false) is safer still. Also, you can set the PC into the block with a debugger if you want to test the old behaviour.
Dan O
the if(false) may actually be useful to ensure it will still compile when "commented-in" again when you make changes elsewhere.
Johannes Schaub - litb
Normal people use CVS?
avakar
I often use this for temporarily disabling code. This way, it is still compiled, and you make clear the code is not obsolete.
Dimitri C.
Normal people extend the language with `<* ... *>` comments (or whatever symbols you like), like in PeopleCode. It doesn't take a genius to write a program that reads a file and outputs a similar file but without these comments.
Eduardo León
Putting the PC in the commented-out block during debugging sounds like a scary tactic; who's to say the compiler doesn't just optimize it out? Just curious...
fbrereto
@avakar: normal people use SVN these days. Or GIT ;)
samuil
I also do that sometimes. Then if I'm debugging, I can step up to that point, move the PC down 1 line, and do the code inside if I want to.
Mike Dunlavey
@fbrereton: I get really annoyed by compilers that get that aggressive about optimizing. If I write the source, I want it compiled. I don't want it to do any more optimization than I would if I were writing the assembler code. That's the kind of thing that nanny-languages do, and it totally misses the point of what optimization is about. REAL optimization is my job, not the compiler's.
Mike Dunlavey
@Mike Dunlavey: You can't optimize as well as the compiler can (unless you do write the assembler code yourself, and are really good at it), and you wouldn't want to have to do it, anyway. Stick to efficient algorithms and no gratuitous inefficiencies, and let the compiler do the rest. It helps if the compiler has a debug mode, but I want the compiler to do all the optimization it can in some mode.
David Thornley
@febrerto: In a debug build, there's no optimisation on by default.
JBRWilkinson
+3  A: 

Calling non-virtual functions that do not modify member variables on null pointers like so:

class CObject 
{
public:
void CallMeOnNull()
{
  int i = 0;
  i++;
}

virtual void DontCallMeOnNull()
{
  int i = 0;
  i++;
}

void DontCallMeOnNull2()
{
  m_iTest++;
}
private:
    int m_iTest;
};

int test()
{
  CObject* pObject = NULL;
  pObject->CallMeOnNull(); //IS OK???!!!
  pObject->DontCallMeOnNull();
  pObject->DontCallMeOnNull2();
}
Igor Zevaka
A: 
#include <stdio.h>
Alex
Care to explain why this is abusive?
Graeme Perrow
Well, it should be <cstdio>, but I agree it's not exactly abusive.
me22
+52  A: 

The most abusive C++ I've seen by far is "Analog Literals" by Eelis.

It basically lets you do this:

  unsigned int c = ( o-----o
                     |     !
                     !     !
                     !     !
                     o-----o ).area;

or

   unsigned int c = ( o-------------o
                      |L             \
                      | L             \
                      |  L             \
                      |   o-------------o
                      |   !             !
                      !   !             !
                      o   |             !
                       L  |             !
                        L |             !
                         L|             !
                          o-------------o ).volume;

See here http://www.xs4all.nl/~weegen/eelis/analogliterals.xhtml

ttvd
Incredible!!!!!!!!!!!!!!
Miguel Angel
I have no words... that's some crazy stuff.
fbrereto
"Good... Use your aggressive feelings, boy!"This feels just like the dark side of the Force
fishlips
that is insane!
mezoid
No words...should have sent a poet!
Mike Clark
shiitt... that's an ugly hack!
Atmocreations
The purpose of this library contradicts itself: "Have you ever felt that integer literals like "4" don't convey the true size of the value they denote? If so, use an analog integer literal instead ... use N*2+1 dashes between the I's to get a value of N."
Steve Guidi
@Steve you may wanna read [Tweaking Analog Literals](http://hostilefork.com/2009/08/29/tweakinganalog-literals-humor/) then :)
Johannes Schaub - litb
+4  A: 

I have seen stuff like this, tricking around with const :-)

class abc {
public:
    long GetVal() const
    {
     return const_cast<abc*>(this)->GetVal();
    }

    long GetVal()
    {
     return member_++;
    }
private:
    member_;
};
Totonga
Implementing the const version in terms of the non-const similar to that is actually a fine thing to do -- the real problem here is the existence of a const version of a logically-mutating method.
me22
Most time not. Its just that they do not know about mutable. Its more GetVal is a const method most time. member_ could be an access counter or whatever variable. So mutable member_ woud allow you to make the compiler check wheather none other members are changed.
Totonga
+5  A: 

#include <afxwin.h> //basic MFC include

beggs
The MFC might not be elegant, but it serves its purpose.
Eduardo León
+21  A: 
#define private public
h0b0
Don't read this one while drinking coffee, or you'll be cleaning your monitor.
fbrereto
I like: #define if while better
windfinder
I had to do this when working with MFC because some function that was obviously meant to be public was actually private due to MFC developers not understanding complications of some MFC macro that changed the visibility.
erikkallen
I once put #define struct union in someone's c code to see how long it would take them to find it. Quite a while...
John Burton
+2  A: 

Here's some downvote-bait. I do a couple things that reduce errors in editing, but look funny, like

MyFunction(1stArg
    , 2ndArg
    , 3rdArg
    ... etc. ...
    , lastArg
    );

because if I want to add a new argument to the function, I often add it at the end, so it is a 1-line edit, rather than 2 lines.

Another example:

if (false){ }
else if (Test1){
    ... do stuff ...
}
else if (Test2){
    ... do stuff ...
}
... more tests ...
else {
    ... do stuff ...
}

Why? because it makes it easier to insert, delete, or re-order the tests without the first one having different syntax (and thus edited differently) from the rest. This is especially useful in generated code.

Mike Dunlavey
What's the point in the first one? That the comma is at the start of the line?
jalf
Mike Dunlavey
+1 @Mike: not too dumb. i'll remember that one the next time i'm building complex if's :D thx for the idea
Atmocreations
+3  A: 

I occurred to work on a program where the previous programmer systematically checked that address of non dynamic buffers where not NULL... at first it just look dumb, but when applied to a full program it makes it quite harder to read and maintain.

char buf[10];
if (buf != NULL) {
...
}

That was C, but you can do the same with C++ (hopefully not).

kriss
+1  A: 

Got another one. Calling a constructor on a class explicitly creates and destroys an object straight away:

class CClass
{
public:
    CClass()
    {
     printf("I am teh constructor");
    }
    ~CClass()
    {
     printf("I am teh destructor");
    }
};

//...Somewhere in main
printf("Before constructor test");
CClass::CClass();
printf("After constructor test");

What you will see is this:

Before constructor test
I am teh constructor
I am teh destructor
After constructor test
Igor Zevaka
That's just an unnamed temporary variable whose scope is restricted to a single statement.
mskfisher
+1  A: 
SDLSurface *surf = "C:\\images\\picture1.jpeg";

I was helping someone get started with SDL, when I noticed that data tips in the Visual C++ debugger showed an actual file path when the mouse pointer was hovered over an SDLSurface pointer, which lead me to discover this piece of code. When I asked him why, he said he didn't know what to do, so he had to just try something. He's in the 3rd year of a CS master.

Cecil Has a Name
+1  A: 

alt text

delete this;

It's never nice to see suicidal objects, but sometimes it makes sense. The FAQ entry explains why it sometimes makes sense.

Jeff Foster
Makes sense for reference counting.
Cecil Has a Name
A: 

String batman_dialog = "BOOM POW WHACK KAPOW!!!!!"; String robin_dialog = "Holy caps batman"; String bg_music = "na na na na na na na na na na na na na batman!!!"

Ravi Vyas