{"id":744,"date":"2018-03-19T22:14:37","date_gmt":"2018-03-19T22:14:37","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=744"},"modified":"2023-05-31T15:55:36","modified_gmt":"2023-05-31T15:55:36","slug":"john-carmack-cpp-programming-legend","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/john-carmack-cpp-programming-legend\/","title":{"rendered":"John Carmack: A C++ Programming Legend"},"content":{"rendered":"<p>Have you already seen a basketball or a soccer player plays\u00a0a simple yet effective game\u00a0to such a point that you say: Why couldn&#8217;t everybody play like him, he uses only easy techniques?<\/p>\n<p>And as C++ programmer I had the same remark when exploring the John Carmack\u00a0source code.\u00a0It&#8217;s so simple, we wonder why we can &#8216;t develop like him.<\/p>\n<p>Let&#8217;s explore some Doom3 source code choices and try to understand why the code even if it&#8217;s simple, it&#8217;s very efficient.<\/p>\n<p>On November 23, 2011 id Software maintained the tradition and it released the\u00a0<a href=\"https:\/\/github.com\/dhewm\/dhewm3\">source code<\/a>\u00a0of their previous engine. This source code was reviewed by many developers, here\u2019s as an example of Doom3 feedback from fabien (<a href=\"http:\/\/fabiensanglard.net\/doom3_bfg\/\">orginal source<\/a>):<!--more--><\/p>\n<blockquote><p>Doom 3 BFG is written in C++, a language so vast that it can be used to generate great code but also abominations that will\u00a0make your eyes bleed. Fortunately, id Software settled for a C++ subset close to \u201cC with Classes\u201d which flows down the brain with little resistance:<\/p>\n<ul>\n<li>No exceptions.<\/li>\n<li>No References (use pointers).<\/li>\n<li>Minimal usage of templates.<\/li>\n<li>Const everywhere.<\/li>\n<li>Classes.<\/li>\n<li>Polymorphism.<\/li>\n<li>Inheritance.<\/li>\n<\/ul>\n<\/blockquote>\n<p>To resume only a subset of the c++98 standard is used. And Here are some design choices of Doom3:<\/p>\n<p><strong>1 \u2013 Provides a common base class with useful services.<\/strong><\/p>\n<p>Many classes inherit from the idClass:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1279\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom10.png\" alt=\"doom10\" width=\"393\" height=\"496\" \/><\/a><\/p>\n<p>The idClass\u00a0\u00a0provides the following services:<\/p>\n<ol>\n<li>Instance creation.<\/li>\n<li>Type info management.<\/li>\n<li>Event management.<\/li>\n<\/ol>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1280\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom11.png\" alt=\"doom11\" width=\"392\" height=\"448\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>2- Make easy the string manipulation<\/strong><\/p>\n<p>Generally, the string is the most used type in a project, many treatments are done using them, and we need functions to manipulate them.<\/p>\n<p>Doom3 defines the idstr class which contains almost all useful methods to manipulate strings, no need to define your own method as the case of many string classes provided by other frameworks.<\/p>\n<p><strong>3- The source code is highly decoupled from the GUI framework (MFC)<\/strong><\/p>\n<p>In many projects using MFC, the code is highly coupled with their types, and you can find types\u00a0from MFC everywhere in the code.<\/p>\n<p>In Doom3, The code\u00a0is highly decoupled with MFC, only GUI classes have a direct dependency on it. As shown by this following CQLinq query:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1117\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom3.png\" alt=\"doom3\" width=\"416\" height=\"496\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>This choice has a big impact on the productivity. Indeed, only the Gui developers must care about\u00a0the MFC framework, and for the other developers, it\u2019s not mandatory to waste time with MFC.<\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4- It provides a very good utility library (idlib)<\/strong><\/p>\n<p>In almost all projects the most used types are utility classes, as shown by the result of this following query:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1119\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom4.png\" alt=\"doom4\" width=\"414\" height=\"496\" \/><\/a><\/p>\n<p>As we can observe the most used are utility ones. If \u00a0C++ developers don\u2019t\u00a0use a good framework for utilities, they spend most of their development time to fight with the technical layer.<\/p>\n<p>idlib provides useful classes with all needed methods to treat string, containers, and memory. Which facilitate the work of developers and let them focus more on the game\u00a0logic.<\/p>\n<p><strong>5- The implementation is very easy to understand<\/strong><\/p>\n<p>Doom3 implements a hardcoded compiler, and as known by C++ developers, it\u2019s not an easy task to develop parsers and compilers. However, the implementation of the Doom3 is very easy to understand\u00a0and its\u00a0code is very clean.<\/p>\n<p>Here\u2019s the dependency graph of the classes used by the compiler:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom16.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1284\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom16.png\" alt=\"doom16\" width=\"726\" height=\"354\" \/><\/a><\/p>\n<p>And here\u2019s a code snippet from the compiler source code:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1283\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/doom15.png\" alt=\"doom15\" width=\"676\" height=\"513\" \/><\/a><\/p>\n<p>We already study the source code of many parsers and compilers. But it\u2019s the first time we discover a compiler with a source\u00a0code\u00a0very easy to be understood, it\u2019s the same for the whole Doom3 source code. It\u2019s magic. When we explore the Doom3 source code, we can\u2019t say: WOW it\u2019s beautiful!<\/p>\n<p>To resume the Doom3 source code is very clean, easy to understand and maintain and use only a subset of the standard. No advanced techniques were used and it respects a basic best practices of designing, naming\u00a0and formatting the code.<\/p>\n<p>We can say that the John Carmack secret was the KISS principle as defined in wikipedia:<\/p>\n<pre><b>KISS<\/b>\u00a0is an\u00a0<a class=\"mw-redirect\" title=\"Acronym and initialism\" href=\"https:\/\/en.wikipedia.org\/wiki\/Acronym_and_initialism\">acronym<\/a>\u00a0for \"<b>Keep it simple, stupid<\/b>\" as a design principle noted by the\u00a0<a class=\"mw-redirect\" title=\"U.S. Navy\" href=\"https:\/\/en.wikipedia.org\/wiki\/U.S._Navy\">U.S. Navy<\/a>\u00a0in 1960.<sup id=\"cite_ref-TDal_1-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-TDal-1\">[1]<\/a><\/sup><sup id=\"cite_ref-EPar_2-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-EPar-2\">[2]<\/a><\/sup>\u00a0The KISS principle states that most systems work best if they are kept simple rather than made complicated; therefore\u00a0<a title=\"Simplicity\" href=\"https:\/\/en.wikipedia.org\/wiki\/Simplicity\">simplicity<\/a>\u00a0should be a key goal in\u00a0<a title=\"Design\" href=\"https:\/\/en.wikipedia.org\/wiki\/Design\">design<\/a>\u00a0and unnecessary complexity should be avoided. The phrase has been associated with aircraft engineer\u00a0<a title=\"Kelly Johnson (engineer)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Kelly_Johnson_(engineer)\">Kelly Johnson<\/a>\u00a0(1910\u20131990).<sup id=\"cite_ref-BRich_3-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-BRich-3\">[3]<\/a><\/sup>\u00a0The term \"KISS principle\" was in popular use by 1970.<sup id=\"cite_ref-Pit_4-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-Pit-4\">[4]<\/a><\/sup>\u00a0Variations on the phrase include: \"Keep it simple, silly\", \"keep it short and simple\", \"keep it simple and straightforward\",<sup id=\"cite_ref-monash_5-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-monash-5\">[5]<\/a><\/sup>\u00a0\"keep it small and simple\" and \"keep it stupid, simple\".<sup id=\"cite_ref-6\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle#cite_note-6\">[6]<\/a><\/sup><\/pre>\n<p>What&#8217;s interesting about this definition is the following assertion:<\/p>\n<p><em>The KISS principle states that most systems work best if they are kept simple rather than made complicated.<\/em><\/p>\n<p><strong>What lessons to learn when adopting the new C++ standards?<\/strong><\/p>\n<p>The new standards introduced many new interesting features. Think that using\u00a0all these features will make your code very efficient it&#8217;s a bad idea.\u00a0 many new features are more useful to develop the generic libraries especially all the features related to the generic programming.<\/p>\n<p>Don&#8217;t force yourself to use all the new features, use a feature only if it&#8217;s absolutely needed and contribute to making your code more efficient.For example, this interesting\u00a0<a href=\"http:\/\/www.acodersjourney.com\/2016\/02\/c-11-auto\/\">post\u00a0<\/a>talks about the drawbacks of the overuse of the auto keyword.<\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you already seen a basketball or a soccer player plays\u00a0a simple yet effective game\u00a0to such a point that you say: Why couldn&#8217;t everybody play like him, he uses only easy techniques? And as C++ programmer I had the same remark when exploring the John Carmack\u00a0source code.\u00a0It&#8217;s so simple, we wonder why we can &#8216;t &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/john-carmack-cpp-programming-legend\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;John Carmack: A C++ Programming Legend&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,67,13,5,186,137],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-c-2","tag-cpp","tag-design","tag-john-carmack","tag-programming"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":26,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/744\/revisions\/1481"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}