Top Banner
 Simple Simple Pure Pure Java Java Anton Keks Anton Keks [email protected] [email protected]
21

Simple Pure Java

Nov 29, 2014

Download

Technology

Anton Keks

How to bring fun back into Java enterprise development
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Simple Pure Java

   

Simple Simple Pure Pure JavaJava

Anton KeksAnton [email protected]@codeborne.com

Page 2: Simple Pure Java

   

The Enterprise...The Enterprise...

Page 3: Simple Pure Java

   

Enterprise Architecture

20 years ago a new field was born, addressing

System complexity = more and more money for building IT systems

Poor business alignment = more difficult to keep those increasingly expensive systems aligned with business need

A problem of more cost, less valueToday: even more cost, even less value

Page 4: Simple Pure Java

   

ComplexityComplexity

Page 5: Simple Pure Java

   

☠☠BOO!BOO!

Page 6: Simple Pure Java

   

Your average Java (web) appYour average Java (web) app

● Framework (JSF?)● Model● Portal?● Services!● Remote services, SOA, EJB● JNDI● Stubs, generated code● How many layers?

Page 7: Simple Pure Java

   

......

● Patterns!● Factory, Singleton, Facade● Enterprise patterns!● DTO, DAO, etc● Getters/setters

(what's the deal - generate 'em)● JPA, JAXB● JMS!

Page 8: Simple Pure Java

   

Bad wordsBad words

It's always nice to see guys like

SessionServiceContextManager

Layer Tier Bus Context Manager Locator Assembler Bean Broker Facade Transfer Object DAO …

or AbstractStrategyFactoryProxyFacadeBuilder

Page 9: Simple Pure Java

   

EAR, WAR, WAR, WAREAR, WAR, WAR, WAR

Configuration, descriptors!Configuration, descriptors!

JNDI!JNDI!

ESB? ;-)ESB? ;-)

Must be a wizard to make it all run!Must be a wizard to make it all run!

Improves job security index :-)Improves job security index :-)

Page 10: Simple Pure Java

   

For a small thing we createFor a small thing we create

● ManagedBean● MegaServiceWrapper● CoreServiceLocal, Stub (JNDI lookup?)● CoreService (&& CoreServiceImpl)● AggregatedService● ConcreteService● ConcreteDAO● JPAAdapter● ...

Page 11: Simple Pure Java

   

The result?The result?

● It kills productivity● Thousands lines of code, very few

functionality● Well hidden ”business logic”● N minute deploy time (+ N minutes app

server startup)● Oops, sometimes redeploy doesn't work,

need to restart● Slow UI responsivness

Page 12: Simple Pure Java

   

What to do?What to do?

● Concentrate on domain terminology● This was the intention of OOP

● Avoid overly defensive code● You are not writing a framework!● Fellow developers are friends

● Follow Clean Code by Robert C. Martin

Page 13: Simple Pure Java

   

Java platform and languageJava platform and language

● Java (EE) is a victim of JCP● Many unreal/unusable JSRs

● Stick to proven open-source stuff● Less standards – the better

● Java language is ok● The biggest miss are closures● DSLs are possible: Mockito, LambdaJ● Don't bloat (generate) your code

Page 14: Simple Pure Java

   

Code styleCode style

● Is your code style limiting readability?● Avoid too many line breaks and braces● Emphasize what is important

public int size() { if (root == null) { return 0; } else { return root.numSiblings(); }}

public int size() { if (root == null) return 0; return root.numSiblings();}

public int size() { return root != null ? root.numSiblings() : 0;}

Page 15: Simple Pure Java

   

Code styleCode style

● Avoid over-indenation (or code ladder)public void startCharging() { if (customer.hasFunds()) { if (!station.isCharging()) { if (!station.currentlyBooked()) { reallyStartCharging(); return; } } } throw new UnableToStartException();}

public void startCharging() { if (!customer.hasFunds()) throw new UnableToStart if (station.isCharging()) throw new UnableToStart if (station.currentlyBooked()) throw new UnableToStart

reallyStartCharging();}

Page 16: Simple Pure Java

   

Code styleCode style

● Prefer shorter code (use static imports)

import static java.util.Arrays.*;import static java.util.Collections.*;...

return unmodifiableList(asList(1, 2, 3))

List<Integer> list = Arrays.asList(1, 2, 3));list = Collections.unmodifieableList(list);return list;

Looks a bit like functional programming, isn't it?

Page 17: Simple Pure Java

   

Code styleCode style

● Prefer good naming to comments● Avoid getters/setters, equals, hashCode,

toString unless necessary● Break free from 'conventions'● Work towards a DSL

when(session.currentUser()).thenReturn(fakeUser);

assertThat(person.age, is(25));

sort(people, on(Person.class).getAge());

Page 18: Simple Pure Java

   

Proper Java appProper Java app

● Jetty Launcher (esp in development)● Know the APIs well: servlets, filters, etc● Avoid vendor-specific stuff● Keep environment-specific configuration

in version control● Dependency Injection● Avoid scattering cross-cutting concerns● DB migrations (w/ liquibase/dbdeploy)● Start thin and simple, prefer higher SNRSNR

Page 19: Simple Pure Java

   

Web UIWeb UI

● Your framework tells you don't need to know JavaScript? (GWT, JSF, etc)

● B.S.!● Keep it under control: learn basics of

JQuery instead● Knockout.js, Backbone.js can help● You are not limited with Java syntax on

the client side :-)

Page 20: Simple Pure Java

   

Worth reminding...Worth reminding...

●Don't Repeat Yourself

●Keep It Simple Stupid

●You Ain't Gonna Need It

●Test Driven Development

Page 21: Simple Pure Java

   

Let's continue on Let's continue on github:github: github.com/angryziber/simple-javagithub.com/angryziber/simple-java

[email protected]@codeborne.com