Live data from Hacker News

John Carmack's comment on Doom 3's code style

kotaku.com

41–50 of 210 posts

Re: John Carmack's comment on Doom 3's code style

#41
I've loved John's code since I saw it first time when the original Quake was leaked from their FTP site through IP spoofing. I was just a kid at that time, and it was an amazing experience to hack it.

Yet now, the first example that I saw in this article hurts my eyes. Compare:

  for ( i = 0; i numVerts ; i++ ) {
      dot = plane.Distance( in->verts[i] );
      dists[i] = dot;
      if ( dot  LIGHT_CLIP_EPSILON ) {
          sides[i] = SIDE_FRONT;
      } else {
          sides[i]  = SIDE_ON;
      }
      counts[sides[i]]++;
  }
Versus:

  for(i = 0; i numVerts; i++)
  {
      dot = plane.Distance(in->verts[i]);
      
      dists[i] = dot;
      sides[i] = dot  LIGHT_CLIP_EPSILON   ?  SIDE_FRONT :
                                               SIDE_ON;

      counts[sides[i]]++;
  }

Re: John Carmack's comment on Doom 3's code style

#42

> C++ code can quickly get unruly and ugly without diligence on the part of the programmers. To see how bad things can get, check out the STL source code. Microsoft's and GCC's[5] STL implementations are probably the ugliest source code I've ever seen. Even when programmers take extreme care to make their template code as readable as possible it's still a complete mess. Take a look at Andrei Alexandrescu's Loki libra…

Considering how many languages these days completely eschew the header/body paradigm, I'm not sure that "head-only" programming is such an obvious mistake.

Re: John Carmack's comment on Doom 3's code style

#43
post #35

Earlier quoted context omitted.

Oh, I get the concept. I've just been doing this for a while and see it fall part constantly. The problem is always that what is clear to you isn't clear to everyone else, including yourself somewhere down the road. And decomposing everything to atoms tends to lead to a mess of indirection. Clarity can usually be addressed with longer variable or method names, but there's a strong culture against that in nearly ever…

> Clarity can usually be addressed with longer variable or method names, but there's a strong culture against that in nearly ever programming community. It would be interesting to see a list of those that discourage long method and variable names. I haven't seen anything that suggests that Java, Ruby, Python, Haskell, Kotlin, Scala, PHP, Groovy etc. discourage long variable and method names.

IntelliJ has inspections for long class and method names. A lot of Java devs run that. Long names are frequently derided in the Ruby community as being Java-like. I can't speak to Haskell at all. But generally whenever something needs to be typed frequently, it tends to be shortened. My favorite is when vowels are deemed too onerous.

Re: John Carmack's comment on Doom 3's code style

#44
post #11

As someone who has worked with the Doom 3 source code for a mod, I have the opposite opinion. The code very clearly shows a programming team (or programmer) in the process of transitioning from old-school C to C++. Most functions have a huge blob of variable declarations right at the top, as was once necessary in C, even though these variables aren't used until later, or possibly even at all. Usage of const is minima…

You might be correct, but I cannot say, having never read the source code. Since it was Carmack's first project in C++, I am inclined to believe you.

But would you agree that the practices mentioned in the article are what the team got right? You can remove the author's gushing about the code's beauty and have a substantive article left. For instance, the section on vertical spacing had me reconsidering my own style. The discussion of method signatures seemed pretty solid, too.

Re: John Carmack's comment on Doom 3's code style

#45

A lot of the practices here are enshrined in the Google C++ styleguide: http://google-styleguide.googlecode.com/svn/trunk/cppguide.x... The author's first point about establishing conventions so that you can re-use the code that works with those conventions is very important. At Google, nobody writes code to serialize/deserialize bytes, because the default answer is just "use protobufs". Nobody writes low-level commu…

> A lot of the practices here are enshrined in the Google C++ styleguide:

with the notable exception of lining up things horizontally, which tends to be frowned on.

Re: John Carmack's comment on Doom 3's code style

#46

> C++ code can quickly get unruly and ugly without diligence on the part of the programmers. To see how bad things can get, check out the STL source code. Microsoft's and GCC's[5] STL implementations are probably the ugliest source code I've ever seen. Even when programmers take extreme care to make their template code as readable as possible it's still a complete mess. Take a look at Andrei Alexandrescu's Loki libra…

when do you consider someone a guru?

Re: John Carmack's comment on Doom 3's code style

#47

Earlier quoted context omitted.

What do you find so disagreeable about collecting variables at the top of a function? For the most part, I like having all the variable declarations at the top, so it's easy to see what names are in what scope.

I was reading '21st Century C' (I strongly recommend it for anyone writing C on a regular basis), and the author also argued against declaring all the variables up front. I don't quite know why, but I had a very strong reaction against it. As far as best practices go, I would try to keep every function small enough that you can find all the stack variables easily. If the function gets too hairy, refactor it so you ca…

The only time you actually need to declare variables up front nowadays is if you have a goto that would otherwise skip over stack-allocated variable declarations. It's only a problem on certain compilers, but there are enough of them that it's best to keep variable declarations up front in such cases.

Re: John Carmack's comment on Doom 3's code style

#48
post #22

I don't have much experience with C++ codebases, but is this really "exceptional beauty"? The majority of the things he comments on could be enforced with a code-formatter.

Considering the codebase is around a decade old, how many code formatters existed for C/C++ back then?

I am asking because I've only used gofmt to format Go code and infact it was gofmt which introduced the whole concept to me.

Re: John Carmack's comment on Doom 3's code style

#49
post #11

As someone who has worked with the Doom 3 source code for a mod, I have the opposite opinion. The code very clearly shows a programming team (or programmer) in the process of transitioning from old-school C to C++. Most functions have a huge blob of variable declarations right at the top, as was once necessary in C, even though these variables aren't used until later, or possibly even at all. Usage of const is minima…

What do you find so disagreeable about collecting variables at the top of a function? For the most part, I like having all the variable declarations at the top, so it's easy to see what names are in what scope.

This is not JavaScript, where a variable defined inside a function always has the entire function as its scope, and so it's confusing not to have it on top. In C++, a variable is not in scope until it's declared, so you can just declare it when you're going to start using it.

If your function is five pages long, and in the fifth page you can't remember what's in scope and you have to reread through the entire function above, it might seem that having all variable declarations at the top would help, because you'd only have one place to look at. But the real problem there is that the function is too long, and it should be refactored instead.

Re: John Carmack's comment on Doom 3's code style

#50

I've loved John's code since I saw it first time when the original Quake was leaked from their FTP site through IP spoofing. I was just a kid at that time, and it was an amazing experience to hack it. Yet now, the first example that I saw in this article hurts my eyes. Compare: for ( i = 0; i numVerts ; i++ ) { dot = plane.Distance( in->verts[i] ); dists[i] = dot; if ( dot LIGHT_CLIP_EPSILON ) { sides[i] = SIDE_FRONT…

Bravo. That comparison makes worrying about vertical space seem ludicrous.
Post reply on HN