June 2008 - Posts - Peter Ritchie's MVP Blog

June 2008 - Posts

Comparing CodeRush Refactor! Pro and Resharper 4, part 1 or N -- first glance.

Metadata view of code in referenced assemblies
This is a big one for me.  For whatever reason, Refactor 4 (and prior) completely disables this and sends you to the Object Browser instead.   You get metadata view with CodeRush Refactor! Pro. 

Keyboard layout
As you might imagine, CodeRush Refactor! Pro and Resharper had completely different keyboard layouts.  So, if you're used to R# then Refactor! Pro will take a bit of getting used to.

One thing I don't like with the default Refactor! Pro keyboard layout, is they've replaced Ctrl-. with Quick Navigation.  I use Ctrl-. (instead of Shift-Alt-F10 to get at the smart-tag menu).  Shift-Alt-F10 for smart-tag menu seems more common; so I'll have to get use to Shift-Alt-F10.

Refactor! Pro's Editor enhancements
R# doesn't change to how the editor looks as much as CodeRush Refactor! Pro.  It has the Marker Bar, error/warning/info colouring, and action light-bulbs.  Refactor! Pro with CodeRush has many more, like region painting, flow-break evaluation, Visibility Icons, structural highlighting, etc.

Real-time analysis
Both R# and CodeRush Refactor! Pro do real-time code analysis.  R# has text colouring and the right-hand marker bar.  CodeRush Refactor Pro has a marker bar on both the left and the right.  The left marker bar in CodeRush Refactor! Pro highlights issues for each line, and the right (as does R#) shows a file-wide view of issues.  I find the R# marker bar sometimes doesn't align with the scroll bar so that an issue may be beside the scroll-bar in the marker bar but is actually scrolled off the screen.  CodeRush Refactor Pro doesn't do that and in fact shows the visible portion of the document in its marker bar.  i.e. the marker bar is beside the Visual Studio Indicator Margin (where the breakpoint bullets appear).

Refactorings
They both have many of the same refactorings.  When it comes to non-refactorings the two sometimes approach things differently.  For example R# deals with adding members to a class through "Generate Code" list; whereas CodeRush Refactor Pro uses templates.  E.g. in R#, to add a constructor you hit Alt-Ins and select Constructor.  With Refactor you simply enter "cc "(that 'c', 'c', space).

Smart-Tags
Both approach smart-tags slightly differently.  With R# they use the actions light-bulb at the left margin.  This includes fixes for warnings, errors, plus refactorings, but doesn't include code generation options.  CodeRush Refactor Pro uses an ellipsis-like icon that appears right at the site of the potential refactoring. Code Refactor Pro is a richer experience.  It will show you the result of a refactoring with arrows showing where things will move, red strike-out showing what will be removed, and highlights for things that will be changed/renamed.

Entity Framework Petition of Vote of Non Confidence

I had intended to be happy simply being a signatory of ADO .NET Entity Framework Vote of No Confidence.  But, there's people suggesting signatories of this petition are wackos or on the fringe.

Do yourself a favour and read the petition.  Read what we have issues with and how we think Entity Framework (EF) can be improved to be a better product.  Read seminal material by industry leaders on entity-oriented and object-oriented application development like Domain Driven Design, and Agile Principles, Patterns and Practices in C# (and the predecessor Agile Software Development: Principles, Patterns and Practices) and Applying Domain-Driven Design and Patterns: With Examples in C# and .NET.  If after you have understand this information, you think being forced to use EF on a software development team would mean it would be hard to implement generally accepted object-oriented and entity-oriented design, the sign the petition.

Yes, you have the choice no to use EF; but you also have the choice to help people to not have to deal with the issues detailed in the petition simply because they were not made aware of them.

And despite the naysayers of people who think EF should improve, the ADO.NET team is listening and agrees.

kick it on DotNetKicks.com

Fundamentals of OOD Part 3: Method Cohesion

Single Responsibility Principle (SRP) helps us write more cohesive types and methods.  Cohesion is the relatedness of the members of a type to each other and the relatedness parts of a method's code to other parts.

Method cohesion
Often times a method is not very cohesive, meaning the code that it executes relates to more than one thing.  This can often be seen with a method that contains a large switch statement.  For any invocation of the method only one case statement may be executed; meaning that blocks of code within the method don't relate to all the other blocks.  Switch statements are often an indication that the design should be changed to be more polymorphic or introduce a pattern like the Strategy or Template Method patterns.  Likely a concept of the design is implicit instead of explicit (at least no more explicit than an enum).  For example:

    public class Set {

        public enum Operation {

            Unknown,

            Union,

            Intersection,

            RelativeComplement,

            And,

            Conjunction = And

        }

 

        public static IList<int> PerformOperation(IList<int> one, IList<int> two, Operation operation) {

            List<int> result = new List<int>();

            switch (operation) {

                case Operation.Union:

                    result.AddRange(one);

                    result.AddRange(two);

                    break;

                case Operation.Intersection:

                    foreach (int x in one) {

                        if (two.Contains(x)) {

                            result.Add(x);

                        }

                    }

                    break;

                case Operation.RelativeComplement:

                    foreach (int x in one) {

                        if (!two.Contains(x)) {

                            result.Add(x);

                        }

                    }

                    break;

                case Operation.And:

                    foreach (int x in one) {

                        if (two.Contains(x)) {

                            result.Add(x);

                        }

                    }

                    break;

                default:

                    throw new ArgumentOutOfRangeException("operation");

            }

            return result;

        }

    }

This code works; but it's far from cohesive.  There are many combinations of execution paths this method can take, and each path is unrelated to the other paths.  For example, the Union case has no relation to any of the other cases.  Methods like this are also hard to maintain and prone to errors.  Obviously if another member were added to Operation PerformOperation would have to change--making PerformOperation tightly coupled to Operation--not very object oriented.

This can be made more object-oriented by through Dependency Inversion and the Strategy Pattern:

    public class Set2 {

        public abstract class Operation {

            public abstract IList<int> Execute(IList<int> left, IList<int> right);

        }

 

        public class UnionOperation : Operation {

            public override IList<int> Execute(IList<int> left, IList<int> right) {

                List<int> result = new List<int>();

                result.AddRange(left);

                result.AddRange(right);

                return result;

            }

        }

 

        public class IntersectionOperation : Operation {

            public override IList<int> Execute(IList<int> left, IList<int> right) {

                List<int> result = new List<int>();

                foreach (int x in left) {

                    if (right.Contains(x)) {

                        result.Add(x);

                    }

                }

                return result;

            }

        }

 

        public class RelativeComplementOperation : Operation {

            public override IList<int> Execute(IList<int> left, IList<int> right) {

                List<int> result = new List<int>();

                foreach (int x in left) {

                    if (!right.Contains(x)) {

                        result.Add(x);

                    }

                }

                return result;

            }

        }

 

        public class AndOperation : Operation {

            public override IList<int> Execute(IList<int> left, IList<int> right) {

                List<int> result = new List<int>();

                foreach (int x in left) {

                    if (right.Contains(x)) {

                        result.Add(x);

                    }

                }

                return result;

            }

        }

        public static IList<int> PerformOperation(IList<int> left, IList<int> right, Operation operation) {

            return operation.Execute(left, right);

        }

    }

Now each operation is encapsulated, explicit, PerformOperation need not change as new strategies are added, and we've completely avoided the InvalidOperationException.

kick it on DotNetKicks.com

How Hard Could It Be Indeed: Another sub-standard web site.

Yet another sub-standard website.  I was reading a new Joel Spolsky post today that was a teaser for an article he wrote in Inc. Magazine.  It sounded interesting so I decide to have a read.

 You can imagine my disappointment when this is what I was presented with:

How hard could it be indeed...

Posted by PeterRitchie | with no comments
Filed under: