refactor-my-code

how to refactor tricky logic involving consecutive sets?

The rule at work here is that users can be awarded badges for a streak of 10, 20, and 30. But the user can't be awarded multiple badges for the same streak. I'm tracking consec wins in the user model. For example, if the user hits a 10-streak, the user is awarded a 10-streak badge. If the user is on a 20-streak, he/she receives a 20-s...

Refactoring multiple if statements for user authentication with subdomains

...

Help me refactor my World Cup Challenge Script

I am setting up a World Cup Challenge between some friends, and decided to practice my Ruby and write a small script to automate the process. The Problem: 32 World Cup qualifiers split into 4 tiers by their Fifa ranking 8 entries Each entry is assigned 1 random team per tier Winner takes all :-) I wrote something that suffices yet...

How do I select the item with the highest value using LINQ?

Imagine you got a class like this: class Foo { string key; int value; } How would you select the Foo with the highest value from an IEnumeralbe<Foo>? A basic problem is to keep the number of iterations low (i.e. at 1), but that affects readability. After all, the best I could find was something along the lines of this: IEnum...

How can I make this Dictionary TryGetValue code more readable?

I'd like to test if an id was not yet known or, if it is known, if the associated value has changed. I'm currently using code similar to this, but it is hard to understand for those not familiar with the pattern. Can you think of a way to make it more readable while keeping it short in LOC? string id; string actual; string stored; if (...

How to design an API for operating on remote objects with java RMI

I have a remote object exposed through RMI, which I use to perform several things on a resource that the object holds. Right now I have method for each operation I need to perform. Example: public interface IRobotController { public int walk(int meters); public void dance(int seconds); } public interface RMIRobotController imp...

Help refactoring named_scope...

I have the following named_scope: named_scope :commentors, lambda { |*args| { :select => 'users.*, count(*) as total_comments', :joins => :comments, :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :chalkboard_user_id => nil} }, :group => 'users.id', :having => ['coun...

Refactor my code to eliminate separate DB queries

Here's my existing code. I could use some guidance on how to optimize this. Essentially, I don't want to have to run a SQL query more than once -- self.active_item?(r). What I'm thinking I can do instead of making separate DB queries, is retrieve an array of all the user's active inventory items (e.g., self.active_inventory_items), tur...

Slow Powershell function. How to Improve?

I have written the below Powershell function to call an F5 unit. It loops round for each server and gets each individual stat and then executes a SQL SMO call. The code is running really slowly and i think i have discounted the SQL call as the cause. How can the powershell be improved? function Print-VServerStats() { param($virt...

How to refactor this Ruby code aiming to change an hash's symbols into strings?

I just wrote this piece of code but I'm not quite happy about it. data = {} options.each{ |k,v| data.merge!({k.to_s => v}) } Basically I have: {:a => "something", :b => "something else", :c => "blah"} ... and I want ... {"a" => "something", "b" => "something else", "c" => "blah"} ... in order to send it to a gem that do not hand...

C# Refactoring: How would you refactor 2 methods that do the same except that they use a different Interface

Having 2 different interfaces is a must. How would you refactor this? Should I refactor this code at all? private void CreateInstanceForProviderA() { a = FactorySingleton.Instance.CreateInstanceA("A"); if (a == null) { ShowProviderNotInstanciatedMessage(); return; } a.Owner = Handle.ToInt32(); ...

How to get the list of members (properties/methods) that are not used anywhere in the solution?

Hi everyone, I would like to explain my needs by a sample context below. I have a C# class ClassC which does task TaskT. I've just updated ClassC to fullfill TaskT in a second approach but still keep the first/old one intact. Now I finished coding and want to remove all of the old codes related to the first approach. How can I do this i...

Multiple OracleCommands per OracleConnection

I am opening a connection to a database in which I need to issue multiple deletes before closing the connection. I have the following code but it looks strange with all the using statements. Is there a better way to do this? Is this the correct way to use the using statement with connections/commands? using(OracleConnection oracleCon...

Help refactoring this code

Update: the much better answer has little to do with refactoring, but has to do with setting a default for empty keys. See the first answer- thanks a lot guys! Hi folks, how would you refactor this? if n=network_stats["b"] network_stats["b"] = n +1 else network_stats["b"]=1 end I have a nagging feeling this ...

refactor my if statement code

Hello, I've been messing with this bit of code for over an hour trying to rearrange it different ways. Is there any easier way to write it? if x is not Number ;// if x is string { if y is not Number ;// x, y both strings { Eval(x) Eval(y) return } else ...

Rails partial overloaded with logic needs refactor

How would you refactor this logic filled partial? <%- for post in @posts -%> <div class="post"> <%= link_to post.title, post %> <%- if post.name.empty? -%> <%- else -%> <span class="name"> by <%- if post.email.blank? -%> <%= post.name %> <%- else -%> <a href="mailto:<...

Create a table from CSV columns in SQL Server without using a cursor

Given a table: |Name | Hobbies | ----------------------------------- |Joe | Eating,Running,Golf | |Dafydd | Swimming,Coding,Gaming | I would like to split these rows out to get: |Name | Hobby | ---------------------- |Joe | Eating | |Joe | Running | |Joe | Golf | |Dafydd | Swimm...

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...

What's the OO way of refactoring these two very similar classes in PHP?

I have a class like the following: class DreamsImagesStore { public $table = 'dreams_images'; public function insertNewDreamImage($dream_id, $pid) { try { $values = array($dream_id, $pid); $sth = $this->dbh->prepare("INSERT INTO {$this->table} (dream_id, pid) ...

optimising aggregates with and without a condition in ms sql

Hi, I would like to get a sum from a column, with and without a condition. The code I have now is SELECT regular.id, regular.sum as regularsum, special.sum as specialsum FROM (SELECT Sum(stuff) as sum, id FROM table WHERE commonCondition = true GROUP BY id) as regular INNER JOIN (SELECT Sum(stuff) as sum, id FROM table Where commo...