Código

sostenible

¿Qué es el código sostenible?

Cristian Suarez Dev

cristiansuarez.dev/about/

@cristian_suarez_dev

cristian suarez sin cortes

@criskrus

Cristian Suarez Dev

cristiansuarez.dev/about/

¿De donde viene el termino código sostenible?

¿Qué es

en realidad

código sostenible?

Maintainable code

Código legible

Fácil de modificar

Cubierto por test

Código para hoy

Fácil de leer

y modificar

var TennisGame3 = function (p1N, p2N) {
  this.p2 = 0;
  this.p1 = 0;

  this.p1N = p1N;
  this.p2N = p2N;
};


TennisGame3.prototype.wonPoint = function (pN) {
  if (pN == 'player1')
    this.p1 += 1;
  else
    this.p2 += 1;
};
const TennisGame3 = function (name1, name2) {
  this.scorePlayer1 = 0;
  this.scorePlayer2 = 0;

  this.namePlayer1 = name1;
  this.namePlayer2 = name2;
};

TennisGame3.prototype.wonPoint = function (playerName) {
  if (playerName === 'player1') {
    this.scorePlayer1 += 1;
  } else {
    this.scorePlayer2 += 1;
  }
};

Fácil de leer

y modificar

var TennisGame3 = function (p1N, p2N) {
  this.p2 = 0;
  this.p1 = 0;

  this.p1N = p1N;
  this.p2N = p2N;
};


TennisGame3.prototype.wonPoint = function (pN) {
  if (pN == 'player1')
    this.p1 += 1;
  else
    this.p2 += 1;
};

Fácil de leer

y modificar

const TennisGame3 = function (name1, name2) {
  this.scorePlayer1 = 0;
  this.scorePlayer2 = 0;

  this.namePlayer1 = name1;
  this.namePlayer2 = name2;
};

TennisGame3.prototype.wonPoint = function (playerName) {
  if (playerName === 'player1') {
    this.scorePlayer1 += 1;
  } else {
    this.scorePlayer2 += 1;
  }
};

Convención Naming

Métodos / funciones

Variables

String name;

int distanceInMeters;

Boolean isNegative;
Boolean hasSymbol;


public class Customer {

	/*****/

}

sustantivos

Clases

Paquetes

minúscula

mayúscula

verbos

minúscula

public void saveUser(User user);

public int countUsers(List<User> users);

public Boolean hasUsers(List<User> users);

Ejemplos

public Boolean hasMoreThanMaximumFields(int fields) {
    if (fields > 10) {
        return true;
    }  
    return false;
}

NO Magic numbers/words

public Boolean hasMoreThanMaximumFields(int fields) {
    private int maxFieldsPerRecord = 10;
    
    if (fields > maxFieldsPerRecord) {
        return true;
    }  
    return false;
}

// Not meaningful enough
// private int numberTen = 10;

Ejemplos

public Boolean hasMoreThanMaximumFields(int fields) {
    if (fields > 10) {
        return true;
    }  
    return false;
}

NO Magic numbers/words

Ejemplos

public Boolean hasMoreThanMaximumFields(int fields) {
    private int maxFieldsPerRecord = 10;
    
    if (fields > maxFieldsPerRecord) {
        return true;
    }  
    return false;
}

// Not meaningful enough
// private int numberTen = 10;

Ejemplos

List<String> nameList = new ArrayList<>{"Juan", "Noe", "Maria"};

String addressString = "221b, Baker Street";

NO incluir información técnica

List<String> names = new ArrayList<>{"Juan", "Noe", "Maria"};

String address = "221b, Baker Street";

Ejemplos

List<String> nameList = new ArrayList<>{"Juan", "Noe", "Maria"};

String addressString = "221b, Baker Street";

NO incluir información técnica

Ejemplos

List<String> names = new ArrayList<>{"Juan", "Noe", "Maria"};

String address = "221b, Baker Street";

Ejemplos

function check(foo, bar) {
    if (foo === bar.length) {
        return true
    }
    return false  
}

check(5, 'hello')
List<String> nameList = new ArrayList<>{"Juan", "Noe", "Maria"};

String addressString = "221b, Baker Street";

NO incluir información técnica

List<String> names = new ArrayList<>{"Juan", "Noe", "Maria"};

String address = "221b, Baker Street";

NO nombre genérico + SÍ nombre con sentido

function isWordLengthCorrect(wordLength, word) {
    if (wordLength === word.length) {
        return true
    }
    return false  
}

isWordLengthCorrect(5, 'hello')

Ejemplos

NO usar alias

Customer customer = new Customer("John Smith");

// Different part of the code
Customer client = new Customer("Jane Doe");

NO nombres impronunciables

// None of these names provide useful information
int d;
String n;
boolean sg;
// These names clearly state what they represent
int distanceInLightYears;
String starName;
boolean isSupergiant;

¿Porqué es tan importante la sostenibilidad?

El arte de escribir código para humanos

La degeneración del código

¿Se puede tener un software sostenible?

Diseñar código para el presente

El mito de la reutilización de código

Las reglas del código sostenible

Según Kent Beck

  1. Pasar los test.
  2. Revelar Intención.
  3. No contener duplicidad.
  4. Tener el menor número de elementos posible.

Las reglas del código sostenible

Según Carlos Blé

  1. Código cubierto por test.
  2. Los test son sostenibles.
  3. Las abstracciones tienen sentido.
  4. Hay una intencionalidad explícita.

Las personas primero

¿Por qué es

necesario el

código sostenible?

Piensa en tus compañeros

y en tu "yo" del futuro

«Echarle la culpa a las personas de los desastres en el código es como escupir para arriba; no te lo recomiendo»

 Carlos Blé

Bibliografía

Código sostenible

- Carlos Blé -

Clean Javascript

- Miguel A. Gómez -

Clean Code

- Robert C. Martin -

Bibliografía

Código sostenible

- Carlos Blé -

https://codigosostenible.com/

Gracias

Cristian Suarez Dev

cristiansuarez.dev/about/

@cristian_suarez_dev

cristian suarez sin cortes

@criskrus

Cristian Suarez Dev

cristiansuarez.dev/about/