Do not have too many nested blocks

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: python-best-practices/nested-blocks

Language: Python

Severity: Error

Category: Code Style

Description

Avoid to nest too many loops together. Having too many loops make your code harder to understand. Prefer to organize your code in functions and unit of code you can clearly understand.

Learn More

Arguments

  • max-levels: Maximum number of nesting levels. Default: 4.

Non-Compliant Code Examples

def func():
	for v in bla:
		if bar:
			if baz:
				if wiz:
					if quux: # too many nested elements
						pass
def func():
	if foo:
		pass
	else:
		if bar:
			if baz:
				if wiz:  # too many nested elements
					pass
def func():
	if foo:
		if bar:
			if baz:
				if wiz:  # too many nested elements
					pass
def func():
	if foo:
		pass
	elif bar:
		if bar:
			if baz:
				if wiz:  # too many nested elements
					pass
	else:
		pass

Compliant Code Examples

while Foo:
    while Bar:
        print("foobar")
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: mcretzman/DOCS-9337-add-cloud-info-byoti