refactoring

How to refactor on a budget

We have a C++ MFC application (SLOC is ~200000 if it matters). It was written very quickly during the dot-com era with little thought given to extensibility or scalability. In the intervening time, it's become a mission critical product that we distribute to customers. Right now, we have two great developers maintaining the codebase, b...

Extracting an Event (tabControl_DrawItem) to Class Library

I have the following code under a TabConttrols DrawItem event that I am trying to extract into a class file. I am having trouble since it is tied to an event. Any hints or pointers would be greatly appreciated. private void tabCaseNotes_DrawItem(object sender, DrawItemEventArgs e) { TabPage currentTab = tabCaseNot...

ASP.NET MVC Views don't see any classes after massive refactoring

My project went through a name change, which led to using ReSharper to change the name of namespaces throughout the solution. Everything compiles just fine, but my ASP.NET MVC views are no longer seeing any inherited classes. I've changed the namespace imports in web.config and everything, and I'm certain that the classes exist. They wor...

How do I refactor chained methods?

Starting with this code: new Person("ET").WithAge(88) How can it be refactored to: new Person("ET", 88) What sequence of refactorings needs to be performed to complete the transformation? Why? Because there could be hundreds of these, and I wouldn't want to introduce errors by doing it manually. Would you say a drawback with f...

Tools for keeping unit tests organised and named correctly

I have been trying to clean up the naming and organisation conventions of our projects' unit and integration tests. We are using C#, NUnit, Visual Studio and Resharper. It seems that if there is one best practice for unit test organisation it is that the layout and naming of the test classes and namespaces should mirror those of the cod...

How do I refactor a WinForms application?

I'm going to bugfix a WinForms application (.NET 2.0) in the near future. Looking through the source code I find large code files (more than 2000 lines) most of them are generated dialogs with lots of code-behind. Has anyone tips for me to share? Any war stories or best-practices for bug fixing or refactoring WinForms applications? ...

How do you decide which parts of the code shall be consolidated/refactored next?

Do you use any metrics to make a decision which parts of the code (classes, modules, libraries) shall be consolidated or refactored next? ...

How can I improve this Rails code?

I'm writing a little browser game as a project to learn RoR with and I'm quite new to it. This is a little method that's called regularly by a cronjob. I'm guessing there should be some way of adding elements to the potions array and then doing a bulk save at the end, I'm also not liking hitting the db each time in the loop to get the ...

How do I refactor multiple similar methods of this sort into a single method?

Suppose I have a number of related classes that all have a method like this: protected override OnExecute() { this.constructorParm.BoolProperty = !this.constructorParm.BoolProperty; } The type of constructorParm may change (it may also be a static Setting), and the specific property will certainly change, but the type of the prope...

Do you find cyclomatic complexity a useful measure?

I've been playing around with measuring the cyclomatic complexity of a big code base. Cyclomatic complexity is the number of linearly independent paths through a program's source code and there are lots of free tools for your language of choice. The results are interesting but not surprising. That is, the parts I know to be the hairi...

Refactoring method that binds controls to LINQ

I have a function that binds LINQ results to the controls on my form. The below code works but I just can't get over the feeling I should be slapped for the copy/paste aspect. Can someone help me with what i need to do to deodorize this? Thank You! private void BindDataToForm() { // Bind data to form CaseNotesData...

C style logic and refactoring

I love languages that evaluate a single expression both as a value and as a boolean value. For example A = 1 evaluates to true, and so does 1. If this practice is very common to the developers in my think tank, is it wrong not to refactor out these expressions, assuming no side effects? I have a long standing discussion at work talkin...

Refactoring WinForm ClickNCode to MVP Passive View

I'm attempting to refactor an existing Winform application to use the MVP Passive View pattern. The application's UI, business logic and data storage code have been freely intermingled for years. It looks like it either started out with separate layers or someone attempted to separate it into layers. In any case the layer boundaries were...

How do I break this down into Unit Tests?

I have a method that is called on an object to perform some business logic and add it to the database. The object is a Transaction, and part of the business logic requires searching the databses for related accounts and history items on the account. There are then a series of comparisons and operations that need to bring back informati...

Tool to refactor boolean expressions

I'm looking for a tool to refactor boolean expression. I've got expressions like a1 => (b1 <=> c or d) AND a2 => (b2 <=> c or d) AND a2 => (b2 <=> c or d) The tool should be able to simplify expressions, e.g. extract the sub expression "c or d" in the example above. Is there a free computer algebra system which can do this? Currentl...

API to programmatically refactor Java code

Do you know an API to programmatically refactor Java code. Preferably: easy to use support for rich refactorings ...

Pluses and minuses of using Factories in a Rails test suite?

I'm currently looking at a hefty Rails test suite. It's nothing I can get into specifics about, but the run time for the entire suite (unit/functional/some integration) can run upward of 5 minutes. We're completely reliant on fixtures and are we're not mocking and stubbing as much as we should be. Our next few sprints are going to be ...

Are private methods in general a code smell?

In the sense that a code smell is an indicator of a potential need for refactoring are private methods a code smell? I was looking at some of my own code and it dawned on me that many of my public methods don't actually do anything other than consolidate private methods of the same class. Furthermore, none of the private methods relied ...

How to refactor, fix and optimize this character replacement function in java

While tuning the application, I found this routine that strips XML string of CDATA tags and replaces certain characters with character references so these could be displayed in a HTML page. The routine is less than perfect; it will leave trailing space and will break with StringOutOfBounds exception if there is something wrong with the...

How can I make this Xaml a Template?

Without using code, how can I turn this into a Template? I have about 10 of these, and my code is getting huge. It is working perfectly, I am just looking for ways to clean up the code, I am just not familiar enough with templating and resourcing animations and triggers to do this. Thanks in advance. <RadioButton Width="35" Height="3...