Chromium OS Autotest 编码风格

Chromium OS Autotest 编码风格

语言

  • 尽可能使用python
  • 在host命令的调用中会使用少量的shell命令
  • 一些测试会在测试的依赖项中使用C/C++

缩进和空行

保证你的代码在80个字符的屏幕宽度内。
缩进是4个空格,这是python的标准。函数参数另起一行并使用8个空格的缩进:

#标准写法,参数另起一行,并且使用8空格的缩进
return 'class: %s, host: %s, args = %s' % (
             self.__class__.__name__, self.hostname, self.args)

return 'class: %s, host: %s, args = %s' % (
             self.__class__.__name__,
             self.hostname,
             self.args)

#这是不标准的写法,不要在函数的参数上使用 4个空格的缩进
return 'class: %s, host: %s, args = %s' % (
         self.__class__.__name__, self.hostname, self.args)

#不标准的写法,需要将函数的参数另起一行
return 'class: %s, host: %s, args = %s' % (self.__class__.__name__,
             self.hostname, self.args)

不要再空行键入写空格或TAB

命名原则

  • 尽可能使用具有描述性的单词命名
  • 变量的命名:variable_names_like_this
  • 方法或者函数的命名:method_and_function_names_like_this
  • 类的命名:UpperCamelCase

导入模块

导入模块的顺序如下:

  • 标准的python模块
  • 非标准python模块
  • Autotest模块

在上述三种模块的导入中,所有使用了from关键字导入的模块都应该出现在没有使用from关键字导入的模块之后。
每个模块的导入另起一行,金陵不要使用通配符导入,如:from x import *

  • 不要导入类,应该导入模块,例如:
from common_lib import error

def foo():
    raise error.AutoservError(...)

而不要:

from common_lib.error import AutoservError

正确的导入示例:

import os
import pickle
import random
import re
import select
import shutil
import signal
import subprocess

import common   # Magic autotest_lib module and sys.path setup code.
import MySQLdb  # After common so that we check our site-packages first.

from common_lib import error

使用None

使用is None代替== None,使用is no None代替!= None。这样你就不会因为一些__eq__或者__ne__的方法调用出现错误的情况。

注释

尽可能的清晰明了地书写注释。

Hardcoded 字符串格式

字符串的书写影噶i使用单引号,当子楚川本身含有单引号时,则使用双引号,多行字符串不应该使用双引号,应该使用单引号分行,
并用括号括起来:

REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
                      'over 80 characters and does not use a slash to '
                      'continue.')

文档描述的字符串格式

文档字符串用于代码的功能记录,虽然没有必要过度记录文档,但需要记录重要功能。在创建文档字符串时,需要再三个单引号开头
添加换行符,并在结尾处添加另一个换行符。如果字符串有多行,请在继续描述其余部分之前包括一个简短的摘要行,后面跟一个空行。
请相应地对句子进行大写和标点处理。如果描述有多行,则在继续处理文本之前放置两级缩进。一个示例:

def foo(param1, param2):
    """
    Summary line.

    Long description of method foo.

    @param param1: A thing called param1 that is used for a bunch of stuff
            that has methods bar() and baz() which raise SpamError if
            something goes awry.

    @returns a list of integers describing changes in a source tree

    @raises exception that could be raised if a certain condition occurs.

    """

可以在文档字符串中根据需要定义多个标签:

TagDescription
@authorCode author
@paramParameter description
@raiseIf the function can throw an exception, this tag documents the possible exception types.
@raisesSame as @raise.
@returnReturn value description
@returnsSame as @return
@seeReference to other parts of the codebase.
@warningCall attention to potential problems with the code
@varDocumentation for a variable or enum value (either global or as a member of a class)
@versionVersion string

编写简洁的代码

尽量使你的代码可读性更高,更加简洁。

函数长度

保证函数简洁,尽可能控制在30行以内。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值