legacy projects: how to win the race

59
Legacy Projects How to win the race

Upload: victorcr

Post on 07-Jul-2015

495 views

Category:

Software


1 download

DESCRIPTION

The presentation of the talk "Legacy projects: how to win the race". Made by me on JEEConf Ukraine in May 2014.

TRANSCRIPT

Page 1: Legacy projects: how to win the race

Legacy ProjectsHow to win the race

Page 2: Legacy projects: how to win the race

Technical Lead @ Infopulse UAVictor Polischuk

Many years of legacy systems reviving and refactoring experience

Loves it

E-mail:[email protected]

Skype:victor-cr

Page 3: Legacy projects: how to win the race

Legacy Projects History

2004

Others Java

2014

Others Java

Page 4: Legacy projects: how to win the race

What is……legacy projects?

Page 5: Legacy projects: how to win the race

Comparison Sheet

LegacyEarned a fortune

Important

Predictable

CrappyDesigned by morons

Boring

Unmaintainable

No documentation

Page 6: Legacy projects: how to win the race

Legacy

Crappy

Page 7: Legacy projects: how to win the race

What is the main difference?

Happy owner

Better quality

No difference

Project cost

Page 8: Legacy projects: how to win the race

What is the main difference?

Happy owner

Better quality

No difference

Project cost

Page 9: Legacy projects: how to win the race

Why Does……a legacy project born?

Page 10: Legacy projects: how to win the race

Time passed

Time

Because of time

Time passed

Tim

e

Because of time

Is it the ONLY reason?

Page 11: Legacy projects: how to win the race
Page 12: Legacy projects: how to win the race
Page 13: Legacy projects: how to win the race

Would you choose...

Page 14: Legacy projects: how to win the race

Shiny

Page 15: Legacy projects: how to win the race

DullMainstream

Page 16: Legacy projects: how to win the race

Cutting

Edge New technologies

Challenge unknown

Evolve

You are special

Main-

stream Boring stack

Challenge patience

Nothing new

You are just as everyone

Page 17: Legacy projects: how to win the race

Terrible Project

Clean Project

In future Now

Legacy

Page 18: Legacy projects: how to win the race

No escape

Page 19: Legacy projects: how to win the race

ReWRite……or not?

Page 20: Legacy projects: how to win the race

1. Specify

2. Estimate

3. Compare

4. ?????

5. Profit!!!!!

Page 21: Legacy projects: how to win the race

Failed somewhere?

Page 22: Legacy projects: how to win the race

Refactoring…cookbook

Page 23: Legacy projects: how to win the race

Boy-scout ruleLibraries upgrade

Build migrationCode

transformation

Code generation

Miscellaneous

Page 24: Legacy projects: how to win the race

Keep Stack Updated

Page 25: Legacy projects: how to win the race

Obsolete

Let’s upgrade

MigrationWe are good

Production

Page 26: Legacy projects: how to win the race

Build Health

Page 27: Legacy projects: how to win the race

Checkout

/

Clone

???Working

Build

Page 28: Legacy projects: how to win the race

Bulk Code Changes

Page 29: Legacy projects: how to win the race

Know Your ToolsRegexp:(?ms)public\s+static\s+([^{]+).*?(?=\s+public\s+|}s+\z)

XSLT:<xsl:stylesheet…>

<xsl:template match="/">

<beans>

<xsl:apply-templates

select="struts-config/action-mappings/*"/>

</beans>

</xsl:template>

<xsl:template match="action">

<bean id="{@path}" class="{@type}"/>

</xsl:template>

</xsl:stylesheet>

Page 30: Legacy projects: how to win the race

Prototyping

Page 31: Legacy projects: how to win the race

Scripting

Visio

Rational Rose

Power Designer

Page 32: Legacy projects: how to win the race

Script

Data Model

Template Engine

Generated Prototype

Page 33: Legacy projects: how to win the race

Maintain Clean Code

Page 34: Legacy projects: how to win the race

ResultSet aResultSet = null;ArrayList theAttribute1List = new ArrayList();ArrayList theAttribute2List = new ArrayList();…ArrayList theAttribute30List = new ArrayList();…StringBuffer aQuery = new StringBuffer("select ").append("ATTRIBUTE1,").append("ATTRIBUTE2,")

…while (aResultSet.next()) {if (aResultSet.getString("ATTRIBUTE1") != null) theAttribute1List.add(aResultSet.getString("ATTRIBUTE1"));

…htAttributeList.put("ATTRIBUTE1", distinct(theAttribute1List));htAttributeList.put("ATTRIBUTE2", distinct(theAttribute2List));

010

Page 35: Legacy projects: how to win the race

What have you done to prevent me seeing it?

Page 36: Legacy projects: how to win the race

Universal soldier...

Page 37: Legacy projects: how to win the race

public class UniversalComparator implements Comparator {…if (value1 instanceof GregorianCalendar) {GregorianCalendar lcal_obj1 = (GregorianCalendar) value1;GregorianCalendar lcal_obj2 = (GregorianCalendar) value2;boolean lb_value = lcal_obj1.before(lcal_obj2);if (ii_comparatorDirection == COMP_ASC) {if (lb_value == true) {return 1;

} else {return -1;

}} else {if (lb_value == true) {return -1;

} else {return 1;

}}

} 07

Page 38: Legacy projects: how to win the race

if (value1 instanceof Boolean) {Boolean lv_obj1 = (Boolean) value1;Boolean lv_obj2 = (Boolean) value2;

String ls_value1 = lv_obj1.toString();String ls_value2 = lv_obj2.toString();

logService.debug("compare: lb_value1, lb_value2: "+ ls_value1 + ", " + ls_value2);

int lv_value1 = (ls_value1 == "true") ? 1 : 0;int lv_value2 = (ls_value2 == "true") ? 1 : 0;

if (ii_comparatorDirection == COMP_ASC) {int val = (lv_value1 < lv_value2) ? -1 : 1;// log("val: " + val);

return (lv_value1 < lv_value2) ? -1 : 1;} else return (lv_value1 < lv_value2) ? 1 : -1;

}

06

Page 39: Legacy projects: how to win the race

JavaDoc... No, have never heard

Page 40: Legacy projects: how to win the race

private void moveFile(String from, String to) throws Exception {//move files from 'from' to 'to'

String[] cmd;if (File.separator.compareTo("\\") == 0) { //windowscmd = new String[] {"cmd", "/c", "move",

from.trim().replace("/", File.separator),to.trim().replace("/", File.separator)

};} else {//linux like, simple mv command does not work, using script

cmd = new String[] {parameters.get("movePath") + ".sh",from.trim().replace("/", File.separator),to.trim().replace("/", File.separator)

};}System.gc(); //reduces current process size before forkProcess p = Runtime.getRuntime().exec(cmd);p.waitFor();p.destroy(); //free up memory

} 05

Page 41: Legacy projects: how to win the race

Code review?... WTF?

private static final List<Option> DEFAULT_OPTIONS = new ArrayList<Option>(4);

{DEFAULT_OPTIONS.add(new Option(1, "Unavailable"));DEFAULT_OPTIONS.add(new Option(2, "Unidirectional"));DEFAULT_OPTIONS.add(new Option(3, "Bidirectional"));DEFAULT_OPTIONS.add(new Option(4, "Not applicable"));

}

04

Page 42: Legacy projects: how to win the race

Fail of fail-over protection

Page 43: Legacy projects: how to win the race

public static void lockPartyWithLog(Party p, UnitOfWork uow) {// Local variables

Party partyClone = null;// Lock the object// LOCK_NOWAIT : an exception occurs if the object is being locked

try {partyClone = (Party) uow.refreshAndLockObject(p,

ObjectLevelReadQuery.LOCK_NOWAIT);} catch (DatabaseException dbe) {logService.info("The party ID = " + p.getId()

+ " is locked by an other process");try {Thread.currentThread().sleep(1000);

} catch (Exception e) {logService.error("Thread Exception ", e);

}lockPartyWithLog(p, uow);

}}

03

Page 44: Legacy projects: how to win the race

Master class of API design

Page 45: Legacy projects: how to win the race

public interface Parser {void setReport(InputStream inputstream);void setReport(String s);String getReport();void save();void delete(String Query) throws HibernateException;void setParams(Map<String, String> map);Map<String, String> getParams();void saveReport(String reportPath);Boolean isDuplicated(String fileName);

}

23: public class PMScanReport extends AbstractParser23: implements Parser {..........1556: }

02

Page 46: Legacy projects: how to win the race

Fatality!

Page 47: Legacy projects: how to win the race

public int compare(TradeConfirmation tc1, TradeConfirmation tc2) {int value;Offer o1 = (Offer) tc1.getOffer();Offer o2 = (Offer) tc2.getOffer();if (o1.getTradingInterval() < o2.getTradingInterval()) {

value = -1;} else {

if (o1.getTradingInterval() == o2.getTradingInterval()) {if (o1.getType() < o2.getType()) {value = -1;

} else {if (o1.getType() == o2.getType()) {

PartyDef p1 = o1.getParty().getEffectiveNow();PartyDef p2 = o2.getParty().getEffectiveNow();if (p1.getName().compareTo(p2.getName()) < 0) {

value = -1;} else {

if (p1.getName().compareTo(p2.getName()) == 0) {if (o1.getTradingZone().getEffectiveNow().getIdentification().compareTo(…) < 0) {

value = -1;} else {

if (o1.getTradingZone().getEffectiveNow().getIdentification().compareTo(…)) == 0) {value = 0;

} else {value = 1;

}}

} else {value = 1;

}}

} else {value = 1;

}}

} else {value = 1;

}}return value;

}

01

Page 48: Legacy projects: how to win the race

What Else?

Page 49: Legacy projects: how to win the race

Transaction Management

Dependency Injection

Mass Relocation

Various Migrations

Page 50: Legacy projects: how to win the race

MindsetPrepare yourself

Page 51: Legacy projects: how to win the race

Legacy Law

Successful

Legacy Non-legacy

Legacy

Crappy Non-crappy

Page 52: Legacy projects: how to win the race

I cannot understand

It is

a crap

Assumptions

Page 53: Legacy projects: how to win the race

I cannot understand

It may be

a crap

Assumptions

Page 54: Legacy projects: how to win the race

Victim

Warrior

Page 55: Legacy projects: how to win the race

Pet Clinic Comparison

Regular Successful

Does not care

Does care

Page 56: Legacy projects: how to win the race

Who would you trust with your most precious pet?

Page 57: Legacy projects: how to win the race

ConclusionsFinally

Page 58: Legacy projects: how to win the race

Legacy is everywhere

• You cannot hide or pretend it has nothing to do with you

Legacy means success

• Truly crappy things do not live long

Knowledge is a weapon

• People feel calm when encounter known problems

Change your mindset

• It is up to you: run crying or deal with it like a boss

Trust is important

• It also influences your freedom in decision making

Page 59: Legacy projects: how to win the race

QuestionsThank you for attention