Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

learn more… | top users | synonyms (1)

6
votes
2answers
63 views

Refactor code to populate a spreadsheet

My code looks ugly. What can you recommend? I already know about Enum; maybe it's a good variant for this situation. Anything else? private void writerContent(Integer firstRow, List<Record> ...
2
votes
1answer
29 views

Refactoring my windowsFormApp code

I have around 150 lines of code that I managed to optimize. But, because my two methods and the six if-statements almost are identical, could there be room for improvement? Maybe it could be done ...
1
vote
2answers
38 views

Rails 3 nested controller method

I have 2 nested resources in one of my Rails apps and the way the index is handled by the inner resources bothers me somewhat: def index @program = Program.approved.find(params[:program_id]) if ...
0
votes
1answer
20 views

Creating a generic function for objects of different classes

How might I refactor this to get rid of all the if-else? def self.dispatches_for(object) scope = Dispatch.asc(:days) if object.class == Habit scope.where(:habit=>object).map{|d|d} ...
0
votes
0answers
12 views

How can I reduce redundancy in this angular or the angular html?

I'm very new to angular as far as directive and module options. I've just made my first module and factory here. The areas in the UL are frequently repeated elsewhere in the app, but are seem like ...
0
votes
2answers
41 views

Refatoring Implementation in.NET

I have implemented the following code: class A { abstract void f1(Object obj, Object data); } class A1:A { void f1(Object obj, Object data) { m1(obj,data); } void ...
3
votes
1answer
59 views

State Machine for Character

I come today with a state machine I'm currently working on. Problem: Given any State that the Character is in, a button press or combination of button presses can modify that state in a different ...
2
votes
1answer
43 views

Python: refactoring verbose functions

I wrote up a simple averages calculator for student grades, based on this daily programming challenge. First, the script asks for the file name for the text file containing the student names and ...
0
votes
0answers
13 views

Get rid of long/Complex if..else statements using Chain of Responsibility? [migrated]

I've an HttpHandler, which allows users to login to a system by passing in an encrypted code. Inside the ProcessRequest it performs quite a few steps. Retrieve the encrypted code from request ...
0
votes
1answer
54 views
+50

What about accessing jQuery plugin methods with triggers?

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle: http://jsfiddle.net/styu/5H8cA/ I want to access some of the methods from outside of the plugins scope, namely showPopup(), ...
0
votes
1answer
62 views

How to further refactor these ruby methods?

The code I'm trying to improve, modifies a url according to the options passed in a hash, like this: { :image_aspect_ratio => "square", :image_size => 50 } or { :image_size => { ...
0
votes
2answers
55 views

Write a class with some properties and indexed

Is there a better way to write it? I need the best way to write it. It is this principle? Please show me a better way it breaks SRP? public class PlacementLocation { public int Row { get; set; ...
2
votes
1answer
104 views

Refactoring to avoid if-else in numberOfRowsInSection and cellForRowAtIndexPath

Below is the code that I would like to refactor in order to avoid if-else statements in cellForRowAtIndexPath and numberOfRowsInSection. Depending upon what user selects, section order and ...
2
votes
1answer
54 views

Review internet connection pinging method

This is some C code I have to simply test the internet connection. Any comments/tips on efficiency and refactoring this program down would be greatly appreciated. int testConnection(void) { int ...
2
votes
3answers
105 views

Did I Needlessly Complicate This Function?

Background: We're using a class called Places (inspired by Allen Holub's great article on DrDobbs) that resolved our program paths based on the existence of a configuration file, environment variable ...
4
votes
2answers
126 views

Is there a way to refactor this code?

I'm new to ruby on rails, on this project I'm using Ruby 2.0 and Ruby on Rails 3.0. I would like to know if this piece of code can be refactored, as it is unless params["ot_code"].nil? ots ...
2
votes
1answer
50 views

Different types of model validation

So we have a RESTful webservice that implements CRUD operations for a model, like this one: class MyModel { private long id; private String name; // other fields private boolean ...
2
votes
1answer
107 views

What is the point of this code?

I have come across below method and as far as I can see this method just converts the first letter of a String to uppercase in if its not already. Below main method returns : "Joe" "Joe" "Jo..E" ...
4
votes
2answers
105 views

Small function and level of abstraction - clean code - refactor further

In the book Clean Code from Robert C. Martin are given many recommendations. Two of them are to keep functions small and within a function to only have one level of abstraction. I already ...
1
vote
1answer
56 views

Lowering the cyclomatic complexity of this method, suggestions?

The PHP Mess Detector of my IDE warns about a Cyclomatic Complexity of 14 (threshold is 10). To me this code doesn't seem to hard to follow. Would you refactor it in some way to lower the metric? ...
1
vote
0answers
43 views

Android image with rounded corners

I've followed this blog to get images with nice pretty rounded corners in Android: http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ I've managed to strip out a ...
5
votes
3answers
227 views

Refactoring to functional style C#

Hi I'm trying to improve my functional skills and seeing where it fits and where it might not. Please review the code and see where functional practices might be applied, I'm specifically looking at ...
6
votes
1answer
288 views

Intern with no mentor struggling with refactoring and OOP

A little background I'm an intern at a large engineering company, and I'm also the only CS major in the entire building. The people on my team don't have technical backgrounds, but hired me to start ...
2
votes
2answers
90 views

How do I refactor this form object to lessen verbosity?

I wrote this form object to handle an e-commerce order form. My areas of concern are the verbosity of the populate method, and the repetition of the validations. I omitted some address fields and ...
1
vote
1answer
49 views

What are best practices of creating database importer in Rails?

I have sample database in GES files and I wrote importer in Ruby. Sample file: WAR_APO.GES: https://gist.github.com/regedarek/cd0ce73c5d1e14ff9818 I will have several of GES files which I want to ...
1
vote
1answer
73 views

More efficient way to retrieve data from JSON

I am using the jsmn JSON parser (source code) to get some text from a JSON. jsmn stores the data in tokens, but the tokens do not hold any data, they just point to the token boundaries in JSON string ...
1
vote
2answers
89 views

Refactor code to make it more testable

I have this code that works fine. But I would like to create some unit test for this class. Most of the code is in private or void type methods. Please help me refactor my class to make it more ...
3
votes
1answer
195 views

Custom WYSIWYG Editor - Jquery

I am so sick of WYSIWYG editors being exclusively for the latest browser or being huge with every option under the sun. I tried to make my own, using code I found online as well as a little of my own ...
2
votes
3answers
102 views

Refactor to reduce code duplication

I have four methods like these (here are only two of them): def checkLeft(clickedIndex: Int): Option[Int] = { val leftIndex = clickedIndex - 1 if (leftIndex >= 0 && clickedIndex ...
2
votes
2answers
114 views

How can I make only one if-else statement among all of these?

I have these if-else statements that I am not able to refactor. I have used them for doing validations on server-side using asp.net. Would anyone please suggest ways to reduce these statements? ...
2
votes
2answers
177 views

How to improve this Python function?

This code works nicely for my purposes, but I would like to improve it exponentially. What are some steps I can take? I'm interested in optimization, refactoring, and clean-code. def ...
0
votes
1answer
52 views

How do I automatically call settings for each method?

I am using sendgrid gem to send emails and track by categories, class#method_name and arguments. class NotifyMailer < ActionMailer::Base #* 1) Extracting these 4 lines into separate ...
0
votes
2answers
102 views

Refactor or keep this high-complexity method?

Is this acceptable for readability or would it be better to refactor it into smaller methods? Tips? Comments? Examples? def save_with_payment if valid? charge = Stripe::Charge.create(:amount ...
0
votes
2answers
68 views

How can I DRY this Rails code?

users_controller.rb: class UsersController < ApplicationController def new end def create user = User.create(params[:user]) if user.id session[:user_id] = user.id ...
2
votes
4answers
191 views

if-elseif chain. Is there a better pattern?

I'm working on a TCP Server using .NET and the following code is my first attempt to process the received data (from the socket) to create a packet to deliver. Basically, the packet has a header (7 ...
2
votes
1answer
120 views

Is there a better way to implement this class? [New Programmer]

Is there a better way to implement this class, or does it not constitute implementation in the first place? This is one of my first few tries at OOP. Any suggestions will be much appreciated. ...
1
vote
1answer
61 views

Refactor IQueryable and IEnumerable that share the same condition

I have an extension method to encapsulate query condition, however. i have to separate IQueryable and IEnumerable. Is there any way i can reduce the redundancy and still not lost the performance ...
2
votes
1answer
81 views

Circular Buffer on a typed Float32Array

I'd like , if you would be so kind, some suggestions on performance improvement or maybe someone can point out mistakes I made in the implementation. I am sure there are better ways to do things but ...
1
vote
1answer
40 views

How Can I Refactor This Code For Input Errors

I'd like to refactor the code below to account for if someone types in something besides a number 1 thru 5 or a comma. def roll_again puts "Which dice would you like to keep from this roll? (1, 2, ...
1
vote
1answer
62 views

Refactoring tips

I don't really have experience in factoring. My code is really long, i don't use functions because i don't know if it needs to be a function. I hope you could give me tips so i could clean up my code. ...
0
votes
0answers
58 views

Using a spinner like a HTML select to display string but return int value

I'm still learning Android, I need to have a spinner and for it to display a list of string values. However, when I submit that data later on I need a corresponding integer value. Similarly to how a ...
1
vote
1answer
55 views

How Can I Refactor the Yahtzee Program I Built in Ruby

require'yaml' require 'text-table' class Yahtzee def initialize puts "Welcome to Yahtzee, please enter your name." @name = gets.chomp @turn = 0 @scorecard = {"aces" => 0, "twos" ...
2
votes
2answers
97 views

Separate Numbers with Commas in Ruby

Help! My code works, but it is ugly!! As a newbie, how should I approach refactoring this code? def separate_comma(number) a = number.to_s.split('') b = a.size/3.0 if a.size < 4 p ...
2
votes
5answers
163 views

Java, which of these two is better? [closed]

SomeClass obj = null; try { doSomething(); obj = getResult(); } catch (Exception e) { e.printStackTrace(); } return obj; or try { doSomething(); return getResult(); } catch ...
0
votes
1answer
50 views

How should I refactor my image uploader script?

I come from a C background and need help refactoring the following imgur image uploader script I wrote. It checks if a token_file with access_token and refresh_token exists. If not, it instructs the ...
2
votes
2answers
87 views

Refactoring same loop logic with different input data

I have these two methods: public static String buildLine(String[] values) { StringBuilder lineBuilder = new StringBuilder(); for (int i = 0; i < values.length - 1; i++) { ...
3
votes
1answer
99 views

Refactoring a method that binds data to a TreeView

All the flow depends on parameters. It's all in same method with an if else...but I want to make the code a bit more clear. How can I functionally decompose this method into many? private void ...
0
votes
2answers
123 views

Printing out all the subsets of a set. Am I unnecessarily creating new objects?

1) I am concerned about the creating too many objects in my code. 2) Am I using duplicate code? Can I cut down on the number of lines of code? Note: I only want to use recursion to solve this. ...
0
votes
2answers
52 views

Refactor selective item by highest create date or update date

I have to check for the Highest Date from CostRepository whether it is create date or edit date then i have to get value in that field. edit date can be null. i don't know how to fix the codesmell. ...
1
vote
2answers
68 views

Refactor data tranfer object and model

T_PROJECT is the class that will be retrieve data from linq to sql and ProjectOwnerDataFields is the class that will be used to show data in view. in edit mode i have to get value from model and send ...