This repository has been archived by the owner on Oct 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Monads for objects
sergun edited this page Aug 7, 2012
·
1 revision
Before
if (data != null) { Console.WriteLine(data); }
After
data.Do(d=>Console.WriteLine(d));
Before
string workPhoneCode; if (person != null) { if (person.Work != null) { if (person.Work.Phone != null) { workPhoneCode = person.Work.Phone.Code; } } }
After
string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).With(p=>p.Code);
Before
string workPhoneCode = "default code"; if (person != null) { if (person.Work != null) { if (person.Work.Phone != null) { workPhoneCode = person.Work.Phone.Code; } } }
After
string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).Return(p=>p.Code, "default code");
Before
if ((person.Age > 18) && (person.LastName != null) && (person.Work != null)) { Console.WriteLine(person); }
After
person.If(p=>p.Age > 18).IfNot(p=>p.LastName == null).IfNot(p=>p.Work == null).Do(p=>Console.WriteLine(person));
Before
var person = new Person(); // do something if (person == null) { person = new Person(); } Console.WriteLine(person.LastName);
After
var person = new Person(); // do something Console.WriteLine(person.Recover(()=>new Person()).LastName);
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? Console.WriteLine(person.Work.Address); } catch { }
After
var person = ... ; // person.Work = null; person.TryDo(p=>Console.WriteLine(p.Work.Address));
--
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? Console.WriteLine(person.Work.Address); } catch { Console.WriteLine("Error"); }
After
person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch(e=>Console.WriteLine("Error"));
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? Console.WriteLine(person.Work.Address); } catch (NullReferenceException) { Console.WriteLine("Error"); }
After
person.TryDo(p=>Console.WriteLine(p.Work.Address), typeof(NullReferenceException)) .Catch(e=>Console.WriteLine("Error"));
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? Console.WriteLine(person.Work.Address); } catch (NullReferenceException ex) { if (ex.Message.Contains("some word") == false) { throw; } Console.WriteLine("Error"); }
After
person.TryDo(p=>Console.WriteLine(p.Work.Address), e=>e.Message.Contains("some words")) .Catch(e=>Console.WriteLine("Error"));
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? Console.WriteLine(person.Work.Address); } catch { }
After
person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch();
Before
var person = ... ; // person.Work = null; try { // NullReferenceException? var address = person.Work.Address; Console.WriteLine (address.TrimLeft()); } catch { }
After
var person = ... ; // person.Work = null; person.TryWith(p=>p.Work.Address).Catch().Do(p=>Console.WriteLine(p));
Exception matching and handling policies such as for TryDo.