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

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

Here are some examples:

 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 reason for this? I don’t understand what g_ means, and why you wouldn’t use more convenient names like “SwapChain”. Can someone explain?

+4
source share
1 answer

g_ usually denotes a global variable. The code (not .NET) provided in the Microsoft documentation and samples may use some version of Hungarian notation for historical reasons.

Systems Hungarian notation is not used very much, if at all, in C ++, since the compiler already knows the types of your variables. There is such a thing as the Hungarian Notation app, for which Joel Spolsky wrote an article about .

Now global variables are not a good idea in production code. Global variables are available everywhere, which means that they can be changed anytime, anywhere in your code. This easily becomes a nightmare for maintenance and debugging.

The reason you see them in the sample code is because the samples should be minimal, but compiled code snippets that demonstrate how to use the API. Note that the sample code also skips things like error checking for the same reason. The sample code does not necessarily demonstrate good coding methods or methods, although this is certainly possible.

In short, sample-style code becomes messy in any non-trivial application. In production code, you must create a structure and make the correct code design. This includes not using global variables.

+15
source

Source: https://habr.com/ru/post/1345980/


All Articles