Lightning talk on java

50 %
50 %
Information about Lightning talk on java
Technology

Published on February 25, 2014

Author: narsude

Source: slideshare.net

Description

Good Java Programming Practices

Lightning Talk on Java Common Programming Oversights Chetan Narsude January 20th, 2014

Don’t Swallow Exceptions • Mistake try { } catch (Exception e) { e.printStackTrace(); } • Bad try { } catch (Exception e) { logger.warn(“Exception”, e); } Recommended try { } catch (Throwable e) { if (e instanceof Error) { throw (Error)e; } if (e instanceof RuntimeException) { throw (Runtime)e; } • Ugly try { } catch (Exception e) { handleException(e); } /* somewhere else */ public void handleException(Exception e){} throw new RuntimeException(e); }

Don’t wrap Throwable in RuntimeException try { try { … … } } catch (Throwable cause) { catch (Throwable cause) { throw new RuntimeException(cause); } DTThrowable.rethrow(cause); }

Don’t Catch All Throwable try { try { … … } } catch (Throwable cause) { catch (Error error) { throw new RuntimeException(cause); } throw error; } catch (RuntimeException runtime) { throw runtime; } catch (Throwable cause) { throw new Runtime(cause); }

Don’t Use Static Instance of SimpleDateFormat private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); … /* somewhere in the code */ ed = dateFormat.parse(dateInput.readUTF()); … /* somewhere in the code */ ed = new SimpleDateFormat("yyyy-MMdd").parse(dateInput.readUTF()); private static final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat(“yyyy-MM-dd”); } } … /* somewhere in the code */ ed = dateFormat.get().parse(dateInput.readUTF());

Synchronize Only on Final private Object mutex = new Object(); private final Object mutex = new Object(); /* somewhere in the code */ synchronized (mutex) { reload = true; mutex.notify(); } /* somewhere in the code */ synchronized (mutex) { reload = true; mutex.notify(); } /* possibly elsewhere in the code */ public void setMutex(Object mutex) { this.mutex = mutex; }

Avoid Concatenation While Logging /* various ways of wrongly using logger */ logger.debug(“Exception = “ + e.getMessage()); logger.debug(“Exception = {}”, e); logger.debug(“Exception “ + id + “ “ + e.toString()); /* just print the message from exception */ logger.debug(“Exception = {}“, e.getMessage()); /* print the exception stack trace */ logger.debug(“Some Operation”, e); /* print templatized message with exception stack trace */ logger.debug(“Exception {}“, id, e);

Prevent Resource Leak InputStream stream = new …Stream(); InputStream stream = new …Stream(); while (…) { try { process(stream); while (…) { } process(stream); stream.close(); } } finally { stream.close(); }

Add a comment

Related presentations

Related pages

Lightning Talk 84 - Was bringt Java 9 Neues? - eXXcellent ...

Journal Lightning Talks. Übersicht; News; Lightning Talks; Vorträge; Veröffentlichungen; Termine; Presse-Spiegel
Read more

Lightning Talk - Java, Learn by Examples - YouTube

Lightning Talk @ Cannes 2014 - Full Talk: Make Love Not More, James Hilton - Duration: 9:43. Google Creative Sandbox 243 views
Read more

Lightning Talk - Selbständigkeit mit Java

Es wird ein kurzer Überblick über die Möglichkeiten gegeben, sich auf Basis der eigenen Java-Kenntnissen selbständig zu machen. Dabei werden rechtliche ...
Read more

Java 9: A short summary & overview over the new features ...

A short summary & overview over the new features of Java 9. Java 9 Lightning Talk: An overview over the new features? for help, space to navigate
Read more

20.01.2010: Lightning Talk: "Spring Framework Introduction ...

20.01.2010: Lightning Talk: "Spring Framework Introduction" ... 30.03.2011 Java EE Testing easy done with Arquillian and ShrinkWrap. 06.04.2011 Vortrag: ...
Read more

JRebirth – Lightning Talk - JavaFX 2.2 What’s Up

JavaFX 2.2 Lightning Talk ToulouseJUG. This application has been shown during Heroes Event at ToulouseJUG. Require Java 8 Runtime. You can launch the ...
Read more

Lightning Talk - Java Money API - jug-karlsruhe.de

JSR 354, Java Money API standardisiert bisher von fast jedem Projekt "neu erfundene" Geld und Währungs-Typen. Die Session bietet einen Überblick, wie man ...
Read more

Lightning Talk - "Java: What's the Buzz" | SkillsCast ...

LJC: London Java Community community cast. Tori Wieldt: Join Tori Wieldt for a lightening talk on the Buzz around Java
Read more

Lightning Talk: Our experience from Java to Scala ...

Lightning Talk: Our experience from Java to Scala. Paul Dale. Paul is the Principal Architect for Elemica, a leading global Supply Chain services provider.
Read more

Wat lightning talk - Wat — Destroy All Software Talks

Wat. A lightning talk by Gary Bernhardt from CodeMash 2012 The sarcasm in this talk does not represent anyone's actual opinion.
Read more