Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Real Life - Code Reuse

A few months ago I blogged about how we're using the single responsibility principle in our application at work.  I'm going to make an effort here to write more "Real Life" content.  Often when a developer read principles or ideas in books, there is disconnect between what he/she is reading and how an implementation would look in the real world.  The goal of these "Real Life" scenarios is to show areas where I feel I've reaped some benefit from following a specific pattern or practice.  This does not mean that my implementation is the only way or always "book correct".  The goal is to act as a bridge (quite possibly a old rickety bridge) between the book knowledge and the real world.

Moving along...

One of the purported benefits of OOP has always been the abundant reuse of code.  However, in my experience many applications fail to realize any amount of reuse beyond copying and pasting between applications.  To really take advantage of code reuse you have to have small, concise pieces which you can rearrange in order to make a new behavior.  As a side note, and I want this be clear, many of the principles of good software development, ie. SOLID, go hand-in-hand.  You can't do one without the other and the benefits of one cannot be fully realized without the implementation of the others.  You'll notice that a few sentences ago I said "small, concise pieces".  A light bulb may have gone off in your head thinking "Single Responsibility Principle", and if it did, bonus points for you.

The key to code reuse is to build your components in a very focused way.  By doing so, you can add functionality by combining different pieces of you application.

Consider the following code used to authenticate a user into a site:

   1: protected void btnSubmit_Click(object sender, EventArgs e)
   2: {
   3:     string Username = Server.HtmlEncode(txtUsername.Text);
   4:     string Password = Server.HtmlEncode(txtPassword.Text);
   5:     int nUserID;
   6:  
   7:     if (Username != "" && Password != "")
   8:     {
   9:         using (SqlConnection cnn = new SqlConnection(connectionString))
  10:         {
  11:             using (SqlCommand cmd = cnn.CreateCommand())
  12:             {
  13:                 cmd.CommandText = "usp_Login";
  14:                 cmd.Parameters.AddWithValue("@Username", Username);
  15:                 cmd.Parameters.AddWithValue("@password", password);
  16:                 
  17:                 using(SqlDataReader reader = cmd.ExecuteReader())
  18:                 {
  19:                     if (reader.Read())
  20:                     {
  21:                         nUserId = Convert.ToInt32(reader[0]);    
  22:                     }
  23:                 }
  24:             }
  25:         }
  26:         
  27:         if (nUserID > 0)
  28:         {
  29:             // if the user has checked the box
  30:             // store his information so he doesn't have to log in again
  31:             if (cbRememberMe.Checked)
  32:                 FormsAuthentication.SetAuthCookie(nUserID.ToString(), true);
  33:             else
  34:                 FormsAuthentication.SetAuthCookie(nUserID.ToString(), false);
  35:  
  36:             BusinessLogicLayer.User objUser = new User(nUserID);
  37:  
  38:             // store session variables for later reference;
  39:             Session["UserID"] = nUserID;
  40:             Session["Username"] = objUser.Username;
  41:             Session["Password"] = objUser.Password;
  42:             Session["AccessLevel"] = objUser.AccessLevel;
  43:             Session["AccessLevelName"] = Enum.GetName(typeof(Utilities.AccessLevel),objUser.AccessLevel);
  44:  
  45:             Response.Redirect("~/admin/admin.aspx");
  46:         }
  47:         else
  48:         {
  49:             View = Views.Failed;
  50:         }
  51:     }
  52: }

While the code above is functional, it does way too much.  In this button click event there are:

  • Call to database explicitly
  • logic to set an authorization cookie
  • Some session variables being set

Imagine for a moment that you now want to implement a new piece of functionality, user impersonation.  In order to accomplish this with the path of least resistance, which developers commonly take, would be to copy/paste all of the login logic to another UI event somewhere else in the site.  The issue copy/paste development is that any future change to the login logic has to be copied again or else the impersonation feature isn't really impersonating a users experience.  The insidious nature of copy/paste development is that while often it works, it creates a brittle application.

In a recent application we were wanting to add user impersonation.  Below is an example of an implementation of user impersonation.  Adding the feature to the application was gleefully simple.  The ease in adding impersonation was due entirely to the proper granularity in our application components.  Seriously, this is all the code it took:

   1: public ActionResult ImpersonateUser(int id)
   2: {
   3:     var authService = Container.Resolve<AuthService>();
   4:     var userRepository = Container.Resolve<UserRepository>();
   5:  
   6:     var userToImpersonate = userRepository.GetById(id);
   7:     if (userToImpersonate != null)
   8:     {
   9:         authService.SignIn(userToImpersonate);
  10:     }
  11:  
  12:     return RedirectToAction("Index", "Home");
  13: }

I did not know we were going to want user impersonation for this application when we started.  However because we developed the site with certain principles in mind, we were able to reuse pieces of code in ways not originally intended for added behavior.  As you can see from the code above, there are no "clever hacks" or tricks to get impersonation to work.  The beauty of the code above is that if we change the implementation of "SignIn(user)" all appropriate areas will follow the same rules since all areas use the same code.

As I'm closing I want to challenge those of you who have read this far to stop yourself the next time you find yourself copying code from one location to another.  Is there an abstraction or component you're overlooking?  Think about the long term and ask if you're really making the wisest decision for the long term health of your application.  It will be through this introspection and honesty that you'll find areas where you can write smaller component which will allow you to realize the oft spoken benefit in OOP of code reuse.


Posted 03-09-2009 1:37 PM by Tim Barcz
Filed under:

[Advertisement]

Comments

DotNetShoutout wrote Real Life - Code Reuse - Tim Barcz
on 03-09-2009 3:36 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Dan Malcolm wrote re: Real Life - Code Reuse
on 03-09-2009 5:14 PM

Your "before" and "after" approach is a great way of demonstrating how DRY/SRP etc can be applied in practice. Liberal calls to Container.Resolve are taken to be quite a serious crime though and you might want to change your sample code before anybody else notices... Ideally, authService and userRepository would be constructor injected into your controller.

Tim Barcz wrote re: Real Life - Code Reuse
on 03-09-2009 5:25 PM

@Dan,

I agree on the constructor injection, however in the example I put it that way so people could see how the object interacted.  If I injected the services through the constructor I would have either had to show the whole class or explain where the two classes were coming from (both choice obfuscate what I'm really wanting to show).

Jak Charlton wrote re: Real Life - Code Reuse
on 03-09-2009 11:16 PM

@Tim

The whole point of constructor or property injection is that you don't need to show the whole class or where they are coming from - you only need to focus on the logic you are trying to show.

Having your method call them would show the principle just fine, who cares how they got there anyway?

Reflective Perspective - Chris Alcock » The Morning Brew #303 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #303
on 03-10-2009 3:03 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #303

Mark Dalgarno wrote re: Real Life - Code Reuse
on 03-10-2009 5:35 AM

The problem with reusing small pieces is that it can become quite hard to manage as the number of reusable things grows. The Software Product Lines community has solved this problem by explicitly modelling the reusable elements and how their interdependencies. This is done by capturing rules about what is common (reusable) in every product and what varies between the different products.

Copy-and-paste programming is a classic anti-pattern for developers who must product lots of program variants but who don't model this variability explicitly.

See www.software-acumen.com/about-product-lines for more about product lines.

John Billy wrote re: Real Life - Code Reuse
on 03-11-2009 8:07 AM

Code reuse has always been a tough subject to debate. I hope we will be able to understand from our mistakes some day,

Bob wrote re: Real Life - Code Reuse
on 03-12-2009 11:36 AM

I'll admit I usually reuse a class from a DLL or cut and pasting code into another project. So you would recommend reusing classes exported from a DLL right?

devlicio.us wrote re: Real Life - Code Reuse
on 05-10-2011 1:43 PM

Real life code reuse.. Keen :)

devlicio.us wrote re: Real Life - Code Reuse
on 06-24-2011 10:53 AM

Real life code reuse.. Dandy :)

Pharma976 wrote re: Real Life - Code Reuse
on 05-02-2012 12:13 PM

Hello! bdkfdbg interesting bdkfdbg site! I'm really like it! Very, very bdkfdbg good!

social bookmarking seo wrote re: Real Life - Code Reuse
on 01-18-2013 12:05 AM

pxGkeM Really informative article. Really Cool.

generic stendra online wrote re: Real Life - Code Reuse
on 01-27-2013 12:02 PM

Af4dnZ I truly appreciate this blog. Really Great.

pills for weight loss wrote re: Real Life - Code Reuse
on 01-31-2013 7:08 PM

fq3kY9 Very informative article.Thanks Again. Will read on...

buy viagra online wrote re: Real Life - Code Reuse
on 02-03-2013 8:05 AM

ln9CYk Major thankies for the article.Much thanks again. Want more.

buy imitrex wrote re: Real Life - Code Reuse
on 02-15-2013 3:14 AM

gpTZvH I think this is a real great blog.Much thanks again. Great.

buy viagra discount wrote re: Real Life - Code Reuse
on 02-23-2013 5:59 PM

ebK5px Im obliged for the blog article.Really thank you! Really Great.

buy discount viagra online wrote re: Real Life - Code Reuse
on 03-02-2013 2:18 PM

uDrQzW I cannot thank you enough for the article post.Thanks Again. Awesome.

bookmarks wrote re: Real Life - Code Reuse
on 03-13-2013 11:19 AM

KIbbFh Hey, thanks for the blog article.Much thanks again. Keep writing.

bookmaring service wrote re: Real Life - Code Reuse
on 03-14-2013 12:22 PM

8RMpni Enjoyed every bit of your article post.Thanks Again. Much obliged.

Social bookmarks wrote re: Real Life - Code Reuse
on 03-23-2013 4:31 PM

lFuXoS I really like and appreciate your post. Great.

stoner wrote re: Real Life - Code Reuse
on 04-06-2013 6:51 AM

Thanks a lot for the blog post.Thanks Again. Much obliged.

stoner wrote re: Real Life - Code Reuse
on 04-06-2013 9:04 AM

Great, thanks for sharing this blog.Really looking forward to read more. Really Great.

social bookmarking service wrote re: Real Life - Code Reuse
on 04-12-2013 3:57 PM

QJaj5E Thank you for your blog.Really thank you! Keep writing.

buy social bookmarks wrote re: Real Life - Code Reuse
on 04-16-2013 5:19 AM

XEDDYa Very good post.Really looking forward to read more. Cool.

buy social bookmarks wrote re: Real Life - Code Reuse
on 04-19-2013 5:39 PM

wJrLaO Thanks so much for the blog post.Really looking forward to read more. Fantastic.

social bookmarking service wrote re: Real Life - Code Reuse
on 04-28-2013 5:11 AM

tVpRFS Thanks-a-mundo for the article post.Much thanks again. Fantastic.

slr lenses wrote re: Real Life - Code Reuse
on 05-14-2013 12:09 AM

QmxUf0 I appreciate you sharing this article. Much obliged.

modular homes upstate NY wrote re: Real Life - Code Reuse
on 06-05-2013 7:10 AM

XoZy3e Thank you ever so for you article post.Thanks Again. Keep writing.

best social bookmarks wrote re: Real Life - Code Reuse
on 06-19-2013 8:47 AM

8RhINi Hey, thanks for the article post.Really thank you!

social bookmarks wrote re: Real Life - Code Reuse
on 06-21-2013 10:13 PM

u5Yyhv Im grateful for the blog article.Really thank you! Keep writing.

great service wrote re: Real Life - Code Reuse
on 07-04-2013 8:37 AM

eWJg7w Hey, thanks for the article post.Much thanks again. Really Great.

cool news wrote re: Real Life - Code Reuse
on 07-08-2013 8:00 PM

I2yZnm Say, you got a nice blog.Really looking forward to read more. Much obliged.

moldova news wrote re: Real Life - Code Reuse
on 07-13-2013 5:35 AM

z6Mduo Enjoyed every bit of your article post. Really Cool.

buy cialis online cheap wrote re: Real Life - Code Reuse
on 07-25-2013 6:24 AM

Im obliged for the blog.Really thank you! Awesome.

best news on earth wrote re: Real Life - Code Reuse
on 07-26-2013 5:55 AM

2iTleV Major thanks for the blog.Really looking forward to read more. Keep writing.

social bookmarks wrote re: Real Life - Code Reuse
on 07-28-2013 11:47 PM

gCtuyI Great blog. Much obliged.

greatest news wrote re: Real Life - Code Reuse
on 08-03-2013 4:47 AM

RLcIrr Say, you got a nice article.Really looking forward to read more.

amazing news wrote re: Real Life - Code Reuse
on 08-03-2013 1:38 PM

e8HoVw Wow, great article post.Thanks Again.

great link buildng wrote re: Real Life - Code Reuse
on 08-19-2013 11:00 AM

joVhaA Really informative blog.Thanks Again. Will read on...

awesome links for you wrote re: Real Life - Code Reuse
on 08-19-2013 11:12 PM

CdFEqe Very good article.Much thanks again. Really Great.

awesome links for you wrote re: Real Life - Code Reuse
on 08-21-2013 5:04 PM

OT26cF Thanks-a-mundo for the blog post.Really thank you! Much obliged.

great seo service wrote re: Real Life - Code Reuse
on 09-04-2013 2:23 AM

vBpGqJ Thanks-a-mundo for the blog post.Really looking forward to read more. Keep writing.

seo service wrote re: Real Life - Code Reuse
on 09-07-2013 9:47 PM

abFdKb I really enjoy the article post.Really looking forward to read more. Really Great.

make money online wrote re: Real Life - Code Reuse
on 09-11-2013 6:32 PM

5ByuT9 Very neat post.Really thank you! Awesome.

only for 5 dollars wrote re: Real Life - Code Reuse
on 09-12-2013 1:23 PM

Tou2QE Thanks so much for the post.Thanks Again. Will read on...

cheap link building wrote re: Real Life - Code Reuse
on 09-24-2013 11:40 AM

HRM0Y4 Very neat blog.Really thank you! Will read on...

cheap seo work wrote re: Real Life - Code Reuse
on 09-29-2013 12:05 PM

ylZC6P Muchos Gracias for your blog.Thanks Again. Great.

link building team wrote re: Real Life - Code Reuse
on 10-01-2013 11:05 AM

LBtxED Hey, thanks for the post.Thanks Again. Much obliged.

best link build wrote re: Real Life - Code Reuse
on 10-16-2013 2:16 AM

P0INrB I really liked your article.Thanks Again. Really Cool.

smashing top seo wrote re: Real Life - Code Reuse
on 10-23-2013 12:31 PM

Nn5T22 Great, thanks for sharing this blog article.Much thanks again. Cool.

smashing top seo wrote re: Real Life - Code Reuse
on 11-02-2013 7:35 AM

odkmwV I loved your article.Much thanks again. Awesome.

watch this wrote re: Real Life - Code Reuse
on 11-18-2013 3:54 AM

jC4IKM Thank you ever so for you blog.Thanks Again. Fantastic.

awesome site wrote re: Real Life - Code Reuse
on 11-20-2013 9:44 PM

zmxOqi Very informative blog article. Great.

check this out wrote re: Real Life - Code Reuse
on 12-15-2013 8:29 AM

W5Z9B8 Enjoyed every bit of your post.Thanks Again. Great.

patrik wrote re: Real Life - Code Reuse
on 03-07-2014 9:10 PM
stunning service wrote re: Real Life - Code Reuse
on 04-05-2014 3:33 AM

YiDwml I really like and appreciate your blog.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)