Jump to content

  • Log In with Google      Sign In   
  • Create Account


My programming style


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
43 replies to this topic

#21 kunos   Crossbones+   -  Reputation: 2205

Posted 27 April 2013 - 03:09 PM

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.


Edited by kunos, 27 April 2013 - 03:10 PM.

Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

Sponsor:

#22 ChaosEngine   Crossbones+   -  Reputation: 2290

Posted 28 April 2013 - 04:55 PM

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

#23 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 02:36 AM


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[i].epairs; // <------ HERE
		if ( !ep ) {
			continue;	// ent got removed
		}

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

		for ( ep = entities[i].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;
}


#24 kunos   Crossbones+   -  Reputation: 2205

Posted 29 April 2013 - 02:47 AM

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


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#25 phantom   Moderators   -  Reputation: 7109

Posted 29 April 2013 - 02:49 AM

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.

#26 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 02:55 AM

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)[i]; // <-- HERE
		temp = temp.Right(temp.Length()-strlen("strings/"));
		temp = temp.Left(lang.Length());
		if(idStr::Icmp(temp, lang) != 0) {
			list->RemoveIndex(i);
			i--;
		}
	}
}

Edited by polyfrag, 29 April 2013 - 02:55 AM.


#27 phantom   Moderators   -  Reputation: 7109

Posted 29 April 2013 - 02:59 AM

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?

Edited by phantom, 29 April 2013 - 03:00 AM.


#28 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Posted 29 April 2013 - 03:25 AM

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.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#29 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 03:52 AM

 

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.

 


Edited by polyfrag, 29 April 2013 - 04:30 AM.


#30 kunos   Crossbones+   -  Reputation: 2205

Posted 29 April 2013 - 04:11 AM

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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#31 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Posted 29 April 2013 - 04:55 AM

Let's back up for a minute. What is this code for anyway? A game? A map renderer? Something else?

 

Personal note: did you use this same type of code when you made Pong?


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#32 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 05:23 AM

It's a game of course. I do have map renderers.

 

Personal note: did you use this same type of code when you made Pong?

 

I didn't pick up any of my mentioned habits until 2009.


Edited by polyfrag, 29 April 2013 - 05:50 AM.


#33 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 05:37 AM

Personal note: did you use this same type of code when you made Pong?

 

I was a lot more verbose and didn't use Hungarian notation. I didn't pick up any of my other mentioned habits until 2012.


Edited by Josh Petrie, 29 April 2013 - 01:50 PM.


#34 ranakor   Members   -  Reputation: 439

Posted 29 April 2013 - 06:01 AM

Didn't post to avoid being mean but seeing the turn of this thread, this code (the original post one) in an interview = immediate fail for me, you wouldn't last 1 minute. Even more so if you felt it was good or learn worthy, it's begginer level at best.



#35 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Posted 29 April 2013 - 06:04 AM

Didn't post to avoid being mean but seeing the turn of this thread, this code (the original post one) in an interview = immediate fail for me, you wouldn't last 1 minute. Even more so if you felt it was good or learn worthy, it's begginer level at best.

 

Just to be clear. There's nothing wrong with beginner level code. You just have to acknowledge that's beginner level code and accept that there are better ways to go about writing the code.


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#36 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 06:06 AM

Well so far I'm the only one posting about *innovations*.

#37 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 06:13 AM

Maybe you all see my code as wrong because you were taught a certain way. I'm self-taught. I learned from books. Don't know what current industry standards are.

#38 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 06:18 AM

What makes your code so good?

#39 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Posted 29 April 2013 - 06:23 AM

Well so far I'm the only one posting about *innovations*.

 

 

Maybe you all see my code as wrong because you were taught a certain way. I'm self-taught. I learned from books. Don't know what current industry standards are.

 

Well those aren't "innovations". Everyone has seen that code hundreds of times in their careers. It's the reason everyone is able to tell you better ways of writing and approaching your code.

 

Also, a lot (but not all) of the people in this thread have a CS degree and have been in the industry for years. So it's not only about what they were taught. They have years of real world experience of solving complex problems.

 

And lastly, don't claim ignorance as the reason for obstinately challenging everyone who has posted here. It would be best for you to step back and seriously take what they are saying into consideration.


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#40 polyfrag   Crossbones+   -  Reputation: 1825

Posted 29 April 2013 - 06:29 AM

This is what I'm taking in September at BCIT Computer Systems 2 year diploma followed by 2-year Bachelor of Technology with Game Development option http://www.bcit.ca/study/programs/cst http://www.bcit.ca/study/programs/825hbtech Do you think that will be as good as a Bachelor of Computer Science degree from a university? Good enough?


Edited by Josh Petrie, 29 April 2013 - 01:50 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS