Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Microsoft use the "g_" naming convention with its DirectX10 pipeline variables?

Most of the sample code from Microsoft's DirectX SDK includes variables which use the g_ prefix for Windows API variables, as well as DirectX pipeline variables, such as the swapchain.

Here are some samples:

D3D10_DRIVER_TYPE           g_driverType;
ID3D10Device*               g_pd3dDevice;
IDXGISwapChain*             g_pSwapChain;
ID3D10RenderTargetView*     g_pRenderTargetView;
ID3D10Effect*               g_pEffect;
ID3D10EffectTechnique*      g_pTechnique;
ID3D10InputLayout*          g_pVertexLayout;
ID3D10Buffer*               g_pVertexBuffer;
ID3D10Buffer*               g_pIndexBuffer;
ID3D10EffectMatrixVariable* g_pWorldVariable;
ID3D10EffectMatrixVariable* g_pViewVariable;
ID3D10EffectMatrixVariable* g_pProjectionVariable;
D3DXMATRIX                  g_World;
D3DXMATRIX                  g_View;
D3DXMATRIX                  g_Projection;

What is the reasoning behind this? I'm not getting what the g_ signifies, and why you wouldn't use more convenient names like "SwapChain." Can anyone explain?

like image 905
Darkenor Avatar asked Mar 31 '11 03:03

Darkenor


1 Answers

The g_ typically stands for global variable. The (non-.NET) code presented in Microsoft documentation and samples may use some variant of Hungarian notation for historical reasons.

Systems Hungarian notation isn't used very much, if at all, in C++ since the compiler already knows the types of your variables. There is such a thing as Applications Hungarian notation, for which Joel Spolsky has written an article about.

Now, global variables are not a good idea in production code. Global variables are accessible everywhere, meaning that they can be modified whenever and wherever in your code. That easily becomes a maintenance and debugging nightmare.

The reason why you see them in sample code is because samples are meant to be minimal but compilable code snippets that demonstrates how to use the API. Notice that sample code also omits things like error-checking for the same reason. Sample code do not necessarily demonstrate good coding techniques or practices, although it's certainly possible.

In a nutshell, "sample-style" code becomes messy real quick in any non-trivial application. In production code, you should be setting up a framework and do a proper code design. That includes not using global variables.

like image 191
In silico Avatar answered Sep 22 '22 00:09

In silico