SlideShare a Scribd company logo
Java Coding Pitfalls
Experience from code review

of 1,5M lines of code

tomi vanek
December 2013
Background
–

Code review of project with 1,5M lines of code
– Static code analysis with SonarQube
•

–
–

PMD, CheckStyle, FindBugs

Refactoring of code
Unit test review and refactoring

Copyright © 2013 Accenture All rights reserved.

2
Categories of code issues
Categories of issues
Issues require
analysis

Inefficiencies

Style

Indication of
programmer
error

Issues require
urgent fix

Potential
bugs

Bugs

Future
programmer error
Severity

Info

Minor

Copyright © 2013 Accenture All rights reserved.

Major

Critical

Source: Campbell, Papapetrou: SonarQube in Action

Blocker

4
Examples for bugs
•

Logic Errors that would lead to null pointer exception

•

Failures to close file handlers, IO streams or database
connections

•

non thread safe behavior in multithreaded environment

•

Methods designed to check equality that always return
false (or true)

•

Impossible class casts

•

Missing overridden hashCode() if method equals() is
overridden

Copyright © 2013 Accenture All rights reserved.

5
Examples for potential bugs
•

Potential NPE, which happen only under certain conditions

•

Null checks that dereference the items they are checking

•

Math operations that use wrong precision or lose precision
(i.e class ImmutableMoney uses double for money value,
instead of BigDecimal)

•

Comparing strings and objects with == and != instead of
.equals() method

•

Conditionals that assign instead of compare: if (foo = 1) { …

•

Catch block that swallow exceptions rather than logging or
throwing further

Copyright © 2013 Accenture All rights reserved.

6
Examples for indication of programmer error
•

Empty conditional body, empty catch or finally block

•

Re-check for null (copy-paste without reading code)

•

Commented out code

•

Unused fields, local variables, private methods

•

Inconsistency in JavaDoc and method declaration
(parameters, return values, checked exceptions)

•

Exception created and dropped rather than thrown

•

Call to equals() comparing different types

•

Self assignment of field

•

Missing Break In Switch

•

Code duplicity
Copyright © 2013 Accenture All rights reserved.

7
Equality
Comparison of String objects
using == or !=
Two string variables
if (foo == bar) {
// action
}

if (foo != null
&& foo.equals(bar)) {
// action
}

String variable and constant string
if (foo == “undefined”) {
// action
}

if (“undefined”.equals(foo)) {
// action
}

Is string variable an empty string?
if (foo != “”) {
// action
}

if (StringUtils.isNotBlank(foo)) {
// action
}

Never use == and != for string comparison
Copyright © 2013 Accenture All rights reserved.

9
Common errors in methods
compareTo, equals and hashCode
•

Class defines compareTo(...) and does not define
equals()
–

•

Class inherits equals()
–

•

Class uses inherited Object.equals(), that is incompatible
with definition of equality in the overridden method
compareTo()
Class uses inherited Object.hashCode(), that is
incompatible with definition of equality in the overridden
method equals()

Use of class without a hashCode() method in a
hashed data structure causes thet the object may not
be found
Whenever you implement equals(), you MUST also implement hashCode()

Copyright © 2013 Accenture All rights reserved.

10
Unintentional overloading of equals()
If equals overloads the method with parameter type (i.e. own class type),
the contains method in List can erroneously return false, even if the object
is in the list
public class Foo {
public boolean equals(Foo o) {
...

public class Foo {
@Override public boolean equals(Object o) {
...

Never forget @Override annotation by equals()
Copyright © 2013 Accenture All rights reserved.

11
Equals method should not assume anything about
the type of its argument

equals() - should expect null and any class type
@Override public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Foo)) return false;
Foo that = (Foo) o;
if (!super.equals(that)) return false;
if (bar != null ? !bar.equals(that.bar) : that.bar != null) return false;
...

Use IDE code generation for creating equals() and hashCode() methods.
Copyright © 2013 Accenture All rights reserved.

12
Class cast exceptions
instanceof will always return false – typically a copy-paste bug
public void execute(Foo foo) {
if (foo instanceof Bar) {
...

Never executes body
of if statement

Impossible cast – typically a copy-paste bug
if (foo instanceof Foo) {
Bar that = (Bar) foo;
...

By execution throws
ClassCastException

Class Cast Exception With To Array
Integer[] a = (Integer [])c.toArray();

ClassCastException

Integer[] b = (Integer [])c.toArray(new Integer[c.size()]);

Avoid copy-paste by programming.
Copyright © 2013 Accenture All rights reserved.

13
Test for floating point equality, test for time equality
Floating point number is NEVER precise
if (foo == 0.) {
// action
}

double EPSILON = .0001;
if (Math.abs(foo) < EPSILON) {
// action
}

Initiation of 2 time-related variables with current time may or may not get
equal value. Time has is typically also precision in the computation
context.
Date foo = new Date();
Date bar = new Date();
...
if (foo == bar) {
...

May be
false, if GC
is running
between
initialization
statements

Long epsilon =
TimeUnit.HOURS.toMilliseconds(1);
Date foo = new Date();
Date bar = new Date();
if (bar.getTime() – foo.getTime()
< EPSILON) {
...

Test imprecise value types with precision interval.
Copyright © 2013 Accenture All rights reserved.

14
Exception handling
Exception information and logging
Log and Throw
Avoid logging and throwing – as this results
in multiple log messages for the same
problem. Exception should be logged at last
resort error handler.

Re-throw include original exception
Exception must keep stack cause chain
information, to provide complete information
for the problem triage.

Proper solution
Not catch exception that cannot be
handled on the method level.
If additional details of conditions / state that
caused the exception should be added to
exception, throw wrapper exception with
details and original exception.

...
} catch (FooException ex) {
LOG.error(ex);
throw new BarException(ex);
}
...
} catch (FooException ex) {
LOG.error(ex);
throw new BarException();
}
...
} catch (FooException ex) {
throw new
BarException(“Details..”, ex);
}

If exception cannot be solved, it should not be caught.
Copyright © 2013 Accenture All rights reserved.

16
Catch
Avoid catching Throwable
Catches also Error classes - fault
situations of JVM, that cannot be
recovered in the application business logic
(i.e. OutOfMemoryException)

Swallowed exception
Catch block does not handle caught
exception and continues in processing, or
returns null

Empty catch or finally block
Mostly programmer error – copy-paste
error, forgotten code by refactoring or not
finished code. If valid empty catch, need
comment with valid explanation.

try {
// guarded block
} catch (Throwable ex) {
// What to do with Error-s?
}
...
} catch (FooException ex) {
return null;
}
...
} catch {
// no code here…
}

Process “expected” exceptions, or throw to caller, if “no cure” on method level.
Copyright © 2013 Accenture All rights reserved.

17
Finally

Return From Finally Block
If finally is called after throwing an
exception, return in finally causes
“swallow” of exception, and the method
returns normally

Throw exception from finally block
If the cleanup logic in finally block throws
an exception, the exception form the
guarded block will get lost (will be
replaced by exception thrown in finally)

...
} finally {
return result;
}
...
} finally {
cleanup();
}

Problem if
cleanup
throws
exception

Never end method processing (by return or exception) in finally block.
Copyright © 2013 Accenture All rights reserved.

18
Exception in catch block

Exception without preserving stack trace
Mainly in case an exception is thrown in
catch block. In this case the cause
exception, that initiated catch block
processing has to be connected to the new
exception before throw with initCause()
method.

...
} catch (FooException fx) {
try {
// code can throw some exc.
} catch (BarException bx) {
bx.initCause(fx);
throw bx;
}
}

If exception happens in catch block, preserve exception cause chain (stack trace).
Copyright © 2013 Accenture All rights reserved.

19
Resource not closed
Failures to close file handlers or database connections
close resources (database connections, files, etc.) in finally block
try{
Connection conn =
getConnection();
// If exception is thrown,
// the following connection
// close is not called,
// and stays open.
con.close();
} catch (SQLException ex) {
LOG.error(ex);
}

Connection conn = null;
try{
conn = getConnection();
// work with DB connection
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
Close in finally block
try {
if (con != null) con.close();
} catch (SQLException ex) {
LOG.error(ex);
}
}

Use framework (Spring) for resource cleanup outside of business code.
Copyright © 2013 Accenture All rights reserved.

20
Null
Logic errors that would lead to null pointer exception
Misplaced Null Check
Pay attention to order in logical
operands - if first statement
determines the result (true in OR,
false in AND), the second is not
executed
Split complex logic into simpler
nested conditions and/or extract
values to local variables

Always do null check against
collection before using it in for-each
statement
If collection processed in for each
loop is null, the loop generates a
NullPointerException

if (foo.getBar() > 0 && foo != null) {
...

if (foo != null && foo.getBar() > 0) {
...

if (fooList != null) {
for (Foo foo : fooList) {
// process foo
}
}

NullPointerException typically occurs due to logical errors in code.
Copyright © 2013 Accenture All rights reserved.

22
Threads and session
Multithreaded access
Common violations:
•

Non-final static fields have non-thread-safe type
–
–

•

Calendar
DateFormat

Initiation and update of static field from non-static
(instance) method

Check for threadsafety in JavaDoc of classes, that are used as mutable field types.
Copyright © 2013 Accenture All rights reserved.

24
Mutable violation in Spring beans
•

•

Singleton Spring beans (default
configuration) must be designed as
stateless, without any instance value
fields

public class Foo {
private Calendar start;

In Spring beans instantiated in
application context (singletons) should
be only fields wired by Spring
dependency injection with references
to Spring beans

public class Foo {
private Bar bar;

public void setStart(
Calendar start_) {
start = start_;
}
Not thread safe
...

public void setBar(Bar bar_) {
bar = bar_;
}
Only for
...
dependency
injection

Singleton Spring beans must be designed as stateless.
Copyright © 2013 Accenture All rights reserved.

25
HTTP session
•

Each object stored into session MUST be serializable
–

•

Limit putting data into session
–

•

Session size has significant performance impact by increased load

Use setAttribute by changes of object value in session
–

•

To manage load, container can persist the session, or synchronize to other
nodes in the container cluster

setAttribute can trigger the session synchronization logic of the container

Store more fine-grained objects stored in session attributes, instead of
single monolithic object
–

Some containers synchronize sessions in container by sending only the
changed objects

Application code should access session objects only through framework.
Copyright © 2013 Accenture All rights reserved.

26
Testability
Test Initiation with non-constant values
•

Symptom: Tests are not reproducible - test results are
changing in time
public void setUp() throws Exception {
fileDate = new Date();
applicationDate = new Date();
...

Typically the 2 time values will be the same. But if
the GC starts between these 2 calls, the initialized
time will be different. This could cause different test
results.

private static final String TEST_DAY = “2010-08-26”;
private static final Date DATE = new SimpleDateFormat(“yyyy-MM-dd”).parse(TEST_DAY);

public void setUp() throws Exception {
fileDate = DATE;
applicationDate = DATE;
...

NEVER initiate the test starting values with current time.
Copyright © 2013 Accenture All rights reserved.

28
Testing implementation details
instead of functionality
•

Symptom: Tests fail by refactoring in implementation
code, that does not change the results of the tested
method. Tests do not check the results in detail (wrong
EasyMock
EasyMock
behavior not found).
.expect(repositoryMock.find(DOC_ID))
.expect(repositoryMock.find(DOC_ID))
.andReturn(testFile);
EasyMock.replay(repositoryMock);
testedFoo.setRepository(repositoryMock);

.andStubReturn(testFile);
EasyMock.replay(repositoryMock);
testedFoo.setRepository(repositoryMock);

testedFoo.handle(requestMock, response);

testedFoo.handle(requestMock, response);

EasyMock
.verify(repositoryMock, requestMock);

assertEquals(200, response.getStatus());
assertEquals(CONTENT_LENGTH,
response. getContentLength());
// ... more assertions for result

Don’t Replace Asserts with Verify.
Copyright © 2013 Accenture All rights reserved.

29
Performance
Maps and sets of URLs can be performance hogs
String representations of URLs are preferred as Map keys over URL
Objects in order to avoid network call overhead.
final URL ACC = new URL(“accenture.com”);
Map<URL, Boolean> urls = new HashMap<URL, Boolean>();
urls.put(ACCENTURE, Boolean.TRUE);
Boolean value = urls.get(ACC);
final String ACC = “accenture.com”;
Map<String, Boolean> urls = new HashMap<String, Boolean>();
urls.put(ACCENTURE, Boolean.TRUE);
Boolean value = urls.get(ACC);

Use String representation of URL as key in hash containers.
Copyright © 2013 Accenture All rights reserved.

31
Dead store to local variable,
unused private methods and fields
•

Instantiation of unused variables, unused private fields
and unused private methods cause unnecessary
increase of memory consumption and processing time.
Date date = new Date();

Unnecessary object initialization – in next statement
thrown away and replaced with a new instance.

date = new SimpleDateFormat(“yyyy-MM-dd”).parse(“2013-12-25”);

final Date date = new SimpleDateFormat(“yyyy-MM-dd”).parse(“2013-12-25”);

Avoid unnecessary instantiation of unused variables.
Copyright © 2013 Accenture All rights reserved.

32
Design
Cyclic module dependency
Copy class into different module
Cyclic module dependencies solved with code copying - duplication
In this example web service API module was externalized, but domain classes
remained in the application. Circular dependency was solved by class duplication.
Application

Application

API

WS Impl

WS API

Value

Value

API

WS Impl

WS API

Domain

Value

Identical copy from
Implementation
module

By modularization avoid code duplication.
Copyright © 2013 Accenture All rights reserved.

34
Same class name in different namespaces
•

java.util.Date

•

java.sql.Date

•

Domain specific Date class
private Date startDate = new Date();

Unique names avoid misunderstandings by code maintenance.
Copyright © 2013 Accenture All rights reserved.

35
Class size and complexity
•

Class size
–

•

number of fields and number of methods

Public interface size
–

number of public methods and constants

•

Size of methods

•

Cyclomatic complexity
–

how many different paths can be performed during a block of code execution

–

calculation: Each method that not is “accessor” (getter) starts the counter with 1.
For each keyword/statement (if, for, while, case, catch, throw, return, &&, ||, ? )
found, the counter is incremented by 1

Complex classes and methods are hard to read, understand and change.
Copyright © 2013 Accenture All rights reserved.

36
Class cohesion
•

LCOM4 (Lack of cohesion)
–

how focused the responsibilities of class are (Wikipedia)
A
B

x

y
LCOM4 = 2

D

A

E

C

B

x

C

D
E

y
LCOM4 = 1

Code with low cohesion is difficult to maintain, test, reuse and understand.
Copyright © 2013 Accenture All rights reserved.

37
Discussion
Reference
•

Books
–
–
–

•

Joshua Bloch: Effective Java (2nd edition)
Campbell, Papapetrou: SonarQube in Action
Steve McConnell: Code Complete (2nd edition)

Internet Articles
–

http://www.odi.ch/prog/design/newbies.php
– http://javaantipatterns.wordpress.com/
– http://www.javapractices.com/home/HomeAction.do
– https://today.java.net/article/2006/04/04/exception-handlingantipatterns

Copyright © 2013 Accenture All rights reserved.

39

More Related Content

What's hot (12)

Exception
ExceptionException
Exception
Harry Potter
 
From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017
Agustin Ramos
 
C++ boot camp part 1/2
C++ boot camp part 1/2C++ boot camp part 1/2
C++ boot camp part 1/2
Jesse Talavera-Greenberg
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Ferdin Joe John Joseph PhD
 
Exception handling
Exception handlingException handling
Exception handling
SAIFUR RAHMAN
 
Checking the code of Valgrind dynamic analyzer by a static analyzer
Checking the code of Valgrind dynamic analyzer by a static analyzerChecking the code of Valgrind dynamic analyzer by a static analyzer
Checking the code of Valgrind dynamic analyzer by a static analyzer
PVS-Studio
 
New land of error handling in swift
New land of error handling in swiftNew land of error handling in swift
New land of error handling in swift
Tsungyu Yu
 
Exception handling
Exception handlingException handling
Exception handling
Prafull Johri
 
Chapter v(error)
Chapter v(error)Chapter v(error)
Chapter v(error)
Chhom Karath
 
Checking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - ContinuationChecking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - Continuation
PVS-Studio
 
Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017From Elixir to Akka (and back) - ElixirConf Mx 2017
From Elixir to Akka (and back) - ElixirConf Mx 2017
Agustin Ramos
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Checking the code of Valgrind dynamic analyzer by a static analyzer
Checking the code of Valgrind dynamic analyzer by a static analyzerChecking the code of Valgrind dynamic analyzer by a static analyzer
Checking the code of Valgrind dynamic analyzer by a static analyzer
PVS-Studio
 
New land of error handling in swift
New land of error handling in swiftNew land of error handling in swift
New land of error handling in swift
Tsungyu Yu
 
Checking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - ContinuationChecking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - Continuation
PVS-Studio
 

Similar to Java coding pitfalls (20)

Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception Handling
Lemi Orhan Ergin
 
12slide.ppt
12slide.ppt12slide.ppt
12slide.ppt
mohaz5
 
2 the essentials of effective java
2 the essentials of effective java2 the essentials of effective java
2 the essentials of effective java
Honnix Liang
 
unit 4 .ppt
unit 4 .pptunit 4 .ppt
unit 4 .ppt
thenmozhip8
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Java 104
Java 104Java 104
Java 104
Manuela Grindei
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
Manuela Grindei
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Adil Mehmoood
 
Finding bugs that matter with Findbugs
Finding bugs that matter with FindbugsFinding bugs that matter with Findbugs
Finding bugs that matter with Findbugs
Carol McDonald
 
Exception+Logging=Diagnostics 2011
Exception+Logging=Diagnostics 2011Exception+Logging=Diagnostics 2011
Exception+Logging=Diagnostics 2011
Paulo Gaspar
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
Peter Kofler
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merkle
bmerkle
 
What is an exception in java?
What is an exception in java?What is an exception in java?
What is an exception in java?
Pramod Yadav
 
Java puzzle-1195101951317606-3
Java puzzle-1195101951317606-3Java puzzle-1195101951317606-3
Java puzzle-1195101951317606-3
rsmuralirs
 
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
Future Processing
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
JAYAPRIYAR7
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
ZeeshanAli593762
 
Exception Handling.pdf
Exception Handling.pdfException Handling.pdf
Exception Handling.pdf
lsdfjldskjf
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception Handling
Lemi Orhan Ergin
 
12slide.ppt
12slide.ppt12slide.ppt
12slide.ppt
mohaz5
 
2 the essentials of effective java
2 the essentials of effective java2 the essentials of effective java
2 the essentials of effective java
Honnix Liang
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
Manuela Grindei
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Adil Mehmoood
 
Finding bugs that matter with Findbugs
Finding bugs that matter with FindbugsFinding bugs that matter with Findbugs
Finding bugs that matter with Findbugs
Carol McDonald
 
Exception+Logging=Diagnostics 2011
Exception+Logging=Diagnostics 2011Exception+Logging=Diagnostics 2011
Exception+Logging=Diagnostics 2011
Paulo Gaspar
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
Peter Kofler
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merkle
bmerkle
 
What is an exception in java?
What is an exception in java?What is an exception in java?
What is an exception in java?
Pramod Yadav
 
Java puzzle-1195101951317606-3
Java puzzle-1195101951317606-3Java puzzle-1195101951317606-3
Java puzzle-1195101951317606-3
rsmuralirs
 
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
Future Processing
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
JAYAPRIYAR7
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
ZeeshanAli593762
 
Exception Handling.pdf
Exception Handling.pdfException Handling.pdf
Exception Handling.pdf
lsdfjldskjf
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
Ad

Recently uploaded (20)

Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Ad

Java coding pitfalls

  • 1. Java Coding Pitfalls Experience from code review of 1,5M lines of code tomi vanek December 2013
  • 2. Background – Code review of project with 1,5M lines of code – Static code analysis with SonarQube • – – PMD, CheckStyle, FindBugs Refactoring of code Unit test review and refactoring Copyright © 2013 Accenture All rights reserved. 2
  • 4. Categories of issues Issues require analysis Inefficiencies Style Indication of programmer error Issues require urgent fix Potential bugs Bugs Future programmer error Severity Info Minor Copyright © 2013 Accenture All rights reserved. Major Critical Source: Campbell, Papapetrou: SonarQube in Action Blocker 4
  • 5. Examples for bugs • Logic Errors that would lead to null pointer exception • Failures to close file handlers, IO streams or database connections • non thread safe behavior in multithreaded environment • Methods designed to check equality that always return false (or true) • Impossible class casts • Missing overridden hashCode() if method equals() is overridden Copyright © 2013 Accenture All rights reserved. 5
  • 6. Examples for potential bugs • Potential NPE, which happen only under certain conditions • Null checks that dereference the items they are checking • Math operations that use wrong precision or lose precision (i.e class ImmutableMoney uses double for money value, instead of BigDecimal) • Comparing strings and objects with == and != instead of .equals() method • Conditionals that assign instead of compare: if (foo = 1) { … • Catch block that swallow exceptions rather than logging or throwing further Copyright © 2013 Accenture All rights reserved. 6
  • 7. Examples for indication of programmer error • Empty conditional body, empty catch or finally block • Re-check for null (copy-paste without reading code) • Commented out code • Unused fields, local variables, private methods • Inconsistency in JavaDoc and method declaration (parameters, return values, checked exceptions) • Exception created and dropped rather than thrown • Call to equals() comparing different types • Self assignment of field • Missing Break In Switch • Code duplicity Copyright © 2013 Accenture All rights reserved. 7
  • 9. Comparison of String objects using == or != Two string variables if (foo == bar) { // action } if (foo != null && foo.equals(bar)) { // action } String variable and constant string if (foo == “undefined”) { // action } if (“undefined”.equals(foo)) { // action } Is string variable an empty string? if (foo != “”) { // action } if (StringUtils.isNotBlank(foo)) { // action } Never use == and != for string comparison Copyright © 2013 Accenture All rights reserved. 9
  • 10. Common errors in methods compareTo, equals and hashCode • Class defines compareTo(...) and does not define equals() – • Class inherits equals() – • Class uses inherited Object.equals(), that is incompatible with definition of equality in the overridden method compareTo() Class uses inherited Object.hashCode(), that is incompatible with definition of equality in the overridden method equals() Use of class without a hashCode() method in a hashed data structure causes thet the object may not be found Whenever you implement equals(), you MUST also implement hashCode() Copyright © 2013 Accenture All rights reserved. 10
  • 11. Unintentional overloading of equals() If equals overloads the method with parameter type (i.e. own class type), the contains method in List can erroneously return false, even if the object is in the list public class Foo { public boolean equals(Foo o) { ... public class Foo { @Override public boolean equals(Object o) { ... Never forget @Override annotation by equals() Copyright © 2013 Accenture All rights reserved. 11
  • 12. Equals method should not assume anything about the type of its argument equals() - should expect null and any class type @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Foo)) return false; Foo that = (Foo) o; if (!super.equals(that)) return false; if (bar != null ? !bar.equals(that.bar) : that.bar != null) return false; ... Use IDE code generation for creating equals() and hashCode() methods. Copyright © 2013 Accenture All rights reserved. 12
  • 13. Class cast exceptions instanceof will always return false – typically a copy-paste bug public void execute(Foo foo) { if (foo instanceof Bar) { ... Never executes body of if statement Impossible cast – typically a copy-paste bug if (foo instanceof Foo) { Bar that = (Bar) foo; ... By execution throws ClassCastException Class Cast Exception With To Array Integer[] a = (Integer [])c.toArray(); ClassCastException Integer[] b = (Integer [])c.toArray(new Integer[c.size()]); Avoid copy-paste by programming. Copyright © 2013 Accenture All rights reserved. 13
  • 14. Test for floating point equality, test for time equality Floating point number is NEVER precise if (foo == 0.) { // action } double EPSILON = .0001; if (Math.abs(foo) < EPSILON) { // action } Initiation of 2 time-related variables with current time may or may not get equal value. Time has is typically also precision in the computation context. Date foo = new Date(); Date bar = new Date(); ... if (foo == bar) { ... May be false, if GC is running between initialization statements Long epsilon = TimeUnit.HOURS.toMilliseconds(1); Date foo = new Date(); Date bar = new Date(); if (bar.getTime() – foo.getTime() < EPSILON) { ... Test imprecise value types with precision interval. Copyright © 2013 Accenture All rights reserved. 14
  • 16. Exception information and logging Log and Throw Avoid logging and throwing – as this results in multiple log messages for the same problem. Exception should be logged at last resort error handler. Re-throw include original exception Exception must keep stack cause chain information, to provide complete information for the problem triage. Proper solution Not catch exception that cannot be handled on the method level. If additional details of conditions / state that caused the exception should be added to exception, throw wrapper exception with details and original exception. ... } catch (FooException ex) { LOG.error(ex); throw new BarException(ex); } ... } catch (FooException ex) { LOG.error(ex); throw new BarException(); } ... } catch (FooException ex) { throw new BarException(“Details..”, ex); } If exception cannot be solved, it should not be caught. Copyright © 2013 Accenture All rights reserved. 16
  • 17. Catch Avoid catching Throwable Catches also Error classes - fault situations of JVM, that cannot be recovered in the application business logic (i.e. OutOfMemoryException) Swallowed exception Catch block does not handle caught exception and continues in processing, or returns null Empty catch or finally block Mostly programmer error – copy-paste error, forgotten code by refactoring or not finished code. If valid empty catch, need comment with valid explanation. try { // guarded block } catch (Throwable ex) { // What to do with Error-s? } ... } catch (FooException ex) { return null; } ... } catch { // no code here… } Process “expected” exceptions, or throw to caller, if “no cure” on method level. Copyright © 2013 Accenture All rights reserved. 17
  • 18. Finally Return From Finally Block If finally is called after throwing an exception, return in finally causes “swallow” of exception, and the method returns normally Throw exception from finally block If the cleanup logic in finally block throws an exception, the exception form the guarded block will get lost (will be replaced by exception thrown in finally) ... } finally { return result; } ... } finally { cleanup(); } Problem if cleanup throws exception Never end method processing (by return or exception) in finally block. Copyright © 2013 Accenture All rights reserved. 18
  • 19. Exception in catch block Exception without preserving stack trace Mainly in case an exception is thrown in catch block. In this case the cause exception, that initiated catch block processing has to be connected to the new exception before throw with initCause() method. ... } catch (FooException fx) { try { // code can throw some exc. } catch (BarException bx) { bx.initCause(fx); throw bx; } } If exception happens in catch block, preserve exception cause chain (stack trace). Copyright © 2013 Accenture All rights reserved. 19
  • 20. Resource not closed Failures to close file handlers or database connections close resources (database connections, files, etc.) in finally block try{ Connection conn = getConnection(); // If exception is thrown, // the following connection // close is not called, // and stays open. con.close(); } catch (SQLException ex) { LOG.error(ex); } Connection conn = null; try{ conn = getConnection(); // work with DB connection } catch (SQLException ex) { throw new RuntimeException(ex); } finally { Close in finally block try { if (con != null) con.close(); } catch (SQLException ex) { LOG.error(ex); } } Use framework (Spring) for resource cleanup outside of business code. Copyright © 2013 Accenture All rights reserved. 20
  • 21. Null
  • 22. Logic errors that would lead to null pointer exception Misplaced Null Check Pay attention to order in logical operands - if first statement determines the result (true in OR, false in AND), the second is not executed Split complex logic into simpler nested conditions and/or extract values to local variables Always do null check against collection before using it in for-each statement If collection processed in for each loop is null, the loop generates a NullPointerException if (foo.getBar() > 0 && foo != null) { ... if (foo != null && foo.getBar() > 0) { ... if (fooList != null) { for (Foo foo : fooList) { // process foo } } NullPointerException typically occurs due to logical errors in code. Copyright © 2013 Accenture All rights reserved. 22
  • 24. Multithreaded access Common violations: • Non-final static fields have non-thread-safe type – – • Calendar DateFormat Initiation and update of static field from non-static (instance) method Check for threadsafety in JavaDoc of classes, that are used as mutable field types. Copyright © 2013 Accenture All rights reserved. 24
  • 25. Mutable violation in Spring beans • • Singleton Spring beans (default configuration) must be designed as stateless, without any instance value fields public class Foo { private Calendar start; In Spring beans instantiated in application context (singletons) should be only fields wired by Spring dependency injection with references to Spring beans public class Foo { private Bar bar; public void setStart( Calendar start_) { start = start_; } Not thread safe ... public void setBar(Bar bar_) { bar = bar_; } Only for ... dependency injection Singleton Spring beans must be designed as stateless. Copyright © 2013 Accenture All rights reserved. 25
  • 26. HTTP session • Each object stored into session MUST be serializable – • Limit putting data into session – • Session size has significant performance impact by increased load Use setAttribute by changes of object value in session – • To manage load, container can persist the session, or synchronize to other nodes in the container cluster setAttribute can trigger the session synchronization logic of the container Store more fine-grained objects stored in session attributes, instead of single monolithic object – Some containers synchronize sessions in container by sending only the changed objects Application code should access session objects only through framework. Copyright © 2013 Accenture All rights reserved. 26
  • 28. Test Initiation with non-constant values • Symptom: Tests are not reproducible - test results are changing in time public void setUp() throws Exception { fileDate = new Date(); applicationDate = new Date(); ... Typically the 2 time values will be the same. But if the GC starts between these 2 calls, the initialized time will be different. This could cause different test results. private static final String TEST_DAY = “2010-08-26”; private static final Date DATE = new SimpleDateFormat(“yyyy-MM-dd”).parse(TEST_DAY); public void setUp() throws Exception { fileDate = DATE; applicationDate = DATE; ... NEVER initiate the test starting values with current time. Copyright © 2013 Accenture All rights reserved. 28
  • 29. Testing implementation details instead of functionality • Symptom: Tests fail by refactoring in implementation code, that does not change the results of the tested method. Tests do not check the results in detail (wrong EasyMock EasyMock behavior not found). .expect(repositoryMock.find(DOC_ID)) .expect(repositoryMock.find(DOC_ID)) .andReturn(testFile); EasyMock.replay(repositoryMock); testedFoo.setRepository(repositoryMock); .andStubReturn(testFile); EasyMock.replay(repositoryMock); testedFoo.setRepository(repositoryMock); testedFoo.handle(requestMock, response); testedFoo.handle(requestMock, response); EasyMock .verify(repositoryMock, requestMock); assertEquals(200, response.getStatus()); assertEquals(CONTENT_LENGTH, response. getContentLength()); // ... more assertions for result Don’t Replace Asserts with Verify. Copyright © 2013 Accenture All rights reserved. 29
  • 31. Maps and sets of URLs can be performance hogs String representations of URLs are preferred as Map keys over URL Objects in order to avoid network call overhead. final URL ACC = new URL(“accenture.com”); Map<URL, Boolean> urls = new HashMap<URL, Boolean>(); urls.put(ACCENTURE, Boolean.TRUE); Boolean value = urls.get(ACC); final String ACC = “accenture.com”; Map<String, Boolean> urls = new HashMap<String, Boolean>(); urls.put(ACCENTURE, Boolean.TRUE); Boolean value = urls.get(ACC); Use String representation of URL as key in hash containers. Copyright © 2013 Accenture All rights reserved. 31
  • 32. Dead store to local variable, unused private methods and fields • Instantiation of unused variables, unused private fields and unused private methods cause unnecessary increase of memory consumption and processing time. Date date = new Date(); Unnecessary object initialization – in next statement thrown away and replaced with a new instance. date = new SimpleDateFormat(“yyyy-MM-dd”).parse(“2013-12-25”); final Date date = new SimpleDateFormat(“yyyy-MM-dd”).parse(“2013-12-25”); Avoid unnecessary instantiation of unused variables. Copyright © 2013 Accenture All rights reserved. 32
  • 34. Cyclic module dependency Copy class into different module Cyclic module dependencies solved with code copying - duplication In this example web service API module was externalized, but domain classes remained in the application. Circular dependency was solved by class duplication. Application Application API WS Impl WS API Value Value API WS Impl WS API Domain Value Identical copy from Implementation module By modularization avoid code duplication. Copyright © 2013 Accenture All rights reserved. 34
  • 35. Same class name in different namespaces • java.util.Date • java.sql.Date • Domain specific Date class private Date startDate = new Date(); Unique names avoid misunderstandings by code maintenance. Copyright © 2013 Accenture All rights reserved. 35
  • 36. Class size and complexity • Class size – • number of fields and number of methods Public interface size – number of public methods and constants • Size of methods • Cyclomatic complexity – how many different paths can be performed during a block of code execution – calculation: Each method that not is “accessor” (getter) starts the counter with 1. For each keyword/statement (if, for, while, case, catch, throw, return, &&, ||, ? ) found, the counter is incremented by 1 Complex classes and methods are hard to read, understand and change. Copyright © 2013 Accenture All rights reserved. 36
  • 37. Class cohesion • LCOM4 (Lack of cohesion) – how focused the responsibilities of class are (Wikipedia) A B x y LCOM4 = 2 D A E C B x C D E y LCOM4 = 1 Code with low cohesion is difficult to maintain, test, reuse and understand. Copyright © 2013 Accenture All rights reserved. 37
  • 39. Reference • Books – – – • Joshua Bloch: Effective Java (2nd edition) Campbell, Papapetrou: SonarQube in Action Steve McConnell: Code Complete (2nd edition) Internet Articles – http://www.odi.ch/prog/design/newbies.php – http://javaantipatterns.wordpress.com/ – http://www.javapractices.com/home/HomeAction.do – https://today.java.net/article/2006/04/04/exception-handlingantipatterns Copyright © 2013 Accenture All rights reserved. 39