Google對外發(fā)布C++編碼規范
Google的C++編碼規范對外發(fā)布,引起了業(yè)內開(kāi)發(fā)人員的廣泛關(guān)注。
其中,來(lái)自硅谷的柯化成認為,這是地球上最好的一份C++編程規范,沒(méi)有之一,建議廣大國內外IT人員研究使用。
盛大的資深開(kāi)發(fā)者趙劼表示,“非常同意。Google在這方面下足了功夫,讓所有人寫(xiě)出來(lái)的代碼都使用同樣的規范,就好像在工程師編程世界里普及普通話(huà)一樣。很多資深工程師剛加入的時(shí)候被迫學(xué)習編碼規范,開(kāi)始不習慣,后來(lái)發(fā)現收益非淺。所謂磨刀不誤砍柴功,創(chuàng )業(yè)公司更應該關(guān)注!
科泰的陳榕也認為,“希望Google索性再出版一個(gè)工具,類(lèi)似早先C語(yǔ)言的lint,按照該規范自動(dòng)排版。否則誰(shuí)記得住這么多條條框框?”
C++開(kāi)發(fā)者杜昶旭給大家的建議是,“建議所有開(kāi)發(fā)人員反復閱讀此編碼規范,直到可以背下來(lái)再開(kāi)始寫(xiě)代碼。當然,更好的做法是根據這個(gè)再補充出更具體的執行策略。學(xué)校里這些知識老師強調的太少,提前自學(xué)吧!
當然,也有不同的聲音,來(lái)自大連的sagasw就認為,“關(guān)于Google的C++編碼規范,不知為何突然又火起來(lái),這個(gè)規范在C++社區中應用的不多,關(guān)注度遠不如Gtest,另外這個(gè)規范對于Google是有幫助的,但不是最好的,也不是一定適合每個(gè)公司的,每個(gè)決定后面都有一個(gè)tradeoff,不知這些光會(huì )用規范,那意義不大!
“土豆”也表示,“Google的C++編碼規范沒(méi)有說(shuō)的這么好吧,至少我看Webkit的源碼中,明顯蘋(píng)果的代碼比Google的代碼漂亮些,也容易看些,受不了Google源碼中的N多下劃線(xiàn)!
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
// successfuly, 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
文章來(lái)源于領(lǐng)測軟件測試網(wǎng) http://kjueaiud.com/