Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-spur1.glorb.com!newsread.glorb.com!.POSTED!not-for-mail From: Jeff-Relf.Me <@.> Newsgroups: comp.os.linux.advocacy,sci.physics,comp.lang.c++ Subject: My C++ Coding Standards ( apologies to Bjarne Stroustrup ). Date: Fri, 23 Dec 2016 17:06:52 -0800 (Seattle) Organization: Glorb Internet Services, http://www.glorb.com Lines: 83 Message-ID: References: NNTP-Posting-Host: /zlDkqjrFG1Uj/denlv3LA.450.posting.glorb.com Content-Type: Text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@glorb.com NNTP-Posting-Date: Sat, 24 Dec 2016 01:06:50 +0000 (UTC) User-Agent: Jeff-Relf.Me/X.ZIP X-Notice: Scanned by Mr. Bill Xref: csiph.com comp.os.linux.advocacy:386170 sci.physics:610246 comp.lang.c++:47578 My C++ Coding Standards ( apologies to Bjarne Stroustrup ): 1. If everything ( but the top node ) were local, then you'd be spending all your time traversing trees, like an ant, instead of doing real work. So use Global Variables/Functions early and often. This, the common way, is ambiguous: if ( HandleEvents(1) ) ... Instead, use globals (below), like this: WaitForClick = 1, HandleEvents(); if ( Click ) ... Globals: int WaitForClick, Click ; void HandleEvents() { MSG Msg ; GetMsg: PeekMessage( &Msg, 0, 0, 0, PM_REMOVE ); Click = Msg.message == WM_LBUTTONDOWN ; // Handle system events, like WM_NCACTIVATE... Blah Blah. // When WaitForClick == 1, Exit if-and-only-if there's a Click: if ( Click || !WaitForClick ) { WaitForClick = 0 ; return ; } // Draw Stuff... Blah Blah. goto GetMsg ; } 2. InLine Conditionals ( Bool ? True() : False() ) are nice and concise. For example: inline int TheSmaller( int X, int Y ) { return X < Y ? X : Y ; } #define FlagTheNym /* 'V' is for Visitor. */ \ Nym.Trusted ? 'T' : Nym.Dropped ? 'D' : Nym.Kid ? 'K' : Nym.Adult ? 'A' : 'V' 3. Simplify Odd system calls, like this: wchar aString[999]; Str( aString, L"A 32 bit, unsigned Random Integer: %I64d", Rand32 ); Globals: typedef wchar_t wchar ; unsigned int uiRand ; #define Rand32 ( rand_s( &uiRand ), uiRand ) // Seeding is implicit. #define Str swprintf // Print to a Unicode string, "printf" style. 4. A variable's declared type isn't always appropriate; so Type Cast. #define szStr (int)wcslen // Return the length of a Unicode string. #define Zero( X ) memset( & X, 0, sizeof X ) // Implicit cast to (void*). Zero( aStruct ); 5. Create fewer/larger functions. Put everything in a single, large .CPP file -- don't create .H files. 6. Local, ShortTerm variables are nice. Like: wchar _T[999], *P = _T ; Loop(3) { _Loop(4) P += Str( P, L"( %d, %d )%s", J, JJ, J == eJ ? L"\n" : L" " ); } printf( _T ); Globals: #define Loop( N ) int eJ = ( N ) - 1, J = -1 ; while ( ++J <= eJ ) #define _Loop( N ) int JJ = J, eJ = ( N ) - 1, J = -1 ; while ( ++J <= eJ ) #define Str swprintf typedef wchar_t wchar ; Output: ( 0, 0 ) ( 1, 0 ) ( 2, 0 ) ( 3, 0 ) ( 0, 1 ) ( 1, 1 ) ( 2, 1 ) ( 3, 1 ) ( 0, 2 ) ( 1, 2 ) ( 2, 2 ) ( 3, 2 ) See X.CPP in "Jeff-Relf.Me/X.ZIP".