Created
December 10, 2011 22:36
-
-
Save julien-lafont/1456796 to your computer and use it in GitHub Desktop.
Top 5 des meilleures techniques pour améliorer son code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avant | |
public double calculerCharge(Date date, double qtt) { | |
if (date.before(SUMMER_START) || date.after(SUMMER_END)) | |
// Logique métier | |
else | |
// Logique métier | |
return charge; | |
} | |
// Après | |
public double calculerCharge(Date date, double qtt) { | |
if (isNotSummer(date)) | |
// Logique métier | |
else | |
// Logique métier | |
return charge; | |
} | |
private static bool isNotSummer(Date date) { | |
return date.before(SUMMER_START) || date.after(SUMMER_END); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avant | |
public double getPayAmount() { | |
double result; | |
if (_isDead) result = deadAmount(); | |
else { | |
if (_isSeparated) result = separatedAmount(); | |
else { | |
if (_isRetired) result = retiredAmount(); | |
else result = normalPayAmount(); | |
} | |
} | |
return result; | |
} | |
// Après | |
public double getPayAmount() { | |
if (_isDead) return deadAmount(); | |
if (_isSeparated) return separatedAmount(); | |
if (_isRetirer) return retiredAmount(); | |
return normalPayAmount(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avant | |
public String display(Client client) { | |
if (client == null) return "anonyme"; | |
else return client.nom; | |
} | |
// Après | |
// Introduire un objet "par défaut" | |
public class NullClient extends Client { | |
public NullClient() { | |
this.nom = "anonyme"; | |
} | |
} | |
public String display(Client client) { | |
return client.nom; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avant | |
public void calculerPaye(Date debut, Date fin, ArrayList<Date> conges) { | |
// Logique métier | |
} | |
// Après | |
public void calculerPayer(IntervalleTravail intervalle) { | |
// Logique métier | |
} | |
public class IntervalleTravail { | |
private Date debut; | |
private Date fin; | |
private ArrayList<Date> conges = new ArayList<Date>(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avant | |
public double getSpeed(Vehicule vehicule) { | |
switch(vehicule.type) { | |
case Camion: | |
return getBaseSpeed() * loadFactor(); | |
break; | |
case Avion: | |
return getBaseSpeed() - windDragVector(); | |
break; | |
case Moto: | |
default: | |
return getBaseSpeed(); | |
break; | |
} | |
} | |
// Après | |
public abstract class Vehicule() { | |
protected abstract double getSpeed(); | |
} | |
public class Camion extends Vehicule { | |
protected double getSpeed() { | |
return getBaseSpeed() * loadFactor(); | |
} | |
} | |
public class Avion extends Vehicule { } | |
public class Voiture extends Vehicule { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment