Esneyder Amin Palacios Mena

Sr. Software Engineer at @livevox, co-founder of @hackdo 

Clean Code with Javascript

Talk:

Clean Code

Son una serie de normas y  practicas que nos ayudan ha escribir codigo de calidad, facil de entener, probar y de mantener a lo largo del tiempo.

Leer

vs

Escribir

const nombre = "Andrea";
const age = 20;
console.log(`Mi nombre es: ${nombre} y tengo ${age}`);

Nombramiento de variables

const a = Date.now();
const b = 22;
const c = prompt();
const d = build(a, b, c);
// d ??? 

Refactorización

- Mantener funcionalidad

- Mejora el código

 

const age = prompt("Please insert your age");
const name = prompt("Please insert your name");
const currentTime = Date.now();
const user = buildObjectUser(currentTime, age, name); 
function buildObjectUser(time, age, name) {
  if (time <= 340548639 && age >= 19 && age <= 25) {
    return { time, age, name, promition: true };
  }
  return { time, age, name, promition: false };
}
function buildObjectUser({ currentTime: time, age, name }) {
  const isValidTime = time <= 340548639;
  const hasMinimumAge = age >= 19;
  const hasMaximumAge = age <= 25
  const isValidForPromition = isValidTime && hasMinimumAge && hasMaximumAge;
  if (isValidForPromition) {
    return { time, age, name, promition: true };
  }
  return { time, age, name, promition: false };
}
const age = prompt("Please insert your age");
const name = prompt("Please insert your name");
const currentTime = Date.now();
const user = buildObjectUser({ currentTime, age, name }); 
function buildObjectUser({ currentTime: time, age, name }) {
  const isValidTime = time <= 340548639;
  const hasMinimumAge = age >= 19;
  const hasMaximumAge = age <= 25
  const isValidForPromition = isValidTime && hasMinimumAge && hasMaximumAge;
  return { time, age, name, promition: isValidForPromition };
}

Unica responsabilidad

function applyPromotion(user) {
  if (user.promotion) {
    processPromotion();
    return true;    
  } else {
    rejectPromotion();
    return false;
  }
  return false;
}
if (user.promotion) {
  applyPromotion(user.promotion);
} else {
  rejectPromotion();
}

Funciones cortas

Testing

  • Buena practica
  • Minimiza la probabilidad de introducir bugs
  • Software confiable y robusto
// https://jestjs.io/
// 
test('promotion should be true', () => {
  const paylaod = { currentTime: 236763, age: 20, name: "Paola" }
  const user = buildObjectUser(payload); 
  expect(user.promotion)).toBe(true);
});


test('promotion should be false', () => {
  const paylaod = { currentTime: 236763, age: 40, name: "Paola" }
  const user = buildObjectUser(payload); 
  expect(user.promotion)).toBe(false);
});

Modularidad

y

desacoplamiento

Preguntas

Esneyder Amin Palacios Mena

Sr. Software Engineer at @livevox, co-founder of @hackdo 

Clean Code

By Esneyder Amin Palacios Mena

Clean Code

  • 198