Onions and Spaghetti MSCOSCONF2009 #MOSC2010

71
Onions and Spaghetti Pr ogramming Lessons Learnt the Ha r d Way Sara Falamaki CSIRO Network ing T echnolo gies Lab MSC Malaysia Open Source Developers Conference 2009

Transcript of Onions and Spaghetti MSCOSCONF2009 #MOSC2010

Page 1: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 1/71

Onions and SpaghettiProgramming Lessons Learnt the Hard Way 

Sara Falamaki

CSIRO Networking Technologies Lab

MSC Malaysia Open Source Developers Conference 2009

Page 2: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 2/71

Onions and Spaghetti

Page 3: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 3/71

Onions and Spaghetti

• Categorising Code

•Handling Errors and Exceptions

• Global Variables Are Considered Evil

• Unspaghettifying Your Objects

• Controlling Data Flow

• Encapsulation

• Threads are Hard!

Page 4: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 4/71

Categorising Code

Page 5: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 5/71

Categorising Code

Three Types of Code:

Page 6: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 6/71

Categorising Code

Three Types of Code:

1.  Model: Your data structures and algorithms.

Page 7: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 7/71

Categorising Code

Three Types of Code:

1.  Model: Your data structures and algorithms.

2. View: The interfaces to the outside world.

Page 8: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 8/71

Categorising Code

Three Types of Code:

1.  Model: Your data structures and algorithms.

2. View: The interfaces to the outside world.

3. Controller: Code that ties the Model to the

View.

Page 9: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 9/71

Categorising Code

Three Types of Code:

1.  Model: Your data structures and algorithms.

2. View: The interfaces to the outside world.

3. Controller: Code that ties the Model to the

View.

Don’t mix them!

Page 10: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 10/71

Handling Exceptions

Page 11: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 11/71

Handling Exceptions

Page 12: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 12/71

Handling Exceptions

•Almost universally handled badly

Page 13: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 13/71

Handling Exceptions

•Almost universally handled badly

• Only catch it if you can do something aboutit

Page 14: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 14/71

Handling Exceptions

•Almost universally handled badly

• Only catch it if you can do something aboutit

•If you can’t deal with it, let it bubble up and

fail. Fail-Fast code is good!

Page 15: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 15/71

Handling Exceptions

Page 16: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 16/71

Handling Exceptions

• Don’t hot potato exceptions. Try to dealwith each exception only once.

Page 17: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 17/71

Handling Exceptions

• Don’t hot potato exceptions. Try to dealwith each exception only once.

Page 18: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 18/71

Handling Exceptions

Page 19: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 19/71

Handling Exceptions

• Exceptions can really complicate yourprogram state when you catch them.

Page 20: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 20/71

Handling Exceptions

• Exceptions can really complicate yourprogram state when you catch them.

• If you intend to roll back state, make your

try statements as small as possible.

Page 21: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 21/71

Handling Exceptions

bool

Checker::isAvailable(){  try{  QueryResult result;  checkQuery = "SELECT * FROM Devices WHERE available";

   _db.query(checkQuery, result);  if(result.size()==0){  rescheduleTimer(20);  return false;  }  else{  rescheduleTimer(100);  propagateResults(result);

  return true;  }  }  catch(Exception &e){  cerr << "isAvailable: query failed!";  return false;  }

}

Page 22: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 22/71

Handling Exceptionsbool

Checker::isAvailable(){  QueryResult result;  checkQuery = "SELECT * FROM Devices WHERE available";  try{   _db.query(checkQuery, result);  }

  catch(DBTimeoutException &e){  cerr << "isAvailable: query failed! " << e.what();  rescheduleTimer(20);  return false;  }  if(result.size()==0){  rescheduleTimer(20); return

 false;  }  else{  rescheduleTimer(100);  propagateResults(result);  return true;  } 

}

Page 23: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 23/71

Global Variables are

Considered Evil

Page 24: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 24/71

Global Variables are

Considered Evil

Page 25: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 25/71

Global Variables are

Considered Evil

• We all learn this in first year, but seem toforget it too frequently.

Page 26: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 26/71

Global Variables are

Considered Evil

• We all learn this in first year, but seem toforget it too frequently.

• Global variables masquerading as member

variables are also Evil!

Page 27: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 27/71

Why Are Global

Variables Evil?

Page 28: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 28/71

Why Are Global

Variables Evil?

•They make tracking the program’s state

hard.

Page 29: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 29/71

Why Are Global

Variables Evil?

•They make tracking the program’s state

hard.

• They make using multiple threads moredangerous and difficult.

Page 30: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 30/71

Why Are Global

Variables Evil?

•They make tracking the program’s state

hard.

• They make using multiple threads moredangerous and difficult.

• They make testing really really hard.

Page 31: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 31/71

Control Data Flow

Page 32: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 32/71

Control Data Flow

•Make the flow of data through various areas

of your system as simple as possible.

Page 33: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 33/71

Control Data Flow

•Make the flow of data through various areas

of your system as simple as possible.

• Try to avoid multi-directional data flow.

Page 34: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 34/71

Control Data Flow

•Make the flow of data through various areas

of your system as simple as possible.

• Try to avoid multi-directional data flow.

•Think Model-View-Controller when

designing your program.

Page 35: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 35/71

Original Design

Page 36: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 36/71

Original Design

Page 37: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 37/71

New Design

Page 38: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 38/71

New Design

Page 39: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 39/71

Unspaghettifying Your

Ob ects

Page 40: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 40/71

Unspaghettifying Your

Ob ects

• Don’t use an object as a namespace and itsmembers as global variables

• Don’t be afraid of having a library of staticfunctions that do computation.

Page 41: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 41/71

Unspaghettifying Your

Ob ects

Keep your object graphs well trimmed

• Separate your logic from your objectconstruction

• Avoid constructing new objects in your

object - Use Dependency Injection

Page 42: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 42/71

Unspaghettifying Your

Ob ects

Page 43: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 43/71

Unspaghettifying Your

Ob ects

Page 44: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 44/71

Unspaghettifying Your

Ob ects

• Keep your inheritance graphs well trimmed -Don’t write Onion Code!

Page 45: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 45/71

Unspaghettifying Your

Ob ects

• Keep your inheritance graphs well trimmed -Don’t write Onion Code!

• Keep your call graphs well trimmed. Don’thave long chains of function calls.

Page 46: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 46/71

Encapsulation

Page 47: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 47/71

Encapsulation

Page 48: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 48/71

Encapsulation

• Encapsulation is Good

Page 49: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 49/71

Encapsulation

• Encapsulation is Good

• Excessive Encapsulation is Evil

Page 50: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 50/71

Encapsulation

• Encapsulation is Good

• Excessive Encapsulation is Evil

• Excessive Encapsulation is Evil

Page 51: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 51/71

Encapsulation

• Encapsulation is Good

• Excessive Encapsulation is Evil

• Excessive Encapsulation is Evil

•Don’t encapsulate the encapsulated. Don’twrite Onion Code!

Page 52: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 52/71

Encapsulation

class Door{  public:  Door():_handle(Handle()){}

void open(){  if(!_handle.locked()){   _opened = true;  }  }

  void close(){   _opened = false; }  protected:  bool  _opened;  Handle _handle;

}

Page 53: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 53/71

Encapsulation

class RedDoor:public Door{  public:  RedDoor():_colour("red"){}  protected: string _colour;}

class RedDoorWithAluminiumHandle: public RedDoor{  RedDoorWithAluminiumHandle():_colour("red"),_handle(Handle("

aluminium")){}

}

RedDoorWithAluminiumHandle makeRedDoorWithAluminiumHandle(){  return RedDoorWithAluminiumHandle();}

Page 54: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 54/71

Encapsulation

class Door{  public:  Door(string colour, Handle handle):   _colour(colour), _handle(handle){}  void open(){ if(!_handle.locked()){   _opened = true;  }  void close(){   _opened = false;  }  private:  string _colour;

  Handle _handle;}

Door

 makeRedDoorWithAluminiumHandle(){  handle = Handle("aluminium");  return Door("red", handle);}

Page 55: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 55/71

Encapsulation

Page 56: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 56/71

Encapsulation

• Encapsulate logical parts of your code, don’tencapsulate basic computation or IO.

Page 57: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 57/71

Encapsulation

• Encapsulate logical parts of your code, don’tencapsulate basic computation or IO.

• Don’t be afraid to use public membervariables directly

Page 58: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 58/71

Encapsulation

• Encapsulate logical parts of your code, don’tencapsulate basic computation or IO.

• Don’t be afraid to use public membervariables directly

• Make your encapsulated objects general

enough to use in different modules

Page 59: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 59/71

Encapsulation

• Encapsulate logical parts of your code, don’tencapsulate basic computation or IO.

• Don’t be afraid to use public membervariables directly

• Make your encapsulated objects general

enough to use in different modules

• Composition or Inheritance?

Page 60: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 60/71

Threads are Hard

“To offer another analogy, a folk definition of insanity is to do the same thing over and over again and expect the results to be different.

By this definition, we in fact require that  programmers of multithreaded systems beinsane. Were they sane, they could not understand their programs.”  

-Edward A. Lee

Page 61: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 61/71

Page 62: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 62/71

Threads are Hard

Page 63: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 63/71

Threads are Hard

• If it can be done in one thread, do it in onethread.

• Keep the multithreaded minority of yourcode separate from the single threadedmajority.

• Use message passing rather than sharedmemory.

Page 64: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 64/71

Summary

Page 65: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 65/71

Summary

• Minimise your state.

Page 66: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 66/71

Summary

• Minimise your state.• Make your code as simple as possible but no

simpler.

Page 67: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 67/71

Summary

•Minimise your state.

• Make your code as simple as possible but nosimpler.

• Control your data flow.

Page 68: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 68/71

Simplicity is prerequisite for reliability

- Edsger W. Dijkstra

Page 69: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 69/71

Questions?

Page 70: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 70/71

More Information

Details of this talk and a copy of the slidesare available at:

http://sara.falamaki.id.au/moin/Writing/ProgrammingTips

Page 71: Onions and Spaghetti MSCOSCONF2009 #MOSC2010

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

http://slidepdf.com/reader/full/onions-and-spaghetti-mscosconf2009-mosc2010 71/71

Photo Credits

http:// www.flickr.com / photos / 23232902@N05 / 2542353761 / (exception)

http:// mine.icanhascheezburger.com / view.aspx?ciid=3900660 (trim)

http:// mine.icanhascheezburger.com / view.aspx?ciid=4027009 (hot potato)

http:// www.flickr.com / photos / vox / 2208835201 / sizes / o / (pipes)

http:// icanhascheezburger.com / 2008 / 11 / 05 / funny-pictures-see-it-all-started-with-a-loose-thread-and- just-went-downhill-from-there / (threads)

http:// www.flickr.com / photos / designedlykristi / 122619504 / sizes / o / 

http:// icanhascheezburger.com / 2007 / 06 / 02 / im-in-ur-quantum-box /