refactoring

How to set default values to all wrong or null parameters of method?

At the moment I have this code (and I don't like it): private RenderedImage private RenderedImage getChartImage (GanttChartModel model, String title, Integer width, Integer height, String xAxisLabel, String yAxisLabel, Bool...

So, I have this jquery bit that adds a row to a table the way I need it to, but it's UGLY

I have a table that looks like this: <table name="exercises" id="workout-table"> <tr> <th>Name</th> <th>Reps/Intervals</th> <th>Sets</th> <th>Weight/Distance/Time</th> </tr> [%- i=0 %] [% WHILE i<=10 %] <tr class="workout-rows"> <td><input type="text" name="workout[exercise][[% i %]][name]" /></td> <td><input type="text" name="workou...

PHP Array logic re-factor

PHP Array logic re-factor I'm trying to re-factor my code... This is PHP ... I have the following: $totals[] = "Total"; $totals[] = $counts['hole'][1] + $counts['warn'][1] + $counts['info'][1] + $counts['crit'][1]; $totals[] = $counts['hole'][2] + $counts['warn'][2] + $counts['info'][2] + $counts['crit'][2]; $totals[] = $counts['hole'...

Why Does This Maintainability Index Increase?

I would be appreciative if someone could explain to me the difference between the following two pieces of code in terms of Visual Studio's Code Metrics rules. Why does the Maintainability Index increase slightly if I don't encapsulate everything within using ( )? Sample 1 (MI score of 71) public static String Sha1(String plainText) { ...

How to pull a method out of it's class (and into a new or exsisting)?

What is the easiest way of pulling an excisting method out of it's class and into a new class using Visual studio 2010 / Resharper? Edit: I use Resharper version 5. ...

F# replace ref variable with something fun

I have the following F# function which makes use of a ref variable to seed and keep track of a running total, something tells me this isn't in the spirit of fp or even particular clear on its own. I'd like some direction on the clearest (possible fp, but if an imperative approach is clearer I'd be open to that) way to express this in F#...

How to Elegantly convert switch+enum with polymorphism

I'm trying to replace simple enums with type classes.. that is, one class derived from a base for each type. So for example instead of: enum E_BASE { EB_ALPHA, EB_BRAVO }; E_BASE message = someMessage(); switch (message) { case EB_ALPHA: applyAlpha(); case EB_BRAVO: applyBravo(); } I want to do this: Base* message = someMessage...

Any way to surround code block with Curly Braces {} in VS2008?

I always find myself needing to enclose a block of code in curly braces { }, but unfortunately that isn't included in the C# surround code snippets, which seems to be an oversight. I couldn't find anything on building your own surround snippets either (just other kinds of snippets). I am actually running Resharper too, but it doesn't...

How do you remove functionality from a program in ruby?

You have some code you want to remove associated with an obsolete piece of functionality from a ruby project. How do ensure that you get rid of all of the code? Some guidelines that usually help in refactoring ruby apply, but there are added challenges because having code that isn't being called by anything won't break any unit tests. ...

Refactoring a complicated if-condition

Hi all, Can anyone suggest best way to avoid most if conditions? I have below code, I want avoid most of cases if conditions, how to do it ? any solution is great help; if (adjustment.adjustmentAccount.isIncrease) { if (adjustment.increaseVATLine) { if (adjustment.vatItem.isSalesType) { entry2.setDebit(adjustmen...

Factoring out conditional from Rails view

I have a line of code in a basic Rails blog application I did that determines whether the suffix to the number of comments should be "comment" (if 1) or "comments" (0 or 2+) Currently, it is a line in the view looking like this: <%= post.comments.count() %> <%= post.comments.count() == 1 ? "comment" : "comments" -%> However, sinc...

c# 4.0 - best way to refactor a block of "If (something is Type) {}" statements?

I've got some code that looks like this, public void ResetControls(Control controlOnPage) { if (controlOnPage is TextBox) { ResetTextBoxControl(controlOnPage); } if (controlOnPage is MediaPicker) { ((MediaPicker)controlOnPage).Media = null; } if (controlOnPage is RelatedContentPicker) { ...

Is there a better way to write this repetitive event-declaration code in C# when implementing an interface explicitly?

I have a lot of code like the following, where I explicitly implement some events required by an interface. public class IMicrowaveNotifier { event EventHandler<EventArgs> DoorClosed; event EventHandler<EventArgs> LightbulbOn; // ... } public class Microwave : IMicrowaveNotifier { private EventHandler<EventArgs> _doorClosed; ...

How do I unit test the methods in a method object?

I've performed the "Replace Method with Method Object" refactoring described by Beck. Now, I have a class with a "run()" method and a bunch of member functions that decompose the computation into smaller units. How do I test those member functions? My first idea is that my unit tests be basically copies of the "run()" method (with diff...

How can i make this code, more Ruby-esque?

unless scope.nil? @page ||= Page.find(id, :scope => Page.find(scope) ) else @page ||= Page.find(id) end ...

[Delphi] How would you refactor this code?

This hypothetical example illustrates several problems I can't seem to get past, even though I keep trying!! ... Suppose the original code is a long event handler, coded in the UI, triggered when a user clicks a cell in a grid. Expressed as pseudocode it's: if Condition1=true then begin //loop through every cell in row, //if aCell/...

Replace conditional with polymorphism refactoring or similar?

I have tried to ask a variant of this question before. I got some helpful answers, but still nothing that felt quite right to me. It seems to me this shouldn't really be that hard a nut to crack, but I'm not able to find an elegant simple solution. (Here's my previous post, but please try to look at the problem stated here as procedural ...

Proactively using 'lines of code' (LOC) metric in your software-development process?

hi there, Codebase size has a lot to do with complexity of a software system (the higher the size the higher the costs for maintenance and extensions). A way to map codebase size is the simple 'lines of code (LOC)' metric (see also blog-entry 'implications of codebase-size'). I wondered how many of you out there are using this metric a...

How can I refactor these script tags?

I have the following script tags in the <head> so that they don't prompt any security errors when going back and forth between SSL and non-SSL pages. But it just looks hairy. Any way I can combine them or reduce some of the code? <script type="text/javascript">document.write(["\<script src='",("https:" == document.location.protocol) ? ...

Can I improve performance by refactoring SQL commands into C# classes?

Currently, my entire website does updating from SQL parameterized queries. It works, we've had no problems with it, but it can occasionally be very slow. I was wondering if it makes sense to refactor some of these SQL commands into classes so that we would not have to hit the database so often. I understand hitting the database is gen...