SlideShare uma empresa Scribd logo
1 de 8
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();
}

Mais conteúdo relacionado

Semelhante a Lightning talk on java

Exceptions in PHP
Exceptions in PHPExceptions in PHP
Exceptions in PHPJanTvrdik
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
exceptionhandlinginjava-140224181412-phpapp02.pptx
exceptionhandlinginjava-140224181412-phpapp02.pptxexceptionhandlinginjava-140224181412-phpapp02.pptx
exceptionhandlinginjava-140224181412-phpapp02.pptxARUNPRANESHS
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception HandlingPrabhdeep Singh
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
Better Strategies for Null Handling in Java
Better Strategies for Null Handling in JavaBetter Strategies for Null Handling in Java
Better Strategies for Null Handling in JavaStephan Schmidt
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfFurkan Furkan
 
About java
About javaAbout java
About javaJay Xu
 

Semelhante a Lightning talk on java (10)

Exceptions in PHP
Exceptions in PHPExceptions in PHP
Exceptions in PHP
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
exceptionhandlinginjava-140224181412-phpapp02.pptx
exceptionhandlinginjava-140224181412-phpapp02.pptxexceptionhandlinginjava-140224181412-phpapp02.pptx
exceptionhandlinginjava-140224181412-phpapp02.pptx
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
Exception handling
Exception handlingException handling
Exception handling
 
Better Strategies for Null Handling in Java
Better Strategies for Null Handling in JavaBetter Strategies for Null Handling in Java
Better Strategies for Null Handling in Java
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdf
 
About java
About javaAbout java
About java
 

Último

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 

Último (20)

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 

Lightning talk on java

  • 1. Lightning Talk on Java Common Programming Oversights Chetan Narsude January 20th, 2014
  • 2. 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); }
  • 3. Don’t wrap Throwable in RuntimeException try { try { … … } } catch (Throwable cause) { catch (Throwable cause) { throw new RuntimeException(cause); } DTThrowable.rethrow(cause); }
  • 4. 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); }
  • 5. 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());
  • 6. 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; }
  • 7. 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);
  • 8. Prevent Resource Leak InputStream stream = new …Stream(); InputStream stream = new …Stream(); while (…) { try { process(stream); while (…) { } process(stream); stream.close(); } } finally { stream.close(); }