Handmade Hero»Forums»Code
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
Questions and Answers
Edited by Casey Muratori on
Where can I learn more about code optimization?
Well, obviously there are Handmade Hero episodes that give light introductions. But I'm not an optimization guy, so if you're looking for the hardcore stuff, I'd probably head over to Fabian's blog for a start:

https://fgiesen.wordpress.com/201...izing-sw-occlusion-culling-index/

What do you think about Linux source code?
I don't know, I've never read it.

Are "goto" statements bad programming technique?
No. Anything tells a CPU to do something that CPU was made to do can't be a bad programming technique in my opinion. I don't use gotos in my code only because I was taught not to do so very early on and never really got around to experimenting with them after I learned that most programming dogma is wrong. I suspect I will find some good uses for gotos some day, if I don't move on to another language before then :)

What are those black things you wear on your wrists?
They are medi-active wrist braces, which are (sadly) discontinued as far as I know.

What do you think about TDD and Extreme Programming?
Test Driven Development is fine when it is used appropriately. If misapplied it can waste a lot of time and lead to false confidence, so like all tools, you have to know when to use it and when not to.

"Extreme Programming" is kind of just a giant jumble of things at this point, so I'm not sure how to have an opinion about it. In general, I don't really like these sorts of fad things, because good programming is just good programming, and it doesn't really have to do with organizational process. Trying to institutionalize a process so that you can have bad programmers work effectively just seems like a waste of time to me, and if your programmers are good then institutionalized processes just get in the way.

Why are you not using version control programs for HmH?
Well, there's really no use for them on HmH. Since I have to ship the source code every night to the preorders, there's a ZIP file of every day's source code. If you ever want to view some older version, you can just go get it. So I don't really have any need for it. However, people asked for the code to be available via GitHub, so there is now a GitHub repository that people who pre-order can join.

Do you know any other good places where I can find live coding streams?
I don't know what the best places for live coding streams are, but I do know that two of the people often mentioned on Handmade Hero (Sean Barrett and Jonathan BLow) both have live streams on Twitch (nothings2 and naysayer88).

What do You think about CLion / Microsoft Visual Code / etc.?
I think they're awful. I wish somebody made good development tools for game programmers, but nobody does, so I just use the least bad thing, which for me means GNU Emacs and Microsoft Visual Studio's debugger. I don't like either of them but they're the best things I've found.

Why don't you like the Standard C++ Library / Standard Template Library / Boost / etc.
Because they're extremely low quality code and I don't want them in my codebase.

Why is OOP bad?
Object Oriented Programming is bad because of the "oriented" part. You never want to "orient" your programming around objects, because objects are the byproduct of systems, not the primary element. The format for the objects - the data - on either side of an algorithm is just whatever is most efficient and sensible for that algoritm's function and its integration into the overall system. OOP is a methodology that is fundamentally at odds with the way computers actually work, and I strongly recommend against it.

If I use classes just as C-structs but with methods inside it, with no private data, inheritance, templates or other bad stuff, is that still bad?
The problem with OOP is not that you type the word "class" instead of "struct". It's the "oriented" part that is the problem, not the fact that there might be objects. You can make a totally sensible program with a bunch of classes in it. It's just unlikely you will make such a program with object-oriented thinking. You are much more likely to arrive there, even though there are "objects", by using more traditional algorithm-centric thinking.

Why are the game developers still using C++ if it's so awful?
Because there's really nothing else to use. C++ is one of the only widely-supported programming languages that still allows complete low-level control of what the CPU is going to do. Someday, it would be great if game developers could switch to some new low-level-capable language that had actual well-designed high-level features, but until such a language emerges, we're stuck with C++ and 30 years of Stroustrup-initiated garbage piled on top of a very old, but great for its day, language (C).

Why is Windows so popular if it is so bad?
Because it's still better than the other operating system options out there. Sadly, we're living in pretty dire days for operating systems. There's really no operating system currently in existence that I am happy to run. They are all extremely depressing and unfortunate.

Is there any decent debugger for Linux-based OS that behaves like Visual Studio?
Nope. The visual debuggers on Linux are universally awful. They often can't even inspect data properly, with simple things like unions or certain types of locals leaving them completely confused with how to display data, even though it is displayed properly in the command line GDB (which they all use as a back-end). They're invariably extremely slow, often can't debug external executables, and have a lot of instability or incompatibility with hybrid graphics GPUs.

Should I read Intel software developer manual?
Sure, if you want to learn about the x64 architecture.

With a portfolio of self-taught programming side projects, do you think having a Mechanical Engineering degree is enough to get a job in game programming? In addition to that, does having a job in a non-game programming position help my chances of getting noticed?
I have no idea. Certainly if I was interviewing someone, I would be focusing on their actual programming skills, and I wouldn't care what they had a degree in or where they were currently working. But I'm not the common case for the person who would be screening/interviewing new hires, so the real question is what do the majority of game programmer jobs have as a screening process, and that's something I really have no familiarity with. I've only ever been involved in the hiring of specialized, high-end programmers for very small programming teams, so my experience is very atypical.

Is there another way to migrate over to game programming without going back to school for a programming degree?
Sure. Many programmers are self taught, and certainly it seems that some of the best programmers, even if they did go to school, learned most of what they know about programming on their own. So going to school is largely optional, but obviously it's going to depend on the person.

If I were to go back to school for a programming related degree, does it matter if I get a Bachelors or Masters in CS? A BS looks like I would learn more applicable stuff, but would take longer than a Masters in my situation.
I never went to college, so I literally have no idea. Obviously if I were hiring a programmer I would not care at all about what kind of degree they have or where they went to school, I would be looking very carefully at their programming myself and making a specific judgement about that. But, I am not the common case for the person who would be screening/interviewing programmers in the game industry, so really this question is better asked of an HR recruiter at a company like Electronic Arts, I would think.

This isn't a question specific to my situation, but I read that programmers in the game industry are generally let go at the end of a project (when the game is shipped). Is that true?
That's not the kind of thing I hear about happening with any regularity, but I'm sure it does happen from time to time. I think the liklihood might depend a bit on the definition of "programmer". In my experience, highly technical programmers don't generally get laid off if they're any good. They are usually in very high demand, because there just aren't that many of then. Non-technical programmers - sometimes called "scripters" because they tend to program in a "scripting language" for the game - might be more likely to be hired and laid off as temporary workers since they might be considered as part of the asset production team, which is often ramped up on a temporary basis when a game is nearing completion.

That said, I don't spend much time looking into things like this, and don't know much about the hiring and firing practices of large game companies, as I have never worked for one.

Why don't you (seem to) use references ever? Why not use references when the function can't function/do anything without a non-null-pointer?
References are an entirely superfluous programming construct that is substantively the same as a pointer but without the ability to reassign its target. I don't see any need for this as a language feature, so I don't use it, thus minimizing the number of language features upon which I rely. I often do this, so if I want to, for example, write my own compiler to compile or translate my code in some way, I would only have to support things that are actually useful to me.

But you talked about pointer aliasing which would have been a non issue with references right?
Generally speaking, references have all the same aliasing issues that pointers have. The compiler still has no idea if a referenced struct and a referenced int might collide (ie., if the reference int is contained in the struct).

If C++ is the best around how can it be bad?
I have no idea what that means.

What did Bjarne Stroustrup do to hurt you?
He undertook a rather extensive and deliberate PR job to make C++ the successor to C, which succeeded, but he was a terrible language designer and so we ended up with a massive, complex language whose only good parts are essentially those that were in C originally. This has made it a real chore to do systems programming over the past few decades, since we are all basically stuck with a language whose only evolution since the 1970s has been for the decidedly worse.

If he was just some guy who made a lousy language, there'd be no hard feelings. But he was a guy who made a lousy language and spent all his time selling it really hard, which unsurprisingly turns out to work. It's almost like marketing is more important than development! :P

- Casey
Mārtiņš Možeiko
2559 posts / 2 projects
Questions and Answers
For hardcore optimizations Agners optimization manuals are good: http://www.agner.org/optimize/
He has stuff on how to optimize C/C++ code on high level, how to do optimizations on assembly level and even on microarchitecture level for different AMD and Intel CPU's.
Andrew Bromage
183 posts / 1 project
Research engineer, resident maths nerd (Erdős number 3).
Questions and Answers
cmuratori
Are "goto" statements bad programming technique?
No. Anything tells a CPU to do something that CPU was made to do can't be a bad programming technique in my opinion.

To understand why "Go To statement [is] considered harmful", you need to understand the context in which Dijkstra wrote his paper. This was the time of the "software crisis", when the software business was fundamentally changing. If you've seen that Bret Victor talk, you know what I'm talking about.

Dijkstra was reacting in particular to a style of programming that we now call "spaghetti coding". You can still see it in old (pre-1970) Fortran code if you look at some of it. It's a style of programming that is exactly what you'd expect if programmers started on assembly language. Such programs often had to be documented with flowcharts, because there was no other way to understand what a function or procedure actually did.

What Dijkstra advocated could be considered a forerunner of data-oriented design. He argued that code follows data, and structured programming is the obvious way to process structured data. If you have an array, for example, then the most sane code to process this is a loop.

It says something that everyone remembers the Dijkstra paper, but non-functional programmers seem to have forgotten Lambda: The Ultimate Goto which was part of the same discussion. The argument here is that many programmers of the day were forced to use gotos because the compilers of the day sucked, and if the people who develop programming tools invested a little thought in what programmers actually want to do, there wouldn't be a problem in the first place. Where have we heard that complaint before?

This paper has one of my favourite quotes from any piece of computer science research ever: "This is like saying that cars with square wheels are all right because transportation is worth a bumpy ride: we really ought instead to concentrate on improving our wheels."

There are also programming language semantics issues with goto, of course. Reducible flow graphs (roughly speaking, it's the property that every loop has a single entry point) are theoretically and practically better for optimisation. Disallowing jumps into the middle of a scope from outside (see Lua's rules for goto, as an example) eliminates a large class of errors. However, these are arguments against certain uses of goto, not against goto in general.

I have two main use cases for goto in my code: State machines (lexical analysers in particular), and complex error or transaction handling in C. See the Linux pipe implementation for some good examples.

Yes, yes, I know, some of these could be implemented as RAII in C++. Go away.
Mike T
27 posts
Questions and Answers
Why are the game developers still using C++ if it's so awful?
Because there's really nothing else to use. C++ is one of the only widely-supported,

Don't leave us hanging Casey! :)
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
Questions and Answers
Thanks for the catch - fixed!

- Casey
Jim R. Didriksen
63 posts
Questions and Answers
Why don't you (seem to) use references ever? Why not use references when the function can't function/do anything without a non-null-pointer?
No null pointers, no -> for . , quick code change back and fort between passing a Ref. and a Object (only adding or removing & in the function header).

Is there some ASM or Compiler penalty?

I'm on day 14 now following the code, haven't seen you talk about references but I have done the intro-C and 13.5f days of HH in 3.5f days so I might have missed it. But you talked about pointer aliasing which would have been a non issue with references right?


If C++ is the best around how can it be bad?

Disclaimer: I'm still pretty new to C/C++ (3 to 12 months in) I don't actually have a strong opinion I just have all the questions.

When I catch up with the live stream will there be a diploma?

PS: What did Bjarne do to hurt you? (Stroustrup)

Thanks for this project, I'm really learning and you're Stealthily making me a C not C++ programmer.

- Jim RMHD
Mārtiņš Možeiko
2559 posts / 2 projects
Questions and Answers
Edited by Mārtiņš Možeiko on
C++ references doesn't help compiler in general case. Aliasing also applies to references. Because reference and pointer is the same thing for compiler - just a memory address to another variable/location. It's only that syntax differs.

There are so many disadvantages to references:
* no way to have NULL reference. Sometimes it is useful for default arguments or loops
* no way to reassign reference. Sometimes it is useful in loops
* no way to have reference to reference. With pointers you can have as many indirections as you want

So quickly changing code between reference and object in a lot of cases won't work.

As far as I know they help compiler only in one specific case - with multiple inheritance:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
struct Base1 { int x; };
struct Base2 { int y; };

struct Derived : Base1, Base2 {};

void foo(Base2* obj);

void aaaaa(Derived* obj)
{
  foo(obj);
}

void bbbbb(Derived& obj)
{
  foo(&obj);
}


Compiler produces following code for aaaaa:
1
2
3
4
5
6
_Z5aaaaaP7Derived:
	leaq	4(%rcx), %rax
	testq	%rcx, %rcx
	movl	$0, %ecx
	cmovne	%rax, %rcx
	jmp	_Z3fooP5Base2


and following code b:
1
2
3
_Z5bbbbbR7Derived:
	addq	$4, %rcx
	jmp	_Z3fooP5Base2


This is because pointer passed to aaaaa function can be NULL and compiler needs to check that before casting it to the base object (if it is NULL then it must remain NULL). Reference cannot be NULL so compiler simply casts passed argument without any check.

But if you are using multiple inheritance, then you probably have more problems than to worry about performance for pointers vs references :)
Jim R. Didriksen
63 posts
Questions and Answers
aha okay, I see.
I have a feeling this is something I'll need to learn from a personal f'up.
Ameen Sayegh
51 posts
Questions and Answers
I hear people ask you all the time "What do you think of X language (python, java, php, ...etc)?"

This is a main question and I think it need to be in here :)
Timothy McCarthy
52 posts
Questions and Answers
Edited by Timothy McCarthy on Reason: grammar
My question is: "How to ask questions about older stream issues on the forum?"

People like me (and of course everyone is like me :P) who have started going through the project a little later face a problem when asking forum questions. The question is old and may be resolved in subsequent streams; this may inhibit asking the question or generate the reply, "Oh, that was fixed in..." Reading through the older posts is always a Good Thing (R) to catch up but tends to inhibit interaction. Inhibiting interaction or asking questions are not intended here, I assume. If I understand the project model, people will constantly be going through the code at different places. so this issue will be persistent.

Not having much experience with online forums, game programming, or the game programming community qualifies me to present a definitive solution (...er, suggestion...um, idea.) Request a prefix "Day XXXX:..." on questions for older stream topics to help readers filter and focus on topics.

An idea.
(A brilliant idea but just an idea.)

- tim
Jim R. Didriksen
63 posts
Questions and Answers
Edited by Jim R. Didriksen on
ref. RomulusTFM (Genius Tim)

I was thinking about that just yesterday, what I have been doing is using the YouTube comments, but I'm on day 60 now and their relevance seems to have dried up probably from people using the forums but I'm having trouble searching the forums for and official thread...

So what I was thinking was, what if there is a Official thread per day (I know there are some in the past) and then there is either a link from the episode guide or even the thread could be embedded under the the notes under the video?

clarification : Making the per episode forum threads comment threads for the episodes.

I wouldn't mind doing some of the drudge work, since there will be some initially to catch up.
ndh
Andreas Haferburg
2 posts
Questions and Answers
What tools do you use (hardware and software)?
What techniques/guidelines do you use to make and keep your builds fast?
Mārtiņš Možeiko
2559 posts / 2 projects
Questions and Answers
If you are watching Handmade Hero stream you know what Casey does to keep builds fast - compile everything as single translation unit (aka unity build).
1 posts
Questions and Answers
Casey, why don't you use a plain C?
Mārtiņš Možeiko
2559 posts / 2 projects
Questions and Answers
Because he likes to use few C++ features that are not in C - operator overloading (+ / * for vector types), and function overloading (have same name for different functions that have different arguments).