Google對外發布C++編碼規范
Google的C++編碼規范對外發布,引起了業內開發人員的廣泛關注。
其中,來自硅谷的柯化成認為,這是地球上最好的一份C++編程規范,沒有之一,建議廣大國內外IT人員研究使用。
盛大的資深開發者趙劼表示,“非常同意。Google在這方面下足了功夫,讓所有人寫出來的代碼都使用同樣的規范,就好像在工程師編程世界里普及普通話一樣。很多資深工程師剛加入的時候被迫學習編碼規范,開始不習慣,后來發現收益非淺。所謂磨刀不誤砍柴功,創業公司更應該關注?!?/P>
科泰的陳榕也認為,“希望Google索性再出版一個工具,類似早先C語言的lint,按照該規范自動排版。否則誰記得住這么多條條框框?”
C++開發者杜昶旭給大家的建議是,“建議所有開發人員反復閱讀此編碼規范,直到可以背下來再開始寫代碼。當然,更好的做法是根據這個再補充出更具體的執行策略。學校里這些知識老師強調的太少,提前自學吧?!?/P>
當然,也有不同的聲音,來自大連的sagasw就認為,“關于Google的C++編碼規范,不知為何突然又火起來,這個規范在C++社區中應用的不多,關注度遠不如Gtest,另外這個規范對于Google是有幫助的,但不是最好的,也不是一定適合每個公司的,每個決定后面都有一個tradeoff,不知這些光會用規范,那意義不大?!?/P>
“土豆”也表示,“Google的C++編碼規范沒有說的這么好吧,至少我看Webkit的源碼中,明顯蘋果的代碼比Google的代碼漂亮些,也容易看些,受不了Google源碼中的N多下劃線?!?/P>
Example
They say an example is worth a thousand words so let's start off with an example that should give you a feel for the style, spacing, naming, etc.
An example header file, demonstrating the correct commenting and spacing for an @interface declaration
// GTMFoo.h
// FooProject
//
// Created by Greg Miller on 6/13/08.
// Copyright 2008 Google, Inc. All rights reserved.
//
#import
// A sample class demonstrating good Objective-C style. All interfaces,
// categories, and protocols (read: all top-level declarations in a header)
// MUST be commented. Comments must also be adjacent to the object they're
// documenting.
//
// (no blank line between this comment and the interface)
@interface GTMFoo : NSObject {
@private
NSString *foo_;
NSString *bar_;
}
// Returns an autoreleased instance of GMFoo. See -initWithString: for details
// about the argument.
+ (id)fooWithString:(NSString *)string;
// Designated initializer. |string| will be copied and assigned to |foo_|.
- (id)initWithString:(NSString *)string;
// Gets and sets the string for |foo_|.
- (NSString *)foo;
- (void)setFoo:(NSString *)newFoo;
// Does some work on |blah| and returns YES if the work was completed
// suclearcase/" target="_blank" >ccessfuly, and NO otherwise.
- (BOOL)doWorkWithString:(NSString *)blah;
@end
An example source file, demonstating the correct commenting and spacing for the @implementation of an interface. It also includes the reference implementations for important methods like getters and setters, init, and dealloc.
//
// GTMFoo.m
// FooProject
//
// Created by Greg Miller on 6/13/08.
// Copyright 2008 Google, Inc. All rights reserved.
//
#import "GTMFoo.h"
@implementation GTMFoo
+ (id)fooWithString:(NSString *)string {
return [[[self alloc] initWithString:string] autorelease];
}
// Must always override super's designated initializer.
- (id)init {
return [self initWithString:nil];
}
- (id)initWithString:(NSString *)string {
if ((self = [super init])) {
foo_ = [string copy];
bar_ = [[NSString alloc] initWithFormat:@"hi %d", 3];
}
return self;
}
- (void)dealloc {
[foo_ release];
[bar_ release];
[super dealloc];
}
- (NSString *)foo {
return foo_;
}
- (void)setFoo:(NSString *)newFoo {
[foo_ autorelease];
foo_ = [newFoo copy];
}
- (BOOL)doWorkWithString:(NSString *)blah {
// ...
return NO;
}
@end
原文轉自:http://www.anti-gravitydesign.com