Comment On self->Static Anti-Pattern

If someone comes up to you and says that they work in the video game industry you might think that he or she has an interesting job (and if you're a gamer, then it sounds they have an AWESOME job). Surely, in between marathon video game playing sessions, they would be working with some true geniuses on really cool programs instead of working with the regular schmoes maintaining the Batch Load Processing Report - right? Well, wake up from that dream, bucko.  The same bad coders from whom you inherited that insipid, "enterprise level" application get to make video games while you slave away for "the man". [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: self->Static Anti-Pattern

2009-01-19 09:04 • by puzzled (unregistered)
static bool first = true;


if (first) {
first = false;

Re: self->Static Anti-Pattern

2009-01-19 09:11 • by ThomsonsPier
"of which the following code is a part of"?

Re: self->Static Anti-Pattern

2009-01-19 09:16 • by Mel
239810 in reply to 239809
ThomsonsPier:
"of which the following code is a part of"?

Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in

Re: self->Static Anti-Pattern

2009-01-19 09:19 • by puzzled (unregistered)
239811 in reply to 239810
Sure - it's from the "Live and let die" school of grammar:
[quote user="The Beatles"]But in this ever changing world in which we live in[/quote][/quote]

The Wings, not The Beatles

Re: self->Static Anti-Pattern

2009-01-19 09:21 • by Mel
239812 in reply to 239811
puzzled:
Mel:
Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in


The Wings, not The Beatles

Whoops

Re: self->Static Anti-Pattern

2009-01-19 09:21 • by durnurd
void Lib::output(Lib* self)

{
if (self->active != STATUS) {
if ((self->active == false) && (STATUS == true)) {
STATUS = false;
} else if ((self->active == true) && (STATUS == false)) {
STATUS = true;
}
}
}


or perhaps


void Lib::output(Lib* self)
{
STATUS = self->active;
}

Re: self->Static Anti-Pattern

2009-01-19 09:24 • by Superdude (unregistered)
239814 in reply to 239812
Mel:
puzzled:
Mel:
Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in


The Wings, not The Beatles

Whoops


Surely just Wings (unlessmy sarcasm detector has been unplugged)?

Re: self->Static Anti-Pattern

2009-01-19 09:25 • by Claxon
... marathon video game playing sessions...


Yup, that sounds about right to me! An office that doesn't even have a single arcade machine isn't worth going to every day!

Re: self->Static Anti-Pattern

2009-01-19 09:25 • by Voodoo Coder
239816 in reply to 239811
puzzled:
Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in


The Wings, not The Beatles


Wings, not The Wings. (just keeping in the nitpicky spirit of TDWTF)...

Re: self->Static Anti-Pattern

2009-01-19 09:36 • by Adriano
239817 in reply to 239813
durnurd:
void Lib::output(Lib* self)

{
if (self->active != STATUS) {
if ((self->active == false) && (STATUS == true)) {
STATUS = false;
} else if ((self->active == true) && (STATUS == false)) {
STATUS = true;
}
}
}


or perhaps


void Lib::output(Lib* self)
{
STATUS = self->active;
}


That fails to account for the two other cases (when STATUS and self-active share the same status). I don't know if it's right to do so or not, mind you.

EDIT: Rectified in my next comment. Why can't I delete my own comments? (I know, Community Server...)

Re: self->Static Anti-Pattern

2009-01-19 09:37 • by Adriano
239818 in reply to 239813
durnurd:
void Lib::output(Lib* self) {

if (self->active != STATUS) {
if ((self->active == false) && (STATUS == true)) {
STATUS = false;
} else if ((self->active == true) && (STATUS == false)) {
STATUS = true;
}
}
}


or perhaps


void Lib::output(Lib* self) {
STATUS = self->active;
}


This won't do: it behaves correctly on the two other cases (when STATUS and self->active share the same state).

Re: self->Static Anti-Pattern

2009-01-19 09:40 • by ingenious (unregistered)
239820 in reply to 239816
Voodoo Coder:
puzzled:

The Wings, not The Beatles


Wings, not The Wings. (just keeping in the nitpicky spirit of TDWTF)...


If to nitpick, then I guess the "The" in "The Wings" wasn't meant to be part of the band name, but used as a definite article.

Re: self->Static Anti-Pattern

2009-01-19 09:41 • by Code Dependent
239821 in reply to 239810
Mel:
ThomsonsPier:
"of which the following code is a part of"?

Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in
Try this: "But if this ever-changing world in which we're living makes you give in and cry--Say 'Live and let die'."

Re: self->Static Anti-Pattern

2009-01-19 09:42 • by Zero (unregistered)
239822 in reply to 239810
I thought the lyric was "...world in which we're livin'". Which makes more sense.

Re: self->Static Anti-Pattern

2009-01-19 09:44 • by Bert (unregistered)
239823 in reply to 239818
Adriano:
durnurd:
void Lib::output(Lib* self) {

if (self->active != STATUS) {
if ((self->active == false) && (STATUS == true)) {
STATUS = false;
} else if ((self->active == true) && (STATUS == false)) {
STATUS = true;
}
}
}


or perhaps


void Lib::output(Lib* self) {
STATUS = self->active;
}


This won't do: it behaves correctly on the two other cases (when STATUS and self->active share the same state).

Saving one redundant boolean assignement is hardly worth all of this...

Re: self->Static Anti-Pattern

2009-01-19 09:45 • by OldCoder (unregistered)
My head spins after reading this and trying to figure out what it's doing. Or not doing, in a very complicated fashion.

Does this guy (the coder, not the submitter) really not understand how Object-Oriented code works?

Re: self->Static Anti-Pattern

2009-01-19 09:48 • by puzzled (unregistered)
239825 in reply to 239822
Zero:
I thought the lyric was "...world in which we're livin'". Which makes more sense.


it's

But if this ever changin' world
In which we live in
Makes you give in and cry

Say live and let die

Re: self->Static Anti-Pattern

2009-01-19 09:51 • by stanbeard (unregistered)
239826 in reply to 239812
Actually Paul McCartney sings "But if this ever changing world in which we're living"

It's Axl Rose that sang "But if this ever changing world in which we live in"... and he's realised his mistake because he sung it properly at the Gunners concert I went to last year.

Re: self->Static Anti-Pattern

2009-01-19 09:56 • by pjt33
There are games companies which try to filter out people who don't know how to code very well at interview. I used to work for one. Unfortunately I now work for a games company which accepts anyone who knows how to write syntactically correct code.

Re: self->Static Anti-Pattern

2009-01-19 10:02 • by Slash (unregistered)
239829 in reply to 239826
stanbeard:
the Gunners concert I went to last year.


^ TRWTF ist right there!

Re: self->Static Anti-Pattern

2009-01-19 10:20 • by Code Dependent
239832 in reply to 239826
stanbeard:
Actually Paul McCartney sings "But if this ever changing world in which we're living"

It's Axl Rose that sang "But if this ever changing world in which we live in"... and he's realised his mistake because he sung it properly at the Gunners concert I went to last year.
Not as bad as David Lee Roth singing

I don't believe you
It must be true
No one could look as good as you

...where the actual lyrics are

I don't believe you
You're not the truth
No one could look as good as you

Re: self->Static Anti-Pattern

2009-01-19 10:31 • by Thierry Henry (unregistered)
239833 in reply to 239826
stanbeard:
Actually Paul McCartney sings "But if this ever changing world in which we're living"

It's Axl Rose that sang "But if this ever changing world in which we live in"... and he's realised his mistake because he sung it properly at the Gunners concert I went to last year.


When did Arsenal start performing gigs. I'm intrigued, who sings; is it Wenger or is he on the drums.

Re: self->Static Anti-Pattern

2009-01-19 10:36 • by pscs
239834 in reply to 239813
durnurd:


void Lib::output(Lib* self)
{
STATUS = self->active;
}


void Lib::output() //which is a non-static member, as it should be!
{
STATUS = active;
}

Would be better. It still doesn't solve the rather scare assignment of 'STATUS' there, but that would probably need a total redesign.

Of course, it MIGHT be that 'STATUS' is actually a hardware register, in which case the original complex boolean stuff might be necessary to avoid hardware register writes if data hasn't changed (but I doubt it...)



(PS - this is one of the first 'WTF's in a while which has actually had me issue that utterance)

Re: self->Static Anti-Pattern

2009-01-19 10:40 • by configurator (unregistered)
239837 in reply to 239813
But what if self->active was FILE_NOT_FOUND

Re: self->Static Anti-Pattern

2009-01-19 10:57 • by Matt.C
ANDY GOTH=NO

Re: self->Static Anti-Pattern

2009-01-19 11:02 • by Sad Bug Killer
May be, just may be, this "programmer"'s first language was Lua (we are talking about game companies here after all) and he didn't grasp the concept of static vs instance methods in C++.

To clarify - Lua doesn't have classes per se, instead there are "tables" - associative containers - that can contain functions as values. There's also some syntactic sugar to implicitly pass the containing table to a "member" function, thus emulating methods with this pointer, but there's no difference between a function declared with explicit this parameter and a function declared with the syntactic sugar/implicit this parameter. Which seems to be related to the WTF at hand. Also, the implicit "this" parameter is called "self".

Re: self->Static Anti-Pattern

2009-01-19 11:09 • by Adrian Cheater (unregistered)
I'm surprised that anyone expects video games to somehow magically adhere to a higher standard of code excellence. As an indie developer, I've worked with and written my share of bad code. Hopefully I'm at least trying to improve, but game development seems to get it from all angles.

Goals are unclear:
You almost never know what you're making up front, since it's hard to design 'fun' on paper. It's exploratory software, unless you're writing Madden 2K-something.

Hardware is non-standard:
PC games are a minefield of hardware issues, you often need special cases to handle the wide variety of configurations, or you limit your market appeal.

Budgets and time lines are often rigid:
As we know, if you can't wiggle on cost and speed, you end up wiggling on quality. Front end quality isn't usually too bad, but keeping all those lofty design goals you had up front start to fall aside when the crunch hits. Since games tend to be throw-away code in the end, maintenance may take a back seat to software stability.

Oh, and we have our share of idiots. Guys and girls who 'just like' games, wanted to be a writer instead, have 'a great idea', who may or may not have learned how to code involved in the process. Software is software, and that's why I love coming here, to learn more about what does and doesn't work in other sectors.

Re: self->Static Anti-Pattern

2009-01-19 11:19 • by Code Dependent
239842 in reply to 239840
Adrian Cheater:
...keeping all those lofty design goals you had up front start to fall aside when the crunch hits.
You just reminded me of an expression I saw years ago framed on some coworker's wall: "When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp."

Re: self->Static Anti-Pattern

2009-01-19 11:36 • by dtfinch
Some people can write Fortran in any language. This guy writes Python.

Production code?

2009-01-19 11:39 • by panschk (unregistered)
Right now I'm in the process of teaching myself C++, and some of the stuff I do looks a lot like this. You know, trying if some expression will compile, and what happens if you write
cout << *&*&*&varname;

This does not look like production code that 'does' anything to me.

Re: self->Static Anti-Pattern

2009-01-19 11:50 • by Andrew (unregistered)
239845 in reply to 239810
Mel:
ThomsonsPier:
"of which the following code is a part of"?

Sure - it's from the "Live and let die" school of grammar:
The Beatles:
But in this ever changing world in which we live in


Paul McCartney's Wings sang "Live and let die".

Re: self->Static Anti-Pattern

2009-01-19 11:51 • by Xar (unregistered)
239846 in reply to 239807
static bool first = true;


if (first) {
first = false;


This is actually a known C/C++ practice: since first is declared static its value is kept across several calls to the function, so you only do the init the first time the function is called.

Re: self->Static Anti-Pattern

2009-01-19 11:53 • by Voodoo Coder
239847 in reply to 239820
ingenious:
Voodoo Coder:
puzzled:

The Wings, not The Beatles


Wings, not The Wings. (just keeping in the nitpicky spirit of TDWTF)...


If to nitpick, then I guess the "The" in "The Wings" wasn't meant to be part of the band name, but used as a definite article.


In keeping with the spirit...

The noun "Wings" is hardly an indefinite noun (in this context), requiring any kind of article.

Furthermore, adding the article could lead to entirely different meaning of the noun (i.e. "Looks like The Wings might be in for another run at the Stanley Cup this year!"). While most who are familiar with Paul McCartney's post-Beatles works and/or James Bond movies featuring Roger Moore would identify either statement in the given context, a man from mars may very well wonder how a hockey team in Detroit could be responsible for such an anthem.

</nitpicking>

Re: self->Static Anti-Pattern

2009-01-19 11:54 • by puzzled (unregistered)
239848 in reply to 239846
Xar:
static bool first = true;


if (first) {
first = false;


This is actually a known C/C++ practice: since first is declared static its value is kept across several calls to the function, so you only do the init the first time the function is called.

I knew what the code was for, it was a comment in reply to the "FIRST" comment (I forgot to click the quote\reply button though)

Re: self->Static Anti-Pattern

2009-01-19 12:00 • by Doc Monster (unregistered)
239849 in reply to 239832
Code Dependent:
Not as bad as David Lee Roth singing

I don't believe you
It must be true
No one could look as good as you

Even worse is Sammy Hagar singing

Only time will tell
If our love
Will stand the Test of Time

(--Van Halen, Why Can't This Be Love?)

Sammy needs to read the dictionary page for "tautology".

Re: self->Static Anti-Pattern

2009-01-19 12:02 • by Andrew (unregistered)
239850 in reply to 239846
Xar:
static bool first = true;


if (first) {
first = false;


This is actually a known C/C++ practice: since first is declared static its value is kept across several calls to the function, so you only do the init the first time the function is called.


No, in this context the first flag is absolutely a bad practice. The IF-statement's body constructs the Lib object. There appears to be only one Lib object. So, why don't they construct it in the main block?

Re: self->Static Anti-Pattern

2009-01-19 12:14 • by RBoy (unregistered)
Andy = Nooooooooo!!!!!!


Re: self->Static Anti-Pattern

2009-01-19 12:17 • by Andy Goth
239852 in reply to 239838
Matt.C:
ANDY GOTH=NO
I know when I'm not wanted... :^(

Re: self->Static Anti-Pattern

2009-01-19 12:21 • by Cecil (unregistered)
239853 in reply to 239846
Except it's a horrible practice.

Pull everything inside the "if (first)" block and move it into an Init() function.

Re: self->Static Anti-Pattern

2009-01-19 12:26 • by z0ltan (unregistered)
239854 in reply to 239809
ThomsonsPier:
"of which the following code is a part of"?


Sir, I would upmod you if I could! ;-)

Re: self->Static Anti-Pattern

2009-01-19 12:36 • by cellocgw
239856 in reply to 239810
I thought it was
"If this ever-changing world in which we're living.."
At least one online lyrics source agrees with me:

http://www.sing365.com/music/lyric.nsf/Live-And-Let-Die-lyrics-Paul-McCartney/8479C51B2F85A57748256A4C0008CAE9

Re: self->Static Anti-Pattern

2009-01-19 12:37 • by EFH (unregistered)
I'm surprised noone has mentioned "self" up until now. C++ doesn't have "self", it has "this". There must be a macro someplace

#define self this

Clearly this programmer didn't feel at home with C++.

Re: self->Static Anti-Pattern

2009-01-19 12:41 • by dcardani
239859 in reply to 239849
Doc Monster:
Code Dependent:
Not as bad as David Lee Roth singing

I don't believe you
It must be true
No one could look as good as you

Even worse is Sammy Hagar singing

Only time will tell
If our love
Will stand the Test of Time

(--Van Halen, Why Can't This Be Love?)

Sammy needs to read the dictionary page for "tautology".


Or Smashing Pumpkins' "what I choose is my choice." Brilliant.

What's The Beatles?

2009-01-19 12:49 • by Harrow (unregistered)
I Googled 'The Beatles' -- wow, just wow -- Paul McCartney was in another band before Wings!

The stuff you learn around here...

-Harrow.

Re: self->Static Anti-Pattern

2009-01-19 13:10 • by bob (unregistered)
239861 in reply to 239857
EFH:
I'm surprised noone has mentioned "self" up until now. C++ doesn't have "self", it has "this". There must be a macro someplace

#define self this

Clearly this programmer didn't feel at home with C++.


look again, 'this' is used with the instance methods, the 'self' pointer was passed in to the static methods to make the pseudo-instance methods.

Re: self->Static Anti-Pattern

2009-01-19 13:10 • by Code Dependent
239862 in reply to 239859
dcardani:
Doc Monster:
Code Dependent:
Not as bad as David Lee Roth singing

I don't believe you
It must be true
No one could look as good as you

Even worse is Sammy Hagar singing

Only time will tell
If our love
Will stand the Test of Time

(--Van Halen, Why Can't This Be Love?)

Sammy needs to read the dictionary page for "tautology".


Or Smashing Pumpkins' "what I choose is my choice." Brilliant.
Here ya go (reaching way back)--Bob Wills' "Cherokee Maiden":

One night when the moon was bright on a moonlit glade

Re: self->Static Anti-Pattern

2009-01-19 13:17 • by KattMan
239863 in reply to 239862
Code Dependent:
dcardani:
Doc Monster:
Code Dependent:
Not as bad as David Lee Roth singing

I don't believe you
It must be true
No one could look as good as you

Even worse is Sammy Hagar singing

Only time will tell
If our love
Will stand the Test of Time

(--Van Halen, Why Can't This Be Love?)

Sammy needs to read the dictionary page for "tautology".


Or Smashing Pumpkins' "what I choose is my choice." Brilliant.
Here ya go (reaching way back)--Bob Wills' "Cherokee Maiden":

One night when the moon was bright on a moonlit glade

If you choose not to decide, you still have made a choice.
Rush

Re: self->Static Anti-Pattern

2009-01-19 13:26 • by clickey McClicker (unregistered)
239866 in reply to 239847
Voodoo Coder:
a man from mars may very well wonder how a hockey team in Detroit could be responsible for such an anthem.

</nitpicking>

Technically it is all the same "Wings." They usually just do the music thing during the day but when they get really really mad they turn all Red(green was already taken), rip off their shirts, put on Hockey uniforms which leads to games and fighting/smashing/clobbering/pummelling. It is admirable how they bring such emotion to all their work even if it isn't always that good.

Re: self->Static Anti-Pattern

2009-01-19 13:31 • by Queex (unregistered)
239867 in reply to 239863

If you choose not to decide, you still have made a choice.
Rush


I think that still makes sense, as long as 'choose' and 'decide' don't reference the same Decision object. If presented with the Decision 'You are either with us or against us' you could choose neither option (presumably by raising a FalseDichotomyException) and still have made a choice (i.e. compile without errors).

Re: self->Static Anti-Pattern

2009-01-19 13:32 • by clickey McClicker (unregistered)
239869 in reply to 239863
KattMan:

If you choose not to decide, you still have made a choice.
Rush


That isn't dumb or redundant. That is deep and full of underlying truth.

Like my brother-in-law who quit Wal-mart so he could play games, a lot of people should hear that quote. Inaction is often worse than 'the wrong' action. His inaction is really a choice of shocking stupidity.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment