您现在的位置是:主页 > news > 济南网站制作公司报价/网络宣传

济南网站制作公司报价/网络宣传

admin2025/6/6 2:06:03news

简介济南网站制作公司报价,网络宣传,浙江网站建设公司,莱芜百度推广电话注:这是我第一次翻译文章,加上我英语不是很好,这篇翻译的不是很好。感谢我的同学David帮我修正了很多语句不通的地方。原文: http://www.mozilla.org/hacking/mozilla-style-guide.htmlThis document attempts to explain the bas…

济南网站制作公司报价,网络宣传,浙江网站建设公司,莱芜百度推广电话注:这是我第一次翻译文章,加上我英语不是很好,这篇翻译的不是很好。感谢我的同学David帮我修正了很多语句不通的地方。原文: http://www.mozilla.org/hacking/mozilla-style-guide.htmlThis document attempts to explain the bas…
注:这是我第一次翻译文章,加上我英语不是很好,这篇翻译的不是很好。感谢我的同学David帮我修正了很多语句不通的地方。

原文: http://www.mozilla.org/hacking/mozilla-style-guide.html


This document attempts to explain the basic styles and patterns that are used in the Mozilla codebase. New code should try to conform to these standards so that it is as easy to maintain as existing code. Of course every rule has an exception, but it's important to know the rules nonetheless!

本文档旨在说明Mozilla代码库的基本风格和模式。新代码应遵从本文档的标准,使得新代码和旧代码一样方便维护。当然,任何规则都有例外,但无论如何,了解这些规则还是很重要的。

 

This is particularly directed at people new to the Mozilla codebase, who are in the process of getting their code reviewed. Before getting a review, please read over this document and make sure your code conforms to the recommendations here.

本文档特别针对那些正在为Mozilla代码库做代码审查的的新手。在做代码审查之前,务必先阅读本文档,保证你的代码符合本文的建议。

 

General C/C++ Practices

C/C++的一般原则


  • Are the changes 64bit clean?

  • 是否所有的更改都在64位机上无warningerror

 

COM and pointers

COM和指针

  • Use nsCOMPtr<>

    If you don't know how to use it, start looking in the code for examples. The general rule is that the very act of typing NS_RELEASE should be a signal to you to question your code: "Should I be using nsCOMPtr here?". Generally the only valid use of NS_RELEASE are when you are storing refcounted pointers in a long-lived datastructure.

  • 使用nsCOMPtr<>

     如果你不知道如何使用nsCOMPtr<>, 可以先在代码里查找例子。  一般规则,输入NS_RELEASE会提醒你思考你写的代  码:"这个地方我需要要用nsCOMPtr吗?"。 通常只有在你需要在生命周期的数据结构里存储引用计数指针,才需要  使用NS_RELEASE.


IDL

IDL

  • Use leading-lowercase, or "interCaps" When defining a method or attribute in IDL, the first letter should be lowercase, and each following word should be capitalized. For example: long updateStatusBar();

  • 开头字母小写, 或"interCaps" : 在IDL定义一个方法或属性,第一个字母应该小写,紧跟它后面的每个单词的头一个字母应该大写。例如:

    1. long updateStatusBar()

  • Use attributes wherever possible

     Whenever you are retrieving or setting a single value without any context, you should use attributes. Don't use two methods when you could use one attribute.Using attributes logically connects the getting and setting of a value, and makes scripted code look cleaner.

  • 尽可能地使用属性

    当你获取或设置一个不需要上下文的单一值,应使用属性。在你可以使用一个属性时,就不要使用两个方法。理论上,应使用属性来获取或设置值, 会使脚本代码看起来更简洁。


    This example has too many methods:

    这是一个使用两个方法的例子:

  1. interface nsIFoo : nsISupports {
  2.     long getLength();
  3.     void setLength(in long length);
  4.     long getColor();
  5. };
   The code below will generate the exact same C++ signaturebut is more script-friendly.
   下面的代码会生成与c++完全相同的签名, 但它对脚本更友好

  1. interface nsIFoo : nsISupports {
  2.     attribute long length;
  3.     readonly attribute long color;
  4. };
  • Use java-style constants

  • 使用java风格的常量

    When defining scriptable constants in IDL, the name should be all uppercase, with underscores between words:

    当你在IDL定义脚本常量时, 常量名应全部大写,且单词之间有下划线:

  1. const long ERROR_UNDEFINED_VARIABLE = 1;

Error handling

错误处理

  • Check for errors early and often

  • 早、经常检查错误

    Every time you make a call into an XPCOM function, you should check for an error condition. You need to do this even if you know that call will never fail. Why?

    每次你调用XPCOM函数时,应检该函数发生错误的条件。就算你知道了这个函数从不失败,也要这样做。为什么?

  • Use the nice macros

  • 使用友好的宏

In most cases, your knee-jerk reaction should be to return from the current function when an error condition occurs. Don't do this:

在大多数情况下,当一个错误情况发生了,你第一反应是应从当前函数返回。不要像这样写

  1. rv = foo->Call1();
  2. if (NS_SUCCEEDED(rv)) {
  3.     rv = foo->Call2();
  4.         if (NS_SUCCEEDED(rv)) {
  5.             rv = foo->Call3();
  6.         }
  7.     }
  8. }
  9. return rv;

 

Instead, do this:

应这样

  1. rv = foo->Call1();
  2. NS_ENSURE_SUCCESS(rv, rv);
  3. rv = foo->Call2();
  4. NS_ENSURE_SUCCESS(rv, rv);
  5. rv = foo->Call3();
  6. NS_ENSURE_SUCCESS(rv, rv);

   

Why? Because error handling should not obfuscate the logic of the code. The author's intent in the first example was to make 3 calls in successionbut wrapping the calls in nested if() statements obscured the most likely behavior of the code.

为什么?因为错误处理不应把代码的逻辑弄得晦涩难懂。在第一个例子中,作者的意图是成功时分别调用3个函数,但把函数的调用封装在嵌套的if()语句里,使代码看起来很晦涩难懂。

 

 

Consider a more complicated example that actually hides a bug:

考虑一个比较复杂的例子,其实它隐藏一个臭虫(bug)

  1. PRBool val;
  2. rv = foo->GetBooleanValue(&val);
  3. if (NS_SUCCEEDED(rv) && val)
  4.   foo->Call1();
  5. else foo->Call2();

 

The intent of the author may have been that foo->Call2() would only happen when val had a false value. In fact, foo->Call2() will also be called when foo->GetBooleanValue(&val) fails. This may or may not have been the author's intent, and it is not clear from this code. Here is an updated version:

作者的意图是,当val的值为false时,foo->Call2()才会调用。实际上,当foo->GetBooleanValue(&val)调用失败时, foo2->Call2() 同样也会被调用。这可能是,也有可能不是作者的意图,这段代码表述不清。这里是一个升级版本:

  1. PRBool val;
  2. rv = foo->GetBooleanValue(&val);
  3. if (NS_FAILED(rv)) return rv;
  4. if (val)
  5.    foo->Call1();
  6. else
  7.    foo->Call2();

 

In this example, the author's intent is clear, and an error condition avoids both calls to foo->Call1() and foo->Call2();

Possible exceptions: Sometimes it is not fatal if a call fails. For instance, if you are notifying a series of observers that an event has fired, it might be inconsequential that one of these notifications failed:

在这个例子了,作者的意图很清晰,错误发生情况下避免同时调用foo->Call1() foo->Call2(); 可能的例外:有时候函数调用失败不是致命的。比如,当一个事件被触发要通知一系列观察者,其中一个事件通知失败是无关紧要的。

  1. for (i=0; i<length; i++) {
  2.         // we don't care if any individual observer fails
  3.         observers[i]->Observe(foo, bar, baz);
  4.     }

 

Another possibility is that you are not sure if a component exists or is installed, and you wish to continue normally if the component is not found.

另一个可能的是你无法肯定组件是否存在或已安装,而你希望就算找不到该组件,也正常运行下去。

  1. nsCOMPtr<nsIMyService> service = do_CreateInstance(NS_MYSERVICE_CID, &rv);
  2. // if the service is installed, then we'll use it
  3. if (NS_SUCCEEDED(rv)) {
  4.     // non-fatal if this fails too, ignore this error
  5.     service->DoSomething();
  6.     // this is important, handle this error!
  7.     rv = service->DoSomethingImportant();
  8.     if (NS_FAILED(rv)) return rv;
  9. }
  10.     
  11. // continue normally whether or not the service exists

Strings

字符串

 

When declaring a local, short-lived nsString class, always use nsAutoString or nsCAutoString - these versions pre-allocate a 64-byte buffer on the stack, and avoid fragmenting the heap. Don't do this:

声明一个局部、生命周期短nsString类时,总是使用nsAutoStringnsCAutoString------这些版本的字符串类会在堆栈上预分配64字节的内存,避免内存碎片。不要这样

  1. nsresult foo() {
  2.   nsCString bar;
  3.   ..
  4. }

 

instead:

取而代之

  1. nsresult foo() {
  2.   nsCAutoString bar;
  3.   ..
  4. }

 

It is an easy trap to return an allocated string from an internal helper function, and then use that function inline in your code without freeing the value. Consider this code:

在你的代码使用一个会返回分配内存的字符串的内部辅助函数,但不释放内存,这是一个很明显的自陷。考虑下面的代码:

  1. static char *GetStringValue() {
  2.     ..
  3.     return resultString.ToNewCString();
  4. }
  5.     ..
  6.     WarnUser(GetStringValue());

 

In the above example, WarnUser will get the string allocated from resultString.ToNewCString() and throw away the pointer. The resulting value is never freed. Instead, either use the string classes to make sure your string is automatically freed when it goes out of scope, or make sure that your string is freed.

在上面的例子,把已分配内存的resultString赋值给WamUserToNewCString()会扔掉指针。但返回值从未被释放。因此要么使用自动形式的字符串类,确保当你的字符串超出作用域时会被释放要么确保你的字符串已被释放。

 

 

Automatic cleanup:

自动清除:

  1. static void GetStringValue(nsAWritableCString& aResult) {
  2.     ..
  3.     aResult.Assign("resulting string");
  4. }
  5.     ..
  6.     nsCAutoString warning;
  7.     GetStringValue(warning);
  8.     WarnUser(warning.get());

 

Free the string manually:

手动释放字符串:

 

  1. static char *GetStringValue() {
  2.     ..
  3.     return resultString.ToNewCString();
  4. }
  5.     ..
  6.     char *warning = GetStringValue();
  7.     WarnUser(warning);
  8.     nsMemory::Free(warning);

 

 

Incorrect:

不正确的:

  1. nsAutoString warning; warning.AssignWithConversion("danger will robinson!");
  2. ..
  3. foo->SetUnicodeValue(warning.get());

Correct:

正确的:

  1. NS_NAMED_LITERAL_STRING(warning,"danger will robinson!");
  2. ..
  3. // if you'll be using the 'warning' string, you can still use it as before:
  4. foo->SetUnicodeValue(warning.get());
  5. // alternatively, use the wide string directly:
  6. foo->SetUnicodeValue(NS_LITERAL_STRING("danger will robinson!").get());

 

Naming and Formatting code

变量命名和代码风格

Note: the following is not all set in stone, this is interim to give people a chance to look

注意:下面规则不全是硬性规定,只是临时让开发者有机会了解

  • Control Structures:

  • 控制结构:

  1. if (...) {
  2. else if (...) {
  3. else {
  4. }
  5. while (...) {
  6. }
  7. do {
  8. while (...);
  9. for (...; ...; ...) {
  10. }
  11. switch (...)
  12. {
  13.   case 1:
  14.     {
  15.       // When you need to declare a variable in a switch, put the block in braces
  16.       int var;
  17.     } break;
  18.   case 2:
  19.     ...
  20.     break;
  21.   default:
  22.     break;
  23. }
  • Classes:

  • 类:

  1. class nsMyClass : public X,
  2.                        public Y
  3. {
  4. public:
  5.   nsMyClass() : mVar(0) { ... };
  6.   
  7. private:
  8.   int mVar;
  9. };
  • Methods:

  • 方法:

  1. int
  2. nsMyClass::Method(...)
  3. {
  4.   ...
  5. }
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

 

Original document by Alec Flett.

Alec Flett撰写原稿。

 

 

Thanks to:

感谢:

for additional comments.

进一步评论。

 

 

Additions by Akkana Peck based on discussions on IRC: thanks to: bbaetz, bz, jfrancis, jkeiser, mjudge, and sdagley for comments, and to John Keiser and JST's Reviewer Simulacrumand Brendan and Mitchell's super-review document.

Akkana Peck 基于在IRC上的讨论添加:感谢:bbaetz, bz, jfrancis, jkeiser, mjudge, 和 sdagley的评论,以及提到的John Keiser and JST's Reviewer Simulacrum 和 Brendan and Mitchell's super-review document