The benefits of Early Returns

Returning early in a method is something that we have debated quite a bit at work, and what I see is that there reasons to return early and times to have a single return.  There are two reasons to return early one which prevents code from executing when it does not need to execute and the other is to make code more readable.  As for having a single return the main reason is for code readability but from a different aspect than returning early.

So to demonstrate the point I have included the same method written with return early and on with a single return here.

In this comparison I have simple code that is checking if a SMTP mail message is valid.  I am checking that the message is not null, there is at least one From address, one To address, and something in the body.  This might be a bit of an extreme example but it does work to illustrate the point that early returns can make the code more readable.  With regard to efficiency the early returns make the code more efficient in that there is no need to continue execution if one of the early message checks fail.  Using this example, if the From address is not valid (not containing an @ symbol) then the code will return a false immediately rather than continue checking running the code.

Something to note with this comparison, the method using the early returns has a total of 27 lines whereas the method using the single return has 46 lines.  So unless you are being paid by the line of code when writing methods such as this, it is better to have early returns.

One additional note, if you code looks like this….

you need to do something else because this is just bad (I was emailed this code and I am not sure of its original source).

What is the point of out of the box SharePoint?

SharePoint is a powerful platform, but is SharePoint really anything without custom coding and is an out of the box solution worth the trouble?  The short answer is if you want a web based file share out of the box is fine…but if you need anything else, then plan on coding and jumping through a few hoops.

So what do you get out of the box?

  • File sharing

  • Basic (very basic) versioning control

  • Change the background and the color

So when does coding being?  It begins when you want to change the “SharePoint” word or logo at the top left of the screen.  When it comes to SharePoint the thought of it as a CMS needs to go away and it needs to be thought of as a platform for development. 

SharePoint demonstrates its extensibility and need for custom development when it comes to utilizing functionality that is promised in the out of the box functionality but is not delivered.  One area that this is very apparent is with version control and retention policies.  Version control works well out of the box but it does not do anything with retention policies meaning that if you need to maintain the current version’s retention policy independent from the historical version’s retention policy custom code will be needed.

To accomplish this functionality there are five requirements.

  1. A document library to hold the current documents.

  2. A content type that can be used for the record with applicable metadata.

  3. A record center with a record library to capture the versions.

  4. The record center must be setup as a Send To location for the SharePoint farm.

  5. Some code with an event receiver to capture the versions.

Using this methodology when a user begins the process to update a document the current version is immediately sent to the event receiver and based on the logic within there is a determination whether or not to archive.  The document is then sent to Send To location (the record center) which will handle the routing to the correct record library.  The record library can have whatever retention policies required by the documents applied.

Functionally there is only a few lines of code that are required for this to function:

  1. public override void ItemUpdating(SPItemEventProperties properties)
  2. {
  3. SPFile file = properties.ListItem.File;
  4. string strOut = "";
  5. OfficialFileResult retVal = file.SendToOfficialFile(out strOut);
  6. string value = retVal.ToString();
  7. }

However there is a bit more code that is required to make this a fully functioning option.  Click Here to see the full code (not including the installer for the feature).

My New Blogging Site :)

So this is my new blogging site.  I have built it from scratch using Drupal, a few existing modules, and a little of my own code.  While there is a temptation to utilize an OOTB solution such as Wordpress or a Drupal distributions (Drupal Commons, Open Atrium, or one of the others) it does not allow you to customize everything on the site.  There are aspects of this site that could not be done as I would like if I did not use the methodologies that I did to create the site.  This is my first of hopefully a lot of blog posts on a varying array of topics (which will be tagged and categorized by topic).

So just to give a tour of what is under the hood of this site (not a full list but some of the more notable modals and stuff)...

  • Drupal Aquia
  • ckEditor
  • Taxonomy
  • Taxonomy Menu
  • Views
  • Administration Menu (because the default one is awful)
  • ColorBox with ColorBox node
  • Oh and it is running in Microsoft Azure with a MS SQL backend :)

So I have not turned on commenting on yet... I may in the future but until then read away, and I will update as I go.