📄 linux tutorial c++ coding style, standards and practices.mht
字号:
first viewing. Complex algorithms should be broken up into=20
understandable units and may be too long at 50 lines of code. =
Thus the=20
length of a function requires common sense more than a rule =
which sets a=20
hard limit.=20
<LI>Use of case:=20
<UL>
<LI>Use capital leters with an underscore used to separate =
words for=20
macro constants and enumerations.=20
<LI>Use a single capital letter to denote the start of a new =
word in=20
the variable, class or function name. <BR>Example:=20
<TT>thisIsAnExample</TT>=20
<LI>Function names should begin with a lower case letter.=20
<LI>Class definitions begin with a capital letter. The "C" =
prefix is=20
"Hungarian" notation as noted below. The instantiation should =
begin=20
with a lower case letter. <BR>Example: <TT>CAbcXyz abcXyz</TT> =
</LI></UL>
<LI>Use complement names for complement functions: get/set, =
add/remove,=20
create/destroy, start/stop, insert/delete, increment/decrement,=20
begin/end, first/last, up/down, min/max, next/previous, =
open/close,=20
show/hide, suspend/resume, etc.=20
<LI>Avoid the use of "namespace" in the ".h" include file. If =
the file=20
is included in source code the programmer may no longer use =
variables=20
defined in the namespace and may experience variable name clash. =
<LI>Don't use equality (=3D=3D) comparisons between floating =
point numbers=20
and 0.0. </LI></UL>
<P><A name=3DCAMELCASE></A>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Camel-case:</BIG></B></TD></TR></TBODY></TABLE>
<P>Camel-case is the practice of changing the letter to upper case =
to=20
denote the start of a new word. This allows the name to be shorter =
because=20
no separator character (i.e. "_" used between words.) The =
Camel-case=20
practice uses a "_" separator between upper case constants used in =
words.=20
<P>Examples:=20
<UL>
<LI>VariableForTheBlueTeam=20
<LI>variableForTheBlueTeamInstantiation=20
<LI>SPAM_Eliminator </LI></UL>
<P><A name=3DSTYLE></A>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C++ Style:</BIG></B></TD></TR></TBODY></TABLE>
<P>"Hungarian" prefixes: Some of these are discussed in the book =
"Code=20
Complete" and others I have seen in corporate coding standards. =
While=20
these are prefixes (beginning of name) I have also seen some used =
as=20
suffixes (end of name).=20
<DL>
<DD>
<TABLE border=3D1>
<TBODY>
<TR>
<TD vAlign=3Dtop>m_<BR>my_</TD>
<TD vAlign=3Dtop>Variable is a member of class. Some use the =
prefix=20
"<TT>my</TT>" for class member variables and =
"<TT>our</TT>" for=20
class member static variables. (Also "m" and "ms" for =
member and=20
member-static) I have also seen the use of "_" as both a =
prefix=20
and as a suffix to denote a class member variable. If =
using mixed=20
languages remember that many FORTRAN compilers use a "_" =
suffix.=20
Due to C++ name mangling there will probably be no =
conflict when=20
using this notation. It is a common practice but =
theoretically=20
could pose a problem with mixed languages. See <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.h=
tml">Mixing=20
FORTRAN and C</A> YoLinux tutorial.</TD></TR>
<TR>
<TD vAlign=3Dtop>_<BR>(underscore)</TD>
<TD vAlign=3Dtop>
<UL>
<LI>Variable not intended to be exposed externally to =
the user.=20
Internal workings only. Most often used by compiler and =
library=20
authors.=20
<LI>Sometimes used (not preferred) as a suffix to =
repressent a=20
class member variable.=20
<LI>Sometimes used as variable prefix for function =
arguments.=20
When used with function arguments, it can represent=20
in/out/inout:
<UL>
<LI>_variable: Function input variable
<LI>variable_: Function output variable
<LI>_variable_: Input/output by =
function</LI></UL></LI></UL>
<P>Note: Dangerous when mixing C with FORTRAN as FUNCTION=20
subroutines as suffixed with "_" in the object code. See =
<TT><A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Dnm">nm</=
A></TT>=20
to view object symbols. See the <A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Dg77">g77=
/f77</A>=20
compiler flag "<TT>-fno-underscore</TT>".</P></TD></TR>
<TR>
<TD vAlign=3Dtop>__</TD>
<TD>Marks an use as extension to ANSI C++. Often not =
compiler=20
independant. Usually reserved for use by compiler writers =
so it is=20
best to <B>avoid the double underscore</B>.=20
<P>Note: Be careful when mixing with FORTRAN as "__" is =
often=20
generated in FORTRAN object symbol names. See the f77 =
compiler=20
flag "<TT>-fno-second-underscore</TT>". Also used in =
Microsoft=20
"Managed C++" directives.</P></TD></TR>
<TR>
<TD>a</TD>
<TD>array</TD></TR>
<TR>
<TD>c </TD>
<TD>count / number of</TD></TR>
<TR>
<TD>cb </TD>
<TD>count bytes / number of bytes</TD></TR>
<TR>
<TD>d </TD>
<TD>Data type double</TD></TR>
<TR>
<TD>dw </TD>
<TD>Data type double word</TD></TR>
<TR>
<TD>e </TD>
<TD>element of an array</TD></TR>
<TR>
<TD vAlign=3Dtop>b<BR>f <BR>is<BR>has<BR>should</TD>
<TD vAlign=3Dtop>boolean<BR>(flag)<BR>Example: =
isFound</TD></TR>
<TR>
<TD vAlign=3Dtop>g_<BR><BR>g gbl<BR>Gbl</TD>
<TD vAlign=3Dtop>global variable. Also used is the "::" =
operator.=20
i.e. <TT>::<I>variable</I></TT></TD></TR>
<TR>
<TD>s<BR>s_ </TD>
<TD vAlign=3Dtop>Static variable</TD></TR>
<TR>
<TD>h </TD>
<TD>handle</TD></TR>
<TR>
<TD>i </TD>
<TD>index to an array</TD></TR>
<TR>
<TD>l </TD>
<TD>long data type. i.e. "long int"</TD></TR>
<TR>
<TD>p <BR>ptr</TD>
<TD vAlign=3Dtop>pointer. Sometimes "Ptr" used as =
suffix.</TD></TR>
<TR>
<TD>lp </TD>
<TD>pointer to long data type</TD></TR>
<TR>
<TD>ch </TD>
<TD>C character </TD></TR>
<TR>
<TD>sz </TD>
<TD>Zero terminated string. char[] </TD></TR>
<TR>
<TD>str </TD>
<TD>C++ GNU template data type "string"</TD></TR>
<TR>
<TD>u </TD>
<TD>unsigned </TD></TR>
<TR>
<TD>ev </TD>
<TD>event </TD></TR>
<TR>
<TD>min </TD>
<TD>Lowest value</TD></TR>
<TR>
<TD>first </TD>
<TD>First element</TD></TR>
<TR>
<TD>lim </TD>
<TD>array limit</TD></TR>
<TR>
<TD>cmd </TD>
<TD>Command</TD></TR>
<TR>
<TD>cmp </TD>
<TD>Compare</TD></TR>
<TR>
<TD>init </TD>
<TD>Initialize</TD></TR>
<TR>
<TD>intr </TD>
<TD>Interrupt</TD></TR>
<TR>
<TD>msg </TD>
<TD>Message</TD></TR>
<TR>
<TD>rx </TD>
<TD>Recieve</TD></TR>
<TR>
<TD>tx </TD>
<TD>Transmit</TD></TR>
<TR>
<TD vAlign=3Dtop>C </TD>
<TD vAlign=3Dtop>Prefix for a Class name. Notation used by =
Microsoft=20
MFC. <BR>Example: <TT>CAbcXyz abcXyz</TT><BR>Note: The=20
instatiation begins with a lower case letter while the =
class=20
definition begins with a capital letter.</TD></TR>
<TR>
<TD vAlign=3Dtop>_t <BR>_Type</TD>
<TD vAlign=3Dtop>Type definition (typedef - <B>suffix</B>). =
Prefix=20
with capital letter.</TD></TR>
<TR>
<TD vAlign=3Dtop>_Enum</TD>
<TD vAlign=3Dtop>Enumeration type definition =
suffix.</TD></TR>
<TR>
<TD vAlign=3Dtop>_ds</TD>
<TD vAlign=3Dtop>Data structure =
suffix.</TD></TR></TBODY></TABLE></DD></DL>
<P>Hungarian Examples: <BR>
<UL>
<LI>ppach<I>VarName</I>: pointer to a pointer of an array of =
characters.=20
<LI>m_ppach<I>VarName</I>: class member variable which is a =
pointer to a=20
pointer of an array of characters. </LI></UL>The use of =
"Hungarian" should=20
make the code more readable and NOT more complicated so use it =
wisely and=20
consistently. <A name=3DHEADERCOMMENTS></A>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Introductory File Header=20
Comments:</BIG></B></TD></TR></TBODY></TABLE>
<P>This is usually set as a project or corporate standard and =
include=20
copyrights, statements of ownership, trade secret statements, =
author's=20
name,.... <BR>For example:=20
<P>
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><SMALL><PRE>// =
-------------------------------------------------------------------------=
---
/**
File: ProjectXXX.<I>suffix</I>
=20
System: Super Product X
Component Name: Project X, Module B=20
Status: Version 1.0 Release 1
Language: C++
=20
License: GNU Public License=20
or other... i.e.=20
Licensed Material - Property of Mega Corp
=20
(c) Copyright Mega Corp 2003
=20
Address:
Mega Corp, Super Product X Division
134950 Sepulveda Blvd.
Los Angeles CA 90211
=20
Author: John Doe
E-Mail: John.Doe@MegaCorp.com
=20
Description: Header file for Project X
This file contains the defined types for Project X=20
This is sometimes called the "Abstract" and may be=20
followed by a section called "Notes".
Limitations: bla bla bla
=20
Function: 1) bla bla bla
2) bla bla bla
3) bla bla bla
=20
Database tables used:=20
=20
Thread Safe: No
=20
Extendable: No
=20
Platform Dependencies: None (i.e.: Linux/Intel, IRIX/Mips, =
Solaris/SPARC)
Compiler Options: -lm
=20
Change History: (Sometimes called "Revisions")
Date Author Description
*/ =20
// =
-------------------------------------------------------------------------=
---
...
..
.
..
...
// =
-------------------------------------------------------------------------=
---
/**
Routine: <I>NameOfRoutine()</I>
Inputs:
Arguments:
Externals:
Others:
=20
Outputs:
Arguments:
Externals:
Returns:
Errors:
=20
Routines Called:
*/ =20
// =
-------------------------------------------------------------------------=
---
</PRE></SMALL></TD></TR></TBODY></TABLE></DD></DL>Note: Comments should=20
NOT contain code as it may get out of sync with the code itself.=20
<P><A name=3DAUTODOCUMENTATION></A>
<P>
<HR SIZE=3D5>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -