Nulls and UIs#
I often use simple base classes for things like address and name for more complex data structures, partly for readability and partly for easy formatting overrides. So a User class might look something like:
	class Subscriber
	{
		Base.Name Name;
		Subscriber()
		{
			this.Name = new Name();
		}
	}
	
and the Name class would look as such:
	class Name
	{
		string First = "";
		string Last = "";
		override string ToString()
		{
			return this.First + " " + this.Last;
		}
	}
	
Why not null or string.empty for the Name defaults? Quite simply those values don't play well in the UI and introducing them there seems to cause more code and potential gotchas than it solves. In the UI, this class structure is as simple as I think it can be made:
	User user = new User();
	something = user.Name.First;
	
Does the Law of Demeter apply here? No - the Name object cannot be null and cannot return null parameter values - those might be empty, but of course, we should probably checked if the User object was actually loaded correctly to catch if the entire object is null.
	User user = new User();
	if(user.Load(SomeId))
	{
		something = user.Name.First;
		somethingelse = user.Name.ToString();
	}
	else
	{
		//handle when User is still null
	}
	
I'm always looking for readability and simplicity, I think this solves both well, what do you think?
Wednesday, March 05, 2008 11:33:07 AM (Eastern Standard Time, UTC-05:00) by Dustin #    Comments [0]  |  Tracked by:
http://www.google.com/search?q=xpldclzq [Pingback]
http://www.toysfortots.org/news/details.asp?ID=148 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1265 [Pingback]
http://www.ast-ss.com/articles/article.asp?AID=320 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1203 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1249 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1217 [Pingback]
http://www.mima.org/events/past.asp?eventID=151 [Pingback]
http://www.mixx-expo.com/2.7/sponsor_detail.aspx?sponsor_id=181 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1211 [Pingback]
http://www.toysfortots.org/news/details.asp?ID=146 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1267 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1275 [Pingback]
http://www.johnforceracing.com/news_show.cfm?id=996 [Pingback]
http://www.berettausa.com/product/rifles/series_page.cfm?currentseries=326225862 [Pingback]
http://www.ast-ss.com/articles/article.asp?AID=311 [Pingback]
http://www.mima.org/events/past.asp?eventID=140 [Pingback]
http://www.johnforceracing.com/news_show.cfm?id=1002 [Pingback]
http://www.autocarindia.com/new/RoadtestDetails.asp?id=1259 [Pingback]
http://www.mixx-expo.com/2.7/sponsor_detail.aspx?sponsor_id=172 [Pingback]
http://www.mixx-expo.com/2.7/sponsor_detail.aspx?sponsor_id=178 [Pingback]
http://www.johnforceracing.com/news_show.cfm?id=995 [Pingback]
http://www.johnforceracing.com/news_show.cfm?id=992 [Pingback]
http://www.johnforceracing.com/news_show.cfm?id=1013 [Pingback]
http://www.gettingpersonal.co.uk/clintoncards/gift.asp?Gift=5055 [Pingback]
http://www.comparewebhosts.com/Article.asp?artid=778958128 [Pingback]
http://www.pega.com/Careers/JobDescriptions/JobDescriptionA.asp?ID=590 [Pingback]
http://www.vroomvroomvroom.com.au/more.aspx?D=373892990 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=360122893 [Pingback]
http://info.uwe.ac.uk/events/default.asp?Category=501291721 [Pingback]
http://www.spc.ox.ac.uk/Staff/69/Staff.html?StaffId=240 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=880626309 [Pingback]
http://info.uwe.ac.uk/events/default.asp?Category=123622010 [Pingback]
http://www.gettingpersonal.co.uk/clintoncards/gift.asp?Gift=5057 [Pingback]
http://www.comparewebhosts.com/Article.asp?artid=619066350 [Pingback]
http://www.pcx.com.ph/showcase/profile.asp?id=1976 [Pingback]
http://www.pega.com/Careers/JobDescriptions/JobDescriptionA.asp?ID=578 [Pingback]
http://www.gettingpersonal.co.uk/clintoncards/gift.asp?Gift=5064 [Pingback]
http://www.pcx.com.ph/showcase/profile.asp?id=1436 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=800208274 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=642440260 [Pingback]
http://www.pega.com/Careers/JobDescriptions/JobDescriptionA.asp?ID=764 [Pingback]
http://www.comparewebhosts.com/Article.asp?artid=753952198 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=913654965 [Pingback]
http://www.entertainoz.com.au/index.cfm?oid=438743043 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=90 [Pingback]
http://www.spc.ox.ac.uk/Staff/69/Staff.html?StaffId=207 [Pingback]
http://www.vroomvroomvroom.com.au/more.aspx?D=462703973 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8071 [Pingback]
http://www.vroomvroomvroom.com.au/more.aspx?D=252649875 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=105 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8070 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=96 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=102 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=111 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=113 [Pingback]
http://www.ccisd.net/board_05/view_report.asp?reportID=108 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8094 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8089 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8083 [Pingback]
http://www.discoverlongisland.com/calendar_detail.cfm?event=8101 [Pingback]
http://www.lakeplacid.com/shared/media/press_release_articles.cfm?id=311 [Pingback]
http://www.referaty.cz/akce/akcedetail.asp?id=5376 [Pingback]
http://www.referaty.cz/akce/akcedetail.asp?id=5356 [Pingback]
http://www.referaty.cz/akce/akcedetail.asp?id=5386 [Pingback]
http://www.referaty.cz/akce/akcedetail.asp?id=5361 [Pingback]
http://www.referaty.cz/akce/akcedetail.asp?id=5370 [Pingback]


 

All content © 2008, Dustin Updyke


Search
On this page
Archives
By The Minute