Source code style specifications of Microsoft Office-absolutely confidential documents !!!

Source: Internet
Author: User

Office source code style guide
Dave Parker, 6/30/95

Abstract
This document outlines a general style guide for C and C ++ source code in office development. the main purpose here is to list features of C ++ which we will use and which we will avoid, along with the basic rationale for doing so. there are also standards for basic coding issues for the sake of consistency within the code and robust constructs. this is not a complete list of C/C ++ language features with commentary. rather, it mentions only the issues we consider important. knowledge of C ++ is assumed.
Contents
1. General goals 3
2. Classes 3
2.1 class vs. struct 4
2.2 Public, private, and protected members 4
2.3 Data members 4
2.4 virtual functions 5
2.5 constructors 5
2.6 Destructors 6
2.7 New and delete 7
2.8 Operators 7
2.9 inheritance 8
2.9.1 inheritance of interface vs. Implementation 8
2.9.2 inheritance vs. Containment 10
2.9.3 multiple inheritance 11
3. Other C ++ features 11
3.1 constants and enumerations 12
3.2 references 12
3.3 const parameters and functions 13
3.4 default arguments 13
3.5 function overloading 14
3.6 Operator Overloading 14
4. Common C/C ++ issues 14
4.1 # ifdefs 14
4.2 global variables 15
4.3 macros and inline functions 16
4.4 optimization 16
4.5 Warnings 17
4.6 private data and functions 17
4.7 typedefs 17
4.8 basic data types 17
4.9 pointers 18
4.10 switch Statements 19
4.11 asserts 19
4.12 errors and exceptions 19
5. formatting conventions 20
5.1 naming conventions 20
5.2 function prototypes 21
5.3 variable declarations 22
5.4 class declarations 22
5.5 Comments 23
5.5.1 file headers and section separators 23
5.5.2 function headers 24
5.5.3 in-code comments 25
5.5.4 attention markers 25
5.6 Misc. formatting conventions 26
5.7 source file organization 27
5.7.1 public interface files 27
5.7.2 private interface files 28
5.7.3 implementation files 28
5.7.4 base filenames 29
6. interfaces to DLLs 29
6.1 c Functions and global variables 29
6.2 Common C/C ++ public header files 29
6.3 lightweight COM objects and isimpleunknown 30
7. Appendix A: Basic Hungarian reference 33
7.1 making Hungarian names 33
7.2 standard base tags 33
7.3 standard prefixes 34
7.4 Standard qualifiers 35

1. General goals
C ++ is a complex language that provides always to do things, and going into hole hog "on all of its features can lead to confusion, inefficiency, or maintenance problems. all office developers need to become experts on the features we will use, and avoid the others in order to form solid conventions within the group that we are all comfortable. our use of C ++ features will be fairly conservative. we recommend much rather err on the side of just dealing with C, which we recommend e all used to, then screwing up our app with a new concept that not all of us are used.
Underlying the choice of all of the style decisions are a few basic goals, as listed below. when in doubt about a participant issue, always think about the spirit of these goals. sometimes these goals will conflict, of course, and in these cases we try to either prioritize the tradeoffs or use experience (either our own or from other groups that have used C + extensively ).
1. Simplicity. When in doubt, keep it simple. Bugs are related mostly to complexity, not code.
2. clarity. The Code shoshould do what it looks like it should doing. Other people need to be able to understand your code.
3. efficiency. speed and size are important. using C ++ does not imply big and slow. there are plenty of perfectly reasonable ways to make things as fast or faster than the normal c way. speed and size often trade off, and most people probably err on the side of choosing speed too often. remember that 20% of the Code is responsible for 80% of the time. in most cases, we recommend e more concerned about fitting comfortably in less RAM.
4. appropriateness. use the language construct that is appropriate for the specified action or operation you are trying to do. do not abuse the language. don't need to use a construct just because it happens to work. definitely don't need to use a strange construct to amaze and confuse your friends to try to show how smart you are.
5. natural transition from C to C ++. we all used to be C programmers. others that look at our code are still C programmers (e.g. word and Excel ). when possible, avoid C ++ constructs where a C programmer extends instinct causes a wrong assumption.
6. catch errors early. having the compiler catch an error is ideal. having debug code (e.g. asserts) Catch it is the next best thing, etc. declare things in such as way as to give the compiler the best chance at catching errors.
7. fast builds. total generality and modularity can cause lots of Inter-dependencies between files, which can have a dramatic impact on build Times. this is a constant time sink for everyone. it is often worth rearranging things a little to make incremental builds faster.
8. consistency. the whole point of having a style guide is that programmers are never totally autonomous, even when the Group has strong code ownership. other people need to read and understand your code. everyone has to give a little to have a consistent style guide, but everyone gains it back when they read or debug other people coding.
2. Classes
C ++ classes are a nice way to encapsulate code and data into a single unit, which provides a good paradigm for object-oriented implementations as well other features such as flexible access control, convenient and type-safe polymorphism, and the possibility of code reuse via inheritance.
At the most general, classes are an extension to the built-in typing of C which allows you to define your own types along with the operations on that type. taken to the extreme, every piece of data in a program cocould be an instance of a class. however, we will not go nearly this far in office. we will use classes when there is a good reason to, such as the concept being implemented is inherently object-oriented or polymorphism is required. it has been the experience of our people that programs that use classes for everything evolve into systems that are complex and inefficient. although this may not be the fault of any participant class, complex class hierarchies can lead to needless complexity, and overly abstracted concepts can easily lead to inefficiency.
In general, we will avoid allocating classes on the stack and passing classes by value, because this is where the use of constructors and Destructors gets you into the most trouble. most classes shoshould be allocated via new, freed by delete, and passed by pointer. in addition, we will never declare a global variable which is an instance of a class that has a constructor, because this causes a bunch of C Runtime stuff to get linked in and stuff to happen at boot time to construct the thing, which is a big performance hit. using only heap-allocated classes implies we recommend l probably use classes only for relatively complex objects that you wowould normally have in the heap anyway, not simple things like basic data types. beyond this, it is a judgment call when to use a class. use one if there is a good reason, but not if a more straightforward solution is just as good.
Summary:
Use classes to encapsulate the implementation of an object-oriented concept.
Use classes to implement polymorphism.
Avoid allocating class instances on the stack and passing them by value. use new and delete, and pass them by Pointer. This implies not using classes for simple data types.
Never declare a global instance of a class that has a constructor.
Not everything is as class. Use them only when you gain something.
2.1 class vs. struct
In C ++, a struct can also have member functions and operators and everything else that a class can have. in fact, the only difference between a class and a struct is that all Members default to public access in a struct but private access in a class. however, we will not use this as the deciding point between using a class. A struct. to match the normal intuition, we will use a class if and only if there are member functions included.
Summary:
Use a class instead of a struct if and only if there are member functions.
2.2 Public, private, and protected members
As stated above, structs default to public access and classes default to private access. however, we will depend on the default only in the case of structs (where we leave all the data implicitly public ). for a class, we will declare all members (both data and code) explicitly as public, protected, or private, and group them into sections in that order. for example:
Class foo
{
Public:
Foo ();
~ Foo ();
Void Hey (int I );
Void ack ();
Protected:
Int m_ivalue;
PRIVATE:
Int m_istuff;
Void localhelpersub ();
};

Summary:
Declare all class members explicitly as public, protected, or private, in groups in that order.
2.3 Data members
Data members shocould use the naming convention m_name where name is a normal Hungarian local variable name. this makes member function implementations easier to read (no confusion about member. local data), and allows the use of the same Hungarian name for, e.g ., parameters and members. see the example below.
Data members shocould normally not be declared public because this usually defeats the purpose of the class into action. to efficiently export a data member, declare inline get and set member functions. this will get optimized into the same code as a public data member. for example:
Class counter
{
Public:
Int citems () const {return m_citems ;}
Void setcitems (INT citems) {m_citems = citems ;}
PRIVATE:
Int m_citems;
};

Summary:
Data members use the naming convention m_name.
Do not declare public data members. Use inline accessor functions for performance.
2.4 virtual functions
Virtual functions are used to allow Derived classes to override a method in a base class by providing their own implementation in a way that always causes the most-derived version to be called whenever a method is called through an object pointer, even if that pointer is declared as a pointer to the base class. this is usually done to implement polymorphism, and that when we use l use them. for example, all COM interface methods are virtual because you are always going for Polymorphism via a standard interface.
Unlike simple member functions, virtual functions incur some overhead due to need to call through the vtable. if a class contains at least one virtual function then the data size of each instantiated object will be 4 bytes larger than the combined size of the declared data in order to hold the vtable pointer. after the first virtual function, each additional one only adds another entry to the class vtable, Which is static and per-class (nothing per object ), so the main concern here is whether a class has any virtual functions at all. in addition to the memory overhead, there is the overhead to indirect a pointer twice before calling the function. this is fairly fast and compact in 32-bit code, but affects speed and size nevertheless. perhaps the worst part is that virtual functions cannot be inlined, so there will always be a function call, even when the work is trivial.
Because they have overhead, you shoshould not use virtual functions in a class unless you need. however, make sure you do use them when it makes sense. in particle, if you have a base class which requires a destructor, then the Destructor shoshould definitely be virtual to allow Derived classes to destruct any added members properly. if the Destructor were not virtual, then in a context where polymorphism is being used (so the object pointer is declared as a pointer to the base class ), the base class destructor will always get called, even for an object of A derived class that added data members and declared its own destructor in an attempt to free them. the derived class implements destructor will only get called if the base class destructor is declared virtual. this scenario applies to your other kinds of methods that you will add to your classes. in fact, most of the methods in a base class might be this way if polymorphism is intended. this issues is discussed in more detail in the inheritance section below.
Note that although virtual functions have a performance penalty over regular member functions, they are often the most efficient way to implement a concept such as polymorphism where the alternative wocould be large switch statements (not to mention the benefits of the object-oriented encapsulation ).
Summary:
Use virtual functions to implement polymorphism.
Virtual functions have overhead, so don't need to use them unless you really showould.
A destructor in a base class shoshould always be virtual if polymorphism is intended.
2.5 Constructors
Ah, constructors. every new C ++ programmer extends nightmare. this is one reason to try to minimize the use of constructors -- C programmers aren used to them and will get confused. another reason is the infamous performance overhead of calling a function (unless it than inline) and doing work at possibly unexpected and/or redundant times.
However, using constructors can eliminate the dangers of uninitialized data and can also made the code simpler to read (if you need e used to it ). judicious use of Destructors (see below) which match the constructors can also help prevent memory leaks and other resource management problems.
Fortunately, the issue is mainly one when classes are declared on the stack or passed by value, both of which we will avoid. most of our classes shocould be dynamic memory objects which will be passed around by pointer. in this case, the constructor is essential just a helper function for the functions that create these dynamic objects. using a constructor for this purpose is reasonable to ensure a clean and consistent initialization (if you make sure to initialize all data members ), but to prevent potential performance problems due to redundant initialization the constructor shocould not do anything expensive. simply assigning a constant or a parameter value to each data field is about right. very simple constructors can be made inline.
Most importantly, a constructor shold never be able to fail, because lacking a fancy Exception Handling mechanic, the caller has no way to handle this in some cases. any initialization that can fail (e.g. memory allocations) shocould be put in a separate initialization member function (called, e.g ., finit ). when this is the case, it is often useful to encapsulate the creation of an object in a function (a global function or a member of another class) that callnew and then finit for the object, and returns the result of Finit. for example:
Class foo
{
Public:
Foo (INT clines) {m_hwnd = NULL; m_clines = clines}
Virtual ~ Foo ();
Bool finit ();
Void dosomething ();
PRIVATE:
Hwnd m_hwnd;
Int m_clines;
};

Bool fcreatefoo (INT clines, foo ** ppfoo)
{
If (* ppfoo = new Foo (clines) = NULL)
Return false;
If (* ppfoo-> finit ())
Return true;
Delete * ppfoo;
* Ppfoo = NULL;
Return false;
}

Bool FOO: finit ()
{
M_hwnd = createwindow (...);
Return (m_hwnd! = NULL );
}

Summary:
Do not do expensive work in a constructor.
If you do make a constructor, make sure to initialize all data members.
Very simple constructors can be made inline
A constructor shoshould never fail. Do memory allocations and other potential failures in an finit method.
Consider making a creation function that encapsulates the new and finit operations.
2.6 Destructors
If a class has resources that need to be freed, then the Destructor is a convenient place to put this. the normal case for us will be that this is just the central place to free resources for an object that is freed via Delete (see below ). the trickier use of Destructors is for Stack-allocated classes, but we recommend e going to avoid that by not using classes on the stack.
A destructor shocould be careful to destroy an object properly regardless of how it was created or used. furthermore, if you choose to implement a method that frees any resources before the actual destruction, make sure to reset those fields (e.g. set pointers to null) so that a destructor will not try to free them twice. it is not necessary for the destructor to reset any fields, though, because the object cannot be used after it is destructed.
Like a constructor, A destructor can never fail. Also, as stated above, a destructor in a base class shoshould always be declared virtual to make polymorphism work.
The Destructor for the above example wocould be defined:
Foo :~ Foo ()
{
If (m_hwnd! = NULL)
Destroywindow (m_hwnd );
}

Summary:
Use a destructor to centralize the resource cleanup of a class which is freed via Delete.
If resources are freed before destruction, make sure the fields are reset (e.g. set pointers to null) so that a destructor will not try to free them again.
A destructor shoshould never fail.
A destructor in a base class shoshould always be declared virtual if polymorphism might be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.