midiendo la calidad de código en wtf/min (revisado eui abril 2014)
TRANSCRIPT
![Page 1: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/1.jpg)
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.
![Page 2: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/2.jpg)
Measuring Code Quality: WTF/min
2
@dgomezg
![Page 3: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/3.jpg)
3
Quality CODE RELATED CONCEPTS
![Page 4: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/4.jpg)
http://www.osnews.com/story/19266/WTFs_m
+
@dgomezg
Measuring Code Quality: WTF/min
![Page 5: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/5.jpg)
5
A surface indication that usually
corresponds to a deeper problem in the
system
Term coined by Kent Beck
CODE SMELL
@dgomezg
![Page 6: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/6.jpg)
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
![Page 7: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/7.jpg)
7
Examples of real code from the real world
![Page 8: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/8.jpg)
8
![Page 9: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/9.jpg)
9
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)
![Page 10: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/10.jpg)
/* */10
Comments are useful, butCOMMENTS
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)
![Page 11: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/11.jpg)
/* */COMMENTS: Unuseful info
@dgomezg
![Page 12: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/12.jpg)
/* */COMMENTS: Unuseful info
/** * Método get Disponibilidad. * * @return Returns the disponibilidad. */ public String getDisponibilidad() { // We will get back to the implementation detail // More fun later }
@dgomezg
![Page 13: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/13.jpg)
/* */COMMENTS: Unuseful info /** Atributo codCircuito. */ private String codCircuito; ! /** * Método get codCircuito. * * @return String */ public String getCodCircuito() { return codCircuito; }
@dgomezg
![Page 14: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/14.jpg)
/* */COMMENTS:
Remember to sign your code////////////////////////Manuela Logger log = LoggerFactory.getLogger(MyClass.class.getName()); ////////////////////////
@dgomezg
![Page 15: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/15.jpg)
/* */COMMENTS:
A good marketing tool/** * 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(){
@dgomezg
![Page 16: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/16.jpg)
/* */COMMENTS:
FOOL THE code READEr 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@dgomezg
![Page 17: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/17.jpg)
17
Exceptions are just that: ExceptionsExceptions
Catch only if you can handle it
Declare the exact type
Don’t use for: Flow Control
State demarcation (other than Error)
![Page 18: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/18.jpg)
Exceptions: Simply retrowingpublic static Class cargaClase(java.lang.String pnombreClase) throws ClassNotFoundException { ClassLoader oloader = new ClasesUtil().getClass()
.getClassLoader(); try { return oloader != null ? oloader.loadClass(pnombreClase) : Class.forName(pnombreClase);
} catch (ClassNotFoundException x) { throw x; } }
@dgomezg
![Page 19: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/19.jpg)
Exceptions: NPE PARANOIA
while (session != null) { numSessions++ ; if (session != null) { ... } ...
}
It’s better to check twice to avoid a NullPointerException
@dgomezg
![Page 20: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/20.jpg)
while (session != null) { numSessions++ ; if (session != null) { //.... } else { log.warn("Null session detected.” +
“ Starting session Synchronization"); //.... dead code follows... } }
Exceptions: NPE PARANOIAIt’s better to check twice to avoid a NullPointerException
extra points if you add ‘dead code’ to the else block
@dgomezg
![Page 21: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/21.jpg)
Exceptions: Flow Controlpublic class ProcesoTerminadoCorrectamenteException extends Exception { !}
Man! When a process which right ends is an exception… That’s a good confidence in your system!
@dgomezg
![Page 22: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/22.jpg)
22
NAMINGUse descriptive names
Describe what a Class/function/method does, not what it means Describe what a variable/attribute holdsDon’t use abbreviations
Code for readability
![Page 23: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/23.jpg)
23
NAMING: What does this method do? if( p != null){ applyBusinessLogic(p,estado,manager); }
public void onButtonPressed(ActionEvent e) { FormData formData = parseFromEvent(e); theAlgorithm(formData); }
@dgomezg
![Page 24: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/24.jpg)
24
UnNecessary code
Developers should be lazy
Don’t write code you don’t need
Don’t repeat yourself
![Page 25: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/25.jpg)
25
UnNecessary code: Wrapping well-known APIs
public static String substringBefore(String str,String separator) { return (org.apache.commons.lang.StringUtils
.substringBefore(str, separator)); }
@dgomezg
![Page 26: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/26.jpg)
26
UnNecessary code: Wrapping well-known APIs
public String getPropiedad(String clave) { return wrappedCacheAdmin.getProperty(clave); } public void setClaseAlgoritmo(String clase) { wrappedCacheAdmin.setAlgorithmClass(clase); }
@dgomezg
![Page 27: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/27.jpg)
27
UnNecessary code: Clever? Code /** 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;
@dgomezg
![Page 28: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/28.jpg)
28
UnNecessary code: Clever? CodeChallenge: Could you tell what will print the following code?
System.out.println(DOS * TRES);
@dgomezg
![Page 29: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/29.jpg)
29
2K Effect… We miss you!
NODO 4 !!# Planificacion Procesos Batch # (Formato ss mm hh dd MM yyyy) # cron.sync=0 00 12 * * ? cron.download=0 00 12 * * 2030 cron.reports=0 00 12 * * 2030
NODO 3 !# Planificacion Procesos Batch # (Formato ss mm hh dd MM yyyy) # cron.sync=0 00 16 * * ? cron.download=0 00 03 * * ? 2030 cron.reports=0 00 03 * * ? 2030
NODO 1 !# Planificacion Procesos Batch # (Formato ss mm hh dd MM yyyy) # cron.sync=0 00 15 * * ? cron.download=0 00 23 * * ? 2030 cron.reports=0 00 23 * * ? 2030
@dgomezg
![Page 30: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/30.jpg)
30
UnNecessary code: SOP
/** * Método get Disponibilidad. * * @return Returns the disponibilidad. */ public String getDisponibilidad() { if (numeroPuestos == 0) return "No"; else return "Si"; }
Introducing SOP: String Oriented Programming
@dgomezg
![Page 31: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/31.jpg)
31
UnNecessary code: TAKING ADVICE TOO SERIOUSLY
String concatenation in Java is not recommended. Use StringBuffer or StringBuilder insteadStringBuffer hql = new StringBuffer("select distinct config from Config config ");
Query query = session.createQuery(hql.toString()) ;
Don’t push the recommendations to the limits!!!@dgomezg
![Page 32: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/32.jpg)
32
UnNecessary code: The indentation MADNESS
} }); } } }); } }); } } ] } }, ] }); });
@dgomezg
![Page 33: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/33.jpg)
33
UnNecessary code: PROTECT YOUR CODE
static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, UInt16 signatureLen) { OSStatus err; ... ! if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail; ... } // https://www.imperialviolet.org/2014/02/22/applebug.html
![Page 34: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/34.jpg)
34
DESIGN PRICIPLES
![Page 35: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/35.jpg)
35
KEEP IT SIMPLE STUPID!
@dgomezg
![Page 36: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/36.jpg)
36
Don’t Repeat Yourself
@dgomezg
![Page 37: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/37.jpg)
37
Separation OF Concerns
@dgomezg
![Page 38: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/38.jpg)
38
I: I
!
!
!
!
!
SEPARATION OF CONCERNS DON’T REPEAT YOURSELF
@dgomezg
![Page 39: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/39.jpg)
39
ENSURING CODE QUALITY: THE QUALITY CYCLE
![Page 40: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/40.jpg)
40
Default Quality Cycle
@dgomezg
![Page 41: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/41.jpg)
41
Add Intermediate Quality Checks
@dgomezg
![Page 42: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/42.jpg)
42
Automate Intermediate Checks
@dgomezg
![Page 43: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/43.jpg)
43
Measure with tools
@dgomezg
![Page 44: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/44.jpg)
44
Define Business Key Indicators
@dgomezg
![Page 45: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/45.jpg)
45
Define Alarms
@dgomezg
![Page 46: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/46.jpg)
46
Get a Dashboard
@dgomezg
![Page 47: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/47.jpg)
47
Added Value: keeping everybody happy
@dgomezg
![Page 48: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/48.jpg)
48
TOOLS
![Page 49: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/49.jpg)
49
descriptor file +
source code +
Dependency Manager =
Artifact
Automated Build process
![Page 50: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/50.jpg)
50
TDD
http://reddevnews.com/articles/2007/11/01/testdriven-development-tdd.aspx
http://www.doolwind.com/blog/page/3/
![Page 51: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/51.jpg)
51
Jenkins Jobs to automate the build process: - On Events (commit) - Scheduled (cron expression) - Manually (upon request) !
Lots of plugins: - Release - Deploy - Customize dashboard - Trigger other checks
![Page 52: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/52.jpg)
52
SONARQbe (aka SONAR)
![Page 53: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/53.jpg)
53
SOME FINAL ADVICES
![Page 54: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/54.jpg)
54
Read! Keep your brain healthy
Clean Code:
Robert Martin (@unclebob)
![Page 55: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/55.jpg)
55
No Monkeys, No LizardsBe concious, be consequent
www.adictosaltrabajo.com/detalle-‐noticia.php?noticia=356
![Page 56: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/56.jpg)
56
THe Software RUSTING principleThe software degrades slowly and slightly as time passes…
Sometimes is not slowly neither slightly!!
![Page 57: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/57.jpg)
57
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
![Page 58: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/58.jpg)
58
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
![Page 59: Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)](https://reader031.vdocuments.mx/reader031/viewer/2022020306/554f712cb4c905bb178b51de/html5/thumbnails/59.jpg)
59
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
////////////////////////Manuela Logger log = LogFactory.getLogger(MyClass.class.getName()); ////////////////////////