Onions and Spaghetti MSCOSCONF2009 #MOSC2010

Preview:

Citation preview

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

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Onions and Spaghetti

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!

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Categorising Code

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:

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.

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.

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.

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!

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Handling Exceptions

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Handling Exceptions

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

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

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!

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Handling Exceptions

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.

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.

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Handling Exceptions

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.

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.

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;  }

}

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;  } 

}

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

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

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.

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!

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?

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.

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.

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.

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Control Data Flow

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.

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.

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.

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Original Design

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Original Design

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

New Design

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

New Design

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Unspaghettifying Your

Ob ects

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.

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

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Unspaghettifying Your

Ob ects

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Unspaghettifying Your

Ob ects

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!

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.

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Encapsulation

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Encapsulation

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Encapsulation

• Encapsulation is Good

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

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

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!

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;

}

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();}

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);}

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Encapsulation

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.

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

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

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?

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

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Threads are Hard

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.

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Summary

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Summary

• Minimise your state.

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.

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.

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

8/9/2019 Onions and Spaghetti MSCOSCONF2009 #MOSC2010

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

Questions?

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

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 / 

Recommended