Exceptions to the Rules
The coding conventions described above are mandatory. However, like all good rules, these sometimes have exceptions, which we discuss here.
Existing Non-conformant Code
You may diverge from the rules when dealing with code that does not conform to this style guide.If you find yourself modifying code that was written to specifications other than those presented by this guide, you may have to diverge from these rules in order to stay consistent with the local conventions in that code. If you are in doubt about how to do this, ask the original author or the person currently responsible for the code. Remember that consistency includes local consistency, too.
Windows Code
Windows programmers have developed their own set of coding conventions, mainly derived from the conventions in Windows headers and other Microsoft code. We want to make it easy for anyone to understand your code, so we have a single set of guidelines for everyone writing C++ on any platform.It is worth reiterating a few of the guidelines that you might forget if you are used to the prevalent Windows style:
- Do not use Hungarian notation (for example, naming an
integer
iNum
). Use the Google naming conventions, including the.cc
extension for source files. - Windows defines many of its own synonyms for primitive
types, such as
DWORD
,HANDLE
, etc. It is perfectly acceptable, and encouraged, that you use these types when calling Windows API functions. Even so, keep as close as you can to the underlying C++ types. For example, useconst TCHAR *
instead ofLPCTSTR
. - When compiling with Microsoft Visual C++, set the compiler to warning level 3 or higher, and treat all warnings as errors.
- Do not use
#pragma once
; instead use the standard Google include guards. The path in the include guards should be relative to the top of your project tree. - In fact, do not use any nonstandard extensions, like
#pragma
and__declspec
, unless you absolutely must. Using__declspec(dllimport)
and__declspec(dllexport)
is allowed; however, you must use them through macros such asDLLIMPORT
andDLLEXPORT
, so that someone can easily disable the extensions if they share the code.
However, there are just a few rules that we occasionally need to break on Windows:
- Normally we forbid the use of multiple implementation inheritance; however, it is required when using COM and some ATL/WTL classes. You may use multiple implementation inheritance to implement COM or ATL/WTL classes and interfaces.
- Although you should not use exceptions in your own code,
they are used extensively in the ATL and some STLs,
including the one that comes with Visual C++. When using
the ATL, you should define
_ATL_NO_EXCEPTIONS
to disable exceptions. You should investigate whether you can also disable exceptions in your STL, but if not, it is OK to turn on exceptions in the compiler. (Note that this is only to get the STL to compile. You should still not write exception handling code yourself.) - The usual way of working with precompiled headers is to
include a header file at the top of each source file,
typically with a name like
StdAfx.h
orprecompile.h
. To make your code easier to share with other projects, avoid including this file explicitly (except inprecompile.cc
), and use the/FI
compiler option to include the file automatically. - Resource headers, which are usually named
resource.h
and contain only macros, do not need to conform to these style guidelines.
Parting Words
Use common sense and BE CONSISTENT.
If you are editing code, take a few minutes to look at the
code around you and determine its style. If they use spaces
around their if
clauses, you should, too. If
their comments have little boxes of stars around them, make
your comments have little boxes of stars around them too.
The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you are saying, rather than on how you are saying it. We present global style rules here so people know the vocabulary. But local style is also important. If code you add to a file looks drastically different from the existing code around it, the discontinuity throws readers out of their rhythm when they go to read it. Try to avoid this.
OK, enough writing about writing code; the code itself is much more interesting. Have fun!
Benjy Weinberger
Craig Silverstein
Gregory Eitzmann
Mark Mentovai
Tashana Landray
文章来源于领测软件测试网 https://www.ltesting.net/