My programming style

Started by
42 comments, last by jpetrie 10 years, 11 months ago

And, yes, it HAS to be global. That's just plain retarded.

No it HAS NOT. You are missing the entire point of programming in any language above assembler: encapsulation. Even by keeping a procedural approach to the problem, wanna see how the global goes away? Here it is:

int main()

{

vector<Player*> players;

// CREATE 10 players

for (int i=0;i<10;i++)

{

players.push_back(new Player());
}

// GAME LOOP

while (true)

{

updatePlayers(players);

render(players);

}

// clean up yada yada

return 0;

}

void updatePlayers(vector<Player*>& players)

{

for (auto player : players)

{

// UPDATE THE PLAYER
}
}

there you go.. players is not global anymore and only functions that REQUIRE access to it receive it as a parameter.

This gives you the huge advantage that now you know which subset of your code can access to players.. and when your code reaches 100k lines of code possibly multithreaded, trust me, you'll be glad to be limited to 3-4 functions instead of the EVERYWHERE you get from a global.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

Advertisement

I'm just waiting for Hodgman to arrive and school you all on Data Oriented Design biggrin.png

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I declare variables outside of loops because I have a superstition that the CPU will waste extra cycles reallocating the variable on each iteration of the loop.

This is something you need to get out of your head real fast. Instead of superstition you need to make decisions based on fact, and use tools available to you to inform yourself about what that fact is. In this case, reading documentation, viewing disassembly and running benchmarks would soon enough tell you the truth.

Right now you're voodoo-optimizing, you're pre-emptively optimizing, you're trying to second-guess the compiler, you're assuming that the C++ you write is going to generate a line-for-line equivalent when compiled, and it's not even based on anything more solid than "superstition". That can only lead to worse and worse habits over time, so cure yourself of it now before it does real damage to you.

John Carmack does it toooooO

https://github.com/id-Software/Quake-III-Arena/blob/master/common/bspfile.c


void UnparseEntities( void ) {
	char	*buf, *end;
	epair_t	*ep; // <---- THIS
	char	line[2048];
	int		i;
	char	key[1024], value[1024];

	buf = dentdata;
	end = buf;
	*end = 0;

	for (i=0 ; i<num_entities ; i++) {
		ep = entities.epairs; // <------ HERE
		if ( !ep ) {
			continue;	// ent got removed
		}

		strcat (end,"{\n");
		end += 2;

		for ( ep = entities.epairs ; ep ; ep=ep->next ) {
			strcpy (key, ep->key);
			StripTrailing (key);
			strcpy (value, ep->value);
			StripTrailing (value);

			sprintf (line, "\"%s\" \"%s\"\n", key, value);
			strcat (end, line);
			end += strlen(line);
		}
		strcat (end,"}\n");
		end += 2;

		if (end > buf + MAX_MAP_ENTSTRING) {
			Error ("Entity text too long");
		}
	}
	entdatasize = end - buf + 1;
}

dude.. that code is 12-13 years old!

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

That's because he was writing C and in C (at least until maybe C99?) you HAD to declare all your variables up front and not on the line they were first used.

Secondly that is code from Quake3, a 14 year old game based on an even older engine, the state of the art has moved on ALOT since then... a lot.

Compilers are better, hardware is different, the world moves on; there is probably very little of worth coding style wise (and maybe even data structure wise) in code that old.

In short 'huhuhuh Carmack did it 14 years ago!' might not be voodoo optimisation instead you've ventured into the realm of cargo cult coding - learn to think for yourself or, frankly, gtfo of my profession.

No u

https://github.com/id-Software/DOOM-3-BFG/blob/master/neo/framework/Common.cpp


void idCommonLocal::FilterLangList( idStrList* list, idStr lang ) {

	idStr temp; // <-- THIS
	for( int i = 0; i < list->Num(); i++ ) {
		temp = (*list); // <-- HERE
		temp = temp.Right(temp.Length()-strlen("strings/"));
		temp = temp.Left(lang.Length());
		if(idStr::Icmp(temp, lang) != 0) {
			list->RemoveIndex(i);
			i--;
		}
	}
}
9 year old game based on an even older engine code base still - chances are that code existed back in the 90s and hasn't been updated.

So, no, you remain wrong.. but hey, here's an idea, if you don't want to listen to people who know more than you don't start threads like this?

OP, you do realize that you are using code that is almost 20 years to justify your preconceived notions about how code should be? Do you not think that John Carmack's code or software architecture and engineering would have improved in the last 20 years? Do you think he even still uses C, as opposed to C++. When I say C++, I mean C++ 11 (the latest standard). Even if he did use C, he would be using C 99 or 11 (as in 1999 or 2011, respectively, the new standard). In the newest standards of C, you can declare variables wherever you like. Plus the hardware that he worked on is far far different than it is today.

By the way, using globals is tantamount to mentioning Hitler in an internet argument. You've lost :(

In all seriousness, GameTutorials.com is not the place you want to be learning game programming. Like I said before, that site should have died in the 90s.

Beginner in Game Development?  Read here. And read here.

 

9 year old game based on an even older engine code base still - chances are that code existed back in the 90s and hasn't been updated.

So, no, you remain wrong.. but hey, here's an idea, if you don't want to listen to people who know more than you don't start threads like this?

Well now I know.

By the way, using globals is tantamount to mentioning Hitler in an internet argument. You've lost sad.png

So I'm supposed to add an extra argument to every function where I want to use it? I'd have to rewrite all my rendering code to include a reference to the shader in use. There's like 50 functions. Plus I'd have to modify them internally to use that variable.

So I'm supposed to add an extra argument to every function where I want to use it? I'd have to rewrite all my rendering code to include a reference to the shader in use. There's like 50 functions. Plus I'd have to modify them internally to use that variable.

yep.. you really should.

But.. honestly ( and I think the other guys in this thread will agree with me).. I don't give a damn if you do or not... it's your code, your time, your money, your game.

All I can do is provide a feedback to the questionable code you have posted bragging about it. The fact that you did not have a single one positive feedback should suggest you that you are on the wrong side of the road... if you still feel we are stupid and you are the smart one... keep writing crappy code, post it and feel proud about it... be my guest.. it's fun to read for sure.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

This topic is closed to new replies.

Advertisement