Top testing thinking? Learn top-level test thinking from Google bosses!

How do Google employees write python code?

Google's internal python code specification

Those who are familiar with python will generally try to follow the pep8 specification, and some companies will also formulate internal code specifications. The purpose of large companies to formulate specifications is not to say how you must use the programming language, but to let everyone abide by the same set of rules, save others the cost of reading the code, and facilitate collaboration and communication.

For individuals, it is a big victory to maintain a unified style and consistency in daily coding, and then add some practical rules to effectively reduce possible bugs when writing code.

Next, I intercepted some interesting points in Google's python code specification, hoping to feel their usefulness more deeply in the future.

1. Import modules and packages, do not import individual classes, functions or variables.

This often simplifies the import process and facilitates namespace management. But the disadvantage is also obvious. When the name is long, the code for calling functions and classes will be very long, which affects readability.

# yes
from sound.effects import echo
echo.EchoFilter()

# no
from sound.effects.echo import EchoFilter
EchoFilter()

2. Imports from the root directory, does not assume arbitrary sys.paths, and does not use relative imports.

Assuming the doctor.who path has been added to sys.path by some means, it should also be imported from the beginning.

# yes
from doctor.who import jodie

# other than doctor.who is already in sys.path
# no
import jodie

3. Use exceptions with caution

The details that need to be paid attention to when using exceptions are:

● Prioritize and rationally use built-in exception classes. For example, when a positive number is required, an error caused by a negative number is passed, and the ValueError type is thrown.

● Never use except to catch all exceptions, which may cause some hidden bugs to be hard to find. You should use separate catches for specific exception types.

● Do not use assert to indicate some unexpected situation, use raise instead.

● Do not add too much logic in the try and except clauses, the larger the size of the try block, the easier it is for unexpected exceptions to be triggered.

Try to use the correct built-in exception types:

def division(a, b):
    if b == 0:
        raise ValueError('b can not be zero')

To avoid catching exceptions globally, specify the exception type:

# yes
try:
    1 / 0
    "abc"[100]
except ZeroDivisionError:
    ...
except IndexError:
    ...

# no
try:
    1 / 0
    "abc"[100]
except:
    ...

Don't use mutable types as function default values. If you modify this variable, the default value will change accordingly.

# yes
def foo(a, b=None):
    if b is None:
        b = []
def foo(a, b: Sequence = ()):

# no
def foo(a, b=[]):
def foo(a, b=time.time()):
def foo(a, b={
    
    }):

5. Pay attention to the implicit boolean value of the conditional expression

● For sequences (strings, lists, tuples), it should be noted that the empty sequence is False. When judging whether it is an empty sequence, use the implicit if not seq judgment instead of if len(seq) == 0;

● To judge whether the number is 0, use number == 0 instead of if not number. Because number may be set to the default value of None.

● To judge whether it is None, use x is None instead of not x.

# yes
.if not users: # sequence
if number == 0:
if i % 10 == 0:
def f(x=None):
    if x is None:

# no
if len(users) == 0:
if number is not None and not number:
if not i % 10:
def f(x=None):
    x = x or []

6. Use decorators with caution

A decorator can perform any operation on a function's arguments or return value, which can lead to surprising hidden behaviors.

Also, decorators are executed at import time, making it difficult to catch and handle errors from decorator code.

When using a decorator, you must write a unit test and explain its function and usage.

The decorator itself does not depend on any files, sockets, database connections. Avoid using the @staticmedthod decorator, in most cases, the same effect can be achieved by encapsulating the method as a module-level function.

7. It is recommended to use type declarations. The benefits of type declarations are very obvious:

● Use type declarations to improve code readability.

● You can also use type checking tools to catch problems early.

● After the type declaration is used, there is no need to describe the parameter type in the doc string.

● In the editor, code hints will be provided according to the type.

But in practice, type declarations are often difficult to maintain. When the code is updated, you must remember to update the type declaration. Outdated type declarations will mislead readers. Python's type declaration cost is relatively high in learning costs.

# yes
name: str = 'yuz'
def func(a: int) -> List[int]:

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves【保100%免费】
insert image description here

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_67695717/article/details/132434646