refactoring

Pulling Up Interface Implementation into base class

I'm using a library which requires my views to implement an interface, which is only a dependency property and get\set accessor for it. The only difference is the OwnerType in the DP's Register method. AFAIK, duplicate code is bad, and I've forgotten to change OwnerType after pasting several times now :) So I figure I should try and move...

How do I refactor this to reduce the number of java classes needed?

I have the following two classes and I am starting to see a pattern that even with my little Java background is screaming for a fix. Every new Object is going to require a set of Actions and the number of classes could grow out of hand. How do I refactor this into a generic DeleteAction class? I know some of the answers will be use H...

Refactor my C# code : if-else statement and code repetition.

Given: private void UpdataFieldItems(List<FieldItemBase> existingFieldItems, List<FieldItemBase> selectedFieldItems) { List<FieldItemBase> newFieldItemsSelected; var fieldSelectionChanges = GetFieldSelectionChanges(out newFieldItemsSelected);//retuns a Flagged enum if (Coding.EnumHas(fieldSelectionChanges, F...

How to refactor conditional logic in javascript?

I inherited a largish JavaScript (MooTools) application for browsing and configuring products. In it there's a function that tracks the products' configured state. Other functions are called depending on what state the product is in. I extended it to add some new features, but have ended up with a mess of nested conditionals inside a b...

Avoiding repetition when using callback functions and asynchronous requests

I am writing some Javascript/jQuery code that requires me to make lots of requests to various APIs. I have come across this problem many times and this is just a basic example of one instance of it. The code makes an asynchronous request to a server and then if a condition with the returned data is not met, the code makes another reque...

How to perform 'move field' refactoring on active-record models

This is a fairly common refactoring, Martin Fowler calls it 'move field'. Given 3 models: class Person < ActiveRecord::Base has_one :contact_details has_one :address end class ContactDetails < ActiveRecord::Base end class Address < ActiveRecord::Base end how do I refactor, including migration, the has_one address from Person to ...

What are the best practices for preventing SQL creep?

I have a webapp written in PHP using a MySQL database backend. This question could just as easily apply to any language and application trying to use an SQL database and MVC OOP design. How do you keep your SQL code restricted to the Model? There's a rather longer story specific to my case behind the question. As previously mentioned ...

Code refactoring

I am about to starting to work on a project which involves refactoring and modifying existing code which is in c & c++. The code is a bloated one and is in huge volume. Ofcourse since the code needs to be modified an understanding of the code has to be developed and in a very short span of time since we have some pretty time pressed proj...

Identifying repeated code within PHP project

I have a single PHP file within a legacy project that is at least a few thousand lines long. It is predominantly separated up into a number of different conditional blocks by a switch statement with about 10 cases. Within each case there is what appears to be a very similar - if not exact duplicate - block of code. What methods are avail...

"Cosmetic" clean-up of old, unknown code. Which steps, which order? How invasive?

When I receive code I have not seen before to refactor it into some sane state, I normally fix "cosmetic" things (like converting StringTokenizers to String#split(), replacing pre-1.2 collections by newer collections, making fields final, converting C-style arrays to Java-style arrays, ...) while reading the source code I have to get fa...

Rails: any way to refactor this ActiveRecord code?

I have a piece of code that checks that a survey response picked by user to a survey question is in fact one of valid choices: Question.find_by_id(question_id).question_choices.all(:select => 'id').map {|x| x.id}.include?(user_choice_id) Is there an easier way? Thanks! ...

Refactoring in eclipse: how to add field,getter,setter to all implementations of an interface?

Hi, how can I add a field, getter and setter to all implementations of MyInterface (in the current project or folder?) ...

Refactoring of model for testing purpose

Hi All, I want to refactor my model so I can properly write a unit test for it. But I have some dependencies. Can anybody point me in the right direction how I can remove these dependencies? class Customer { public function save(array $data, $id = NULL) { // create or update if (empty($id)) { $custom...

Challenges of refactoring unit-tests to be maintainable and readable when dealing with List<T> objects

In the book The Art of Unit Testing it talks about wanting to create maintainable and readable unit tests. Around page 204 it mentions that one should try to avoid multiple asserts in one test and, perhaps compare objects with an overridden Equals method. This works great when we have only one object to compare the expected vs. actual re...

why doesn't my refactored ruby using inject work?

I tried to do some refactoring to convert an each block into an inject, but it didn't work and I don't understand why. Here's the code that works before refactoring: class String # Build the word profile for the given word. The word profile is an array of # 26 integers -- each integer is a count of the number of times each letter ...

Partial Mocking As Code Smell?

Why is there so much hating going on about 'partial mocking' and the code that requires it? Here's an (theoretical) example implementation: public ComplexResult1 operationA(Stimulus a) { { ... result = ...; } auditTheChange(a); } public ComplexResult2 operationB(Stimulus b) { { ... result...

How to approach a function that has slightly different behavior when called the first time?

This is a very general programming question that I just want to put out there and see if anyone has any thoughts on it. It shows up time and time again: I have a function somefunction() which is called a lot, but the very first time its called I want it to behave just a bit differently. Here's an example: first_time = true; somefunction...

Which book should I buy? Working Effectively with Legacy Code or Refactoring: Improving the Design of Existing Code?

I am working with a few programs in PHP 4, ASP classic and C# (.NET 2). They were all created by different people with different styles. None of them have any unit tests and there are very few comments. I would like to be able to effectively add new features to the code while refactoring small parts along the way and I am wondering which...

How to protect yourself when refactoring non-regression tests?

Are there specific techniques to consider when refactoring the non-regression tests? The code is usually pretty simple, but it's obviously not included into the safety net of a test suite... When building a non-regression test, I first ensure that it really exhibits the issue that I want to correct, of course. But if I come back later...

How to avoid false positives using a mockist approach in unit tests?

Since the datastructure of my application domain is becoming pretty complex as of late, I started reading up on mock objects. Soon a simple question came to my mind, but the answer has proven to be quite the headache so far. So here goes: We have a class 'Foo' with 'bar' as one of its methods: class Foo { public String bar(int i){ ...