Standard di Codifica: La Regola dell'Arte

📏 Standard di Codifica: La Regola dell’Arte

In bottega, un incastro deve essere pulito per durare. Nel software, lo standard PEP8 è la nostra pialla: serve a eliminare le asperità e rendere il codice leggibile da chiunque.

1. Regole d’Oro

  • Indentazione: Sempre 4 spazi (mai tab).
  • Naming Convention:
    • snake_case per funzioni e variabili.
    • PascalCase per le classi.
    • SNAKE_UPPER_CASE per le costanti.
  • Type Hinting: Obbligatorio per ogni funzione. Documentare il tipo di input e di output garantisce robustezza.

2. Esempio Pratico

from typing import List, Optional

class WoodProject:
    """Represents a woodworking project."""
    
    def __init__(self, name: str, material: str) -> None:
        self.name = name
        self.material = material
        self.components: List[str] = []

    def add_component(self, component_name: str) -> None:
        """Adds a new component to the project."""
        if component_name:
            self.components.append(component_name)

3. Tooling Consigliato

  • Linter: Flake8 o Ruff per trovare errori formali.
  • Formatter: Black per formattare il codice automaticamente.
  • Type Checker: MyPy per validare i tipi.
Last updated on Sunday, February 15, 2026
Built with Hugo
Theme Stack designed by Jimmy