# Python
lp = 100
an = "Vic"
personaquepuedeeditaryborrarlospermisos = 'Admin'
def x(a, b):
return a * b
RECOMENDADO
NO RECOMENDADO
# Python
area = 100
sales_fee = 15
user_email = 'vicobits@gmail.com'
def suma(x, y):
return x + y
Nombres demasiado Cortos/Largos
# Python
storePermissions = 100
def validateEmail(userEmail):
return True
RECOMENDADO
NO RECOMENDADO
# Python
lp = 100
Seguir las convenciones del lenguaje
# Python
def s(a, b):
return a + b
def uxd(store: Store):
return [
{'name': 'Person 1'},
{'name': 'Person 2'}
]
RECOMENDADO
NO RECOMENDADO
# Python
def sumar(a, b):
return a + b
def get_users(store: Store):
return [
{'name': 'Person 1'},
{'name': 'Person 2'}
]
Nombres descriptivos
# Python
def calculate_distance(
x1: float, y2: float, x2: float, y2: float
):
return math.sqrt(
((x1 - x2) ** 2) + ((y1 - y2) ** 2)
)
RECOMENDADO
NO RECOMENDADO
# Python
class Point(object):
x: float = None
y: float = None
def calculate_distance(p1: Point, p2: Point):
return math.sqrt(
((p1.x - p2.x) ** 2) + ((p1.y - p2.y) ** 2)
)
Menos argumentos es mejor
# Python
def convertir_a_dolares(pesos: float):
# 19.8 es el número mágico
return pesos * 19.8
RECOMENDADO
NO RECOMENDADO
# Python
tasa_de_cambio = 19.8
def convertir_a_dolares(pesos: float):
return pesos * tasa_de_cambio
Evitar números Mágicos
# Python
if a = True:
if b = True:
state = 'VALID'
else:
if b = True:
state = 'INVALID'
RECOMENDADO
NO RECOMENDADO
# Python
if a = True and b = True:
state = 'VALID'
state = 'INVALID'
Minimizar el anidado
# Python 1000 lines of code
class Person(object):
def method_a(self):
...
def method_b(self):
...
...
RECOMENDADO
NO RECOMENDADO
# Python 200 lines of code
class Person(object):
def method_a(self):
...
def method_b(self):
...
...
Evitar clases y/o métodos muy grandes
# Esto es una funcion
def is_valid_email(email):
# Definimos un patron regex
email_pattern = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
# retornamos True o False
return bool(re.search(regex,email)
RECOMENDADO
NO RECOMENDADO
def is_valid_email(email):
"""
Verifica correos como:
- usuario@algo.com
- usuario.uno@algo.com.mx
"""
email_pattern = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
return bool(re.search(regex,email)
Comentarios cuando son necesarios
def imprimir_registros_5_veces():
print('Mensaje 1')
print('Mensaje 2')
print('Mensaje 3')
print('Mensaje 4')
print('Mensaje 5')
RECOMENDADO
NO RECOMENDADO
def imprimir_registro(mensaje):
print(mensaje)
def imprimir_registros(veces):
for i in range(veces):
imprimir_registro(f'Mensaje {i + 1}')
No te repitas
# Python
lp = 100 # No se usa
a = 1 + 3
print(a)
RECOMENDADO
NO RECOMENDADO
# Python
a = 5
print(a)
Código que no se usa
Founder - Collectives