14 Replies - 3091 Views - Last Post: 28 August 2012 - 02:10 PM Rate Topic: -----

#1 jaredmalcolm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 18-August 12

C# Code Smells in Relation to Design Patterns

Posted 18 August 2012 - 03:13 PM

I've been spending some time recently trying to learn different design patterns to help improve my coding. One thing that I have an issue with is knowing WHEN to use different design patterns and I've had a hard time finding documentation that helps to show the "signs" of when a design pattern should be used.

I've been searching online for "code smells" and unfortunately most of the answers I've found are in relation to "bad" code smells. I'm more interested in code smells that signal you to look at possibly implementing a design pattern/interface/etc. I'm not looking for what seems to be the common answers of god objects/big (class/method/etc)/multiple classes in a single file/etc.

So basically I'm hoping to find code examples that SCREAM create an create an interface/implement the Observer Pattern/etc.

Is This A Good Question/Topic? 2
  • +

Replies To: C# Code Smells in Relation to Design Patterns

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3161
  • View blog
  • Posts: 9,540
  • Joined: 05-May 12

Re: C# Code Smells in Relation to Design Patterns

Posted 18 August 2012 - 04:12 PM

*
POPULAR

If you have a big switch statement that keys on the type of an object, then you may want to apply the Strategy pattern.

If you have a big chain of if statements trying to find an object or function to handle an event or input type, then you may want to apply the Chain of Responsibility pattern.

If you find that you have a lot of duplicate code that traverses a collection, but the only thing different with each copy is what it does with each item, then you may want to apply the Visitor pattern.

Spoiler

This post has been edited by Skydiver: 18 August 2012 - 04:13 PM

Was This Post Helpful? 5
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 04:43 AM

There are a lot of design patterns out there. In order to present every scenario in which to apply all of the different patterns would require a book (or several books) to be written. Speaking of which there are plenty of good books out there on design patterns. I recently purchased Professional ASP.NET Design Patterns, which I'd suggest to anyone wanting to learn about using design patterns with ASP.NET.

Learning when and where to properly apply design patterns is probably the hardest part of learning about design patterns and can only really be learned through experience. If you work with anyone that knows design patterns you might ask them to look over your code and give you some suggestions. If not, you can always come here and describe what you're trying to accomplish and ask if there is a design pattern that might make it easier for you accomplish and I'm sure there's plenty of people that could point you in the right direction.
Was This Post Helpful? 1
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 05:11 AM

But anyway, here's another example. If you find that you have several classes in which the only thing that varies is a specific algorithm or the results from a specific function then you might have a candidate for the template pattern.

For instance I have a project now where I have to create an output file with about 30 fields. The values for the output file are determined by the billing system selected by the user. Apart from that output file each billing system can be handled the same by the code. So I created a base abstract class as a template that requires a method be overridden that creates the file. Then I create a child class for each billing system that implements the template base class. The code that handles the request to create the file takes the base class as a parameter. This means it can also accept any of the child classes and the code will function in the same way because they all override the method that creates the file. This also has the added benefit of allowing me to add additional billing systems in the future without having to update any of the code that uses these classes.
Was This Post Helpful? 3
  • +
  • -

#5 jaredmalcolm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 18-August 12

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 01:52 PM

View PostNakor, on 20 August 2012 - 04:43 AM, said:

Learning when and where to properly apply design patterns is probably the hardest part of learning about design patterns and can only really be learned through experience.


This is what I'm trying to have a reference (so to speak). I have a couple books on design patters as well as a pluralsight membership, but I was trying to find the real life scenarios that people see and say THIS pattern might work better. I have the unfortunate situation of being the developer that works on the "new" software. My boss does most of the "old" or mainframe work.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8366
  • View blog
  • Posts: 31,089
  • Joined: 12-June 08

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 01:56 PM

Quote

I have the unfortunate situation of being the developer that works on the "new" software. My boss does most of the "old" or mainframe work.

That's where it's the most fun - on the edge of new and what's green!
Was This Post Helpful? 3
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 02:06 PM

I'll take working on new applications over maintaining legacy code any day.
Was This Post Helpful? 3
  • +
  • -

#8 jaredmalcolm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 18-August 12

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 02:28 PM

View PostNakor, on 20 August 2012 - 02:06 PM, said:

I'll take working on new applications over maintaining legacy code any day.


Don't get me wrong I love what I do and the situation I'm in. However, it would be nice to have someone that is more knowledgeable then I am to reference and learn from.
Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 02:38 PM

Yeah, I'm sure that would help. I'm kind of in the same boat at where I work. They had just recently switched to asp.net and C# before I was brought onto the team so no one really had any experience in it besides me and that was just what I had learned in school. I'd say 90% of what I know now has been self taught through books and experience over the last two years.
Was This Post Helpful? 0
  • +
  • -

#10 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 03:31 PM

My advice to anyone who is stepping into more thoughtful development by learning design patterns:

Start with major patterns and then advance into minor ones.

These are purely my own categorizing, some of you probably think that there are no minor or major design patterns. But by "major patterns" I mean those which are meant to built your whole application architecture. Nowadays in C# development there seems to be three patterns to go with, depending on your platform: N-tier architecture for WinForms, MVC for ASP.NET, and MVVM for XAML-based development (WPF, SL, WP, Metro).

The minor patterns are the ones which affect single part of the software, like how you access data (repository pattern for example) or how some class is accessibly (singleton).

If you teach yourself MVVM, you are able to create nicely structured software in any of those platforms. It will be a great start and you'll come up with more maintainable software even if you don't use any other design patterns. But even if you don't, at some point you'll start thinking more about data access and come up with some pattern, like repository pattern.
Was This Post Helpful? 1
  • +
  • -

#11 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 09:34 PM

A recent pattern I've begun playing with is MVP when working with webforms. Model-View-Presenter is an adaptation of the MVC pattern and is a nice way of removing logic from the UI layer and for making your code more testable.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3161
  • View blog
  • Posts: 9,540
  • Joined: 05-May 12

Re: C# Code Smells in Relation to Design Patterns

Posted 20 August 2012 - 10:56 PM

jaredmalcolm, if you want a quick exercise, take a look a this thread in the C++ forum and think about what pattern(s) is (are) present and what other pattern(s) you would apply.
Was This Post Helpful? 0
  • +
  • -

#13 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 27 August 2012 - 08:50 PM

Thought of another one. If you're attempting to fetch a specific item from a database but there is a chance that object may not be found you quite often have to add code to check for null values. You usually have to add this check in many places which creates a code smell because now you're performing the same work in multiple places. One option that can remove this code smell is to use the Null Object pattern.

Good programming says that it is better to code to the interface rather than the implementation. This means that your code should be looking for instances of a class that implements a specific interface or abstract class. So let's say we have a Student class that implements an IStudent interface. We would also create a NullStudent class that implements IStudent and sets all of its values do their default values.

public interface IStudent
{
    int StudentID {get;set;}
    string FirstName {get;set;}
    string LastName {get;set;}
}

public class Student : IStudent
{
    public int StudentID {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
}

public class NullStudent : IStudent
{
    public int StudentID { get { return 0; } set { // do nothing } }
    public string FirstName { get { return string.Empty; } set { // do nothing } }
    public string LastName { get { return string.Empty; } set { // do nothing } }
}



Now, however you fetch your Student from the database, you check if the database returned any data, if so you build your Student object and return it, if not you instead create a NullStudent and return that.

I generally use Entity Framework and linq, so my code might look like this.

public class StudentRepository
{
    public IStudent GetStudentByID(int id)
    {
        return context.Students.FirstOrDefault(s => s.StudentID == id) ?? new NullStudent();
    }
}



Then anywhere else in my code when I need to get a student object

StudentRepository repository = new StudentRepository();
IStudent student = repository.GetStudentByID(someID);



From that point I no longer have to check if the student object is null because it never will be. And since my code is working with the IStudent interface it will work the same whether an actual Student is returned or the NullStudent is returned.

This post has been edited by Nakor: 27 August 2012 - 08:53 PM

Was This Post Helpful? 1
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3161
  • View blog
  • Posts: 9,540
  • Joined: 05-May 12

Re: C# Code Smells in Relation to Design Patterns

Posted 28 August 2012 - 10:55 AM

Beware of the Null Object pattern when dealing with collections, though.

Picking up from the example above, you could have something like:
var repository = new StudentRepository();
IList<Student> officers = new List<Student>();
officers.Add(repository.GetStudentById(presidentID));
officers.Add(repository.GetStudentById(secretaryID));

// How many officers do I have?
int officerCount = officers.Count;



Do you really have 2 officers, 1 officer and a null, or 2 nulls?
Was This Post Helpful? 0
  • +
  • -

#15 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: C# Code Smells in Relation to Design Patterns

Posted 28 August 2012 - 02:10 PM

right, just like most patterns you have to know where to use it and what gotchas might be associated with it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1