David Gómez G. @dgomezg Measuring Code Quality: WTF/min Code from the real world that could give you a stroke and some advices to not replicate them. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/ Leganés 6-7 Febrero 2013
With code examples from the real world that could give you a stroke.
The slides I used on my talk at t3chFest Madrid 2014.
A talk about Code Quality, Clean Code, and good and bad practices when writing code. A fun walk through the experiences from the real world with some advices and introducing some principles that will help you to write better code.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
David Gómez G. @dgomezg
Measuring Code Quality: WTF/min Code from the real world that could give you a stroke and some advices to not replicate them.
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�5
A surface indication that usually
corresponds to a deeper problem in the
system
Term coined by Kent Beck
CODE SMELL
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�6
You write code for a computer to read
Some Misconceptions
You write code to be read by a developer maintaining your code. The computer reads the compiled code
Writing smart code, you will improve performance
Developers spend most of the time writing code
Developers spend most of time reading code
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�6
You write code for a computer to read
Some Misconceptions
You write code to be read by a developer maintaining your code. The computer reads the compiled code
Writing smart code, you will improve performance
Developers spend most of the time writing code
Developers spend most of time reading code
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�6
You write code for a computer to read
Some Misconceptions
You write code to be read by a developer maintaining your code. The computer reads the compiled code
Writing smart code, you will improve performance
Developers spend most of the time writing code
Developers spend most of time reading code
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�6
You write code for a computer to read
Some Misconceptions
You write code to be read by a developer maintaining your code. The computer reads the compiled code
Writing smart code, you will improve performance
Developers spend most of the time writing code
Developers spend most of time reading code
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�7
Examples of real code from the real world
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�8
@dgomezg
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�9
@dgomezg
The following code has been modified slightly
to protect anonymity of authors
!
but always respect the original idea (although it could be difficult to believe)
/* */Measuring Code Quality: WTF/min
Leganés!6-7 Febrero 2014
�10
Comments are useful, butCOMMENTS
@dgomezg
Only if they add information
or if they explain the code
Don’t describe what the code is doing
Explain why (pre/post-conditions)
Use cases (for methods/types/functions)
/* */Measuring Code Quality: WTF/min
Leganés!6-7 Febrero 2014
COMMENTS: Unuseful info
@dgomezg
/* */Measuring Code Quality: WTF/min
Leganés!6-7 Febrero 2014
COMMENTS: Unuseful info
@dgomezg
/** * Método get Disponibilidad. * * @return Returns the disponibilidad. */ public String getDisponibilidad() { // We will get back to the implementation detail // More fun later }
/** * This method removes all selected files * * Returns a jQuery collection of all affected elements. * * @name reset * @type jQuery * @author Daniel B. ( http://www.visit-my-web.com/) * */ reset: function(){
Producto p = null; List prod = null; List listaElementos = new ArrayList(); // if (nElements > 80) { cmd.setOrden(Producto.SELECT_POR_TIPO); l = new ListParam(); l.add(new Integer(idTipoProducto)); l.add(new Integer(idCategoria));
* for the trick to work out, keep the indentation
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�17
Exceptions are just that: ExceptionsExceptions
@dgomezg
Catch only if you can handle it
Declare the exact type
Don’t use for: Flow Control
State demarcation (other than Error)
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
Exceptions: Simply retrowing
@dgomezg
public static Class cargaClase(java.lang.String pnombreClase) throws ClassNotFoundException { ClassLoader oloader = new ClasesUtil().getClass()
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�24
UnNecessary code
@dgomezg
Developers should be lazy
Don’t write code you don’t need
Don’t repeat yourself
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�25
UnNecessary code: Wrapping well-known APIs
@dgomezg
public static String substringBefore(String str,String separator) { return (org.apache.commons.lang.StringUtils
.substringBefore(str, separator)); }
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�26
UnNecessary code: Wrapping well-known APIs
@dgomezg
public String getPropiedad(String clave) { return wrappedCacheAdmin.getProperty(clave); } public void setClaseAlgoritmo(String clase) { wrappedCacheAdmin.setAlgorithmClass(clase); }
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�27
UnNecessary code: Clever? Code
@dgomezg
/** La constante CERO. */ public static final int CERO=0; /** La constante UNO. */ public static final int UNO=1; /** La constante DOS. */ public static final int DOS=2; /** La constante TRES. */ public static final int TRES=3;
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�28
UnNecessary code: Clever? Code
@dgomezg
Challenge: Could you tell what will print the following code?
System.out.println(DOS * TRES);
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�30
UnNecessary code: SOP
@dgomezg
/** * Método get Disponibilidad. * * @return Returns the disponibilidad. */ public String getDisponibilidad() { if (numeroPuestos == 0) return "No"; else return "Si"; }
Introducing SOP: String Oriented Programming
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�31
UnNecessary code: TAKING ADVICE TOO SERIOUSLY
@dgomezg
String concatenation in Java is not recommended. Use StringBuffer or StringBuilder insteadStringBuffer hql = new StringBuffer("select distinct config from Config config ");
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�50
@dgomezg
THe Software RUSTING principleThe software degrades slowly and slightly as time passes…
Sometimes is not slowly neither slightly!!
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�51
@dgomezg
The boy scout ruleTry and leave this world a little better than you found it, and when your turn comes to die you can die happy in feeling that at any rate you have not wasted your time but have done your best.
Robert Stephenson Smyth Baden-Powell
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�52
@dgomezg
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
John F. Woods, September 1991
Measuring Code Quality: WTF/minLeganés!6-7 Febrero 2014
�53
@dgomezg
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.