Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
1
vote
0answers
44 views
How can I refactor this method so I don't use it in multiple places?
I have the following method which I use in multiple places, but it only differs by a few different things, so I was wondering how I can refactor it so I can have it in a common class and call it from ...
2
votes
2answers
27 views
Refactoring an interruptable thread
I have a long-running task that processes an input file and then uploads it to a web server. The processing part can be interrupted (with cleanup) but the upload shouldn't be interruptable so that we ...
0
votes
2answers
53 views
Re-factoring amateur ruby code
I am new to ruby and I would be grateful if I need help with re-factoring my code.
class Beam
def initialize
puts "Please specify span of beam"
@span = gets.chomp.to_f
puts "How many ...
1
vote
1answer
42 views
It there a pattern used to process a same function but assign the result to a different variable regarding to a different condition?
Say I have a code snippet like this
if(string.Compare(isStandardFlag,"STANDARD",true)==0)
{
hearingID = centreSession.standardID;
centreSession.standardOutcomeID
...
1
vote
1answer
46 views
Refactoring Java transmitter delegate implementation
I have 3 classes: a transmitter, a view for displaying and a recording view for flushing the screen contents into a file. The view and the recording view classes register themselves from their ...
1
vote
2answers
82 views
CRUD Menu creation - how best to refactor?
I would like to re-write this c# code so it is easier to understand.
Should I create a separate function to configure a menu item?
Is there is some kind of using statement so I only need to mention ...
0
votes
2answers
39 views
Readability question
Which one of these two would you prefer?
The first piece of code is shorter and less noisy (especially when embedded in nested control structures), the second piece seems to me a little more correct ...
4
votes
1answer
47 views
My VBS Word macro is really slow, looking for advice!
So, I have a 100page long docx format document. I'm using a macro written in VBS to extract some information and then just generate a table from them.
I iterate through the paragraphs and store the ...
-1
votes
1answer
112 views
please review my switch case code
Now I hava a enum param and wants to do something which is acording to the enum param value.
While the enum param is too much and my method will become too long.How can I refactor my code?
I know ...
0
votes
1answer
49 views
How would I refactor this javascript code to avoid repetition
This is a code which has 3 buttons (each called #make_post_X) and once clicked on each it will do some stuff and then have the same code - check if what was clicked had active class, and if not it ...
1
vote
0answers
25 views
Converting data in a Rails Migration using the model
Lets imagine we have an Article entity, with data imported from a legacy database. It contains an appendix column with type integer. This column is always filled with values 0, 1 or nil, so we decide ...
7
votes
6answers
568 views
Complex if else
How would you write this?
string halfADayOff = "Half a day off";
string oneDayOff = "One day off";
string days = "NoDaysOff";
if (typeOfDelegation)
{
if ((differenceInDays == 0) ...
1
vote
2answers
134 views
How can I refactor this code in order to not repeat code?
I have this code:
private eFileStatus CheckConfigSettings()
{
mFileStatus = mXMLHelper.CheckElement(eElementName.StartLanguage.ToString(), out mContent);
if (eFileStatus.OK != ...
4
votes
1answer
118 views
Refactor a sequence of functions
I have an http library in Ruby with a Response class, I have broken it down into smaller methods to avoid having a big intialize method, but then I end up with this:
def initialize(res)
@res = ...
5
votes
1answer
97 views
Avoid redundant code in this simple method
I am wondering what would be an efficient way to simplify this very simple PHP method and I really lack inspiration:
public function formatToFullDate($countryCode, $hideTime = false)
{
$format = ...
2
votes
2answers
118 views
Decorators - a more elegant solution?
What I really like about SO, is that you get to see some many different ways of doing things! I am seeing a lot of use of itertools, and I am trying to get my teeth into that module more. I also see a ...
1
vote
2answers
97 views
Code review and refactoring this Python program
Here's a simple Python Flask app. It uses the wikidot API to transfer pages between two wikis. I'd like to hear your input on the best way to refactor this code. Here are a couple of questions I ...
2
votes
1answer
166 views
Is this spaghetti javascript code? How can it be refactored with a javascript library or framework?
The code allows you to select an area from the left column and another area from the right column followed by clicking on the Choose button which sends the chosen areas to the server:
<html>
...
1
vote
1answer
46 views
Refactor function that returns a comma Operator to pass jsHint
Some background
This functions does some business logic before submitting a "add to cart" form
HTML
<form class="atcLoginRequired" method="get" action="/somePage">
<input type="hidden" ...
0
votes
1answer
55 views
Optimization: Eliminate conditional expression within common code
Background
The following code (part of a natural language processor) was written to eliminate duplicate code by using isLeft as a conditional throughout the method:
private void ...
2
votes
3answers
70 views
I need help refactoring some Rails code that looks clunky.
I would like to refactor this block, it looks clunky:
# refactor me
receive_payment_on = false
config[:sections].each do |section|
if section[:applicants]
section[:applicants][:sections].each ...
1
vote
4answers
104 views
How can I refactor these while and unless loops to be DRYer?
I have a pack of cards and I am doing a 'deal' action.
I am dealing n number of cards, for now to one player.
How can I dry up the while and unless loops to be fewer lines?
def ...
0
votes
2answers
106 views
Recursive bubble sort in Java
I want to know if someone can suggest how I can remove the initialValue parameter of my method which sorts an array:
public static void bubble(int[] array, int initialValue) {
int aux;
for ...
2
votes
1answer
53 views
two or more render/redirect in the same method
I often have method like this one with two or more render to do due to catching the error for example.
I currently do something like this:
def update
@user = current_user
if ...
1
vote
1answer
145 views
Verify collection as a method parameter
How can I verify that ohterClassMock.MethodToTest() was called with contracts?
That it was called with contract, that have Id = 1 and Param2 = 34 and also with contract2, which has Id = 2 and Param2 ...
3
votes
2answers
94 views
Refactoring from .. in .. select to more compact one
Is it possible to write the two following lines of code in a more compact one?
IEnumerable<int> collection = from partnerNumbers in contract.Contact.PartnerNumbers select partnerNumbers.Pnr;
...
4
votes
1answer
127 views
Is there a better way to test for the exception conditions?
This method assigns the value of a specified column in a DataRow to specified property in an object.
I have 3 conditionals handling exceptions, and I want these conditions to throw exceptions.
The ...
3
votes
3answers
246 views
Rewrite from .. where .. select to more compact one
This is the code:
public DateTime GibSomeStartDate(IEnumerable<int> partnerNumbers, DateTime startTime)
{
return (from contract in this.databaseContext.Contract
where ...
3
votes
1answer
46 views
Improve this retrocomputing scanline function [Chrome only]
I need to refactor this code, targeted to Google Chrome (current+) to make it manageable going forward. My brain seems to be turning to a kind of workable mush, I'm not even sure if I'm thinking or ...
1
vote
1answer
49 views
Double linked list with different dependencies
I need to connect different Task with a double linked list and different Dependencies which affect a calculation for which I need the values of both Task:
public class Task {
private ...
3
votes
1answer
48 views
how can i make this small jQuery serializeobject util any better
I have this little utility/helper which serializes a form into a javaScript object
$.fn.serializeObject = function(fn) {
var o = {},
a = ...
2
votes
1answer
74 views
How can I make this jQuery more object oriented or reusable?
I wrote this code to allow users to select multiple images, and each image can be selected multiple times.
It feels convoluted, though, and prone to errors. Like forgetting to do something associated ...
5
votes
2answers
251 views
C# product inventory. How would you refactor this?
I am learning C# and I have made my way through the entire C# 101 course on buzz3D. The last project they have you do is create a product inventory system that must be split up between classes and ...
3
votes
2answers
40 views
Shortening method based on an argument name
As you can see below, I have a method which executes statements based on the first letter of a component firing an ItemEvent:
public void itemStateChanged(ItemEvent ie) {
if(ie.getSource() == ...
1
vote
2answers
97 views
How to optimize/refactor this method?
Here is my method:
public JsonResult ValidateAll(string toys)
{
Dictionary<string, object> res = new Dictionary<string, object>();
List<string> result_check = new ...
2
votes
1answer
46 views
Rails helper method refactor
I have this messy helper method:
def gesture(klass, item, text, desc)
element_class = klass.to_s + " gesture"
content_tag :li do
if klass == :sell
link_to new_reply_path(item_id: ...
1
vote
1answer
83 views
Is there a valid answer to this “Why?” comment?
This is probably too little to go on for a definitive answer, but in looking over some legacy code, I found this (a previous peruser commented "Why?") in what serves as the main form in a Windows CE / ...
1
vote
1answer
49 views
Is this enum declaration “wrong,” or is it just me?
Legacy code has this enum declaration:
public enum ChangeListTypes
{
Always = 1,
Never = 2,
CostChange = 3,
None = 0
}
I would have done it this way:
public enum ChangeListTypes
{
...
4
votes
4answers
197 views
Find the 2 distinct highest values in a array
Good Night
I want to know if there is a better way to improve my code to find two highest in a array, and these numbers need to be distinct.
I don´t want to sort the values.
Only run in the array ...
2
votes
4answers
107 views
Remove code duplication inside of a loop preserving performance
I'm coding a 2D collision engine, and I need to merge adjacent axis-aligned bounding boxes depending on a direction (left, right, top, bottom).
The four cases are very similar, except for the if ...
4
votes
1answer
53 views
Is aliasing repeated code as internal lambdas a good coding practice?
When writing code that contains repeated blocks, it is good practice to extract it in an outside function.
C++11 also allows me to extract the code into a lambda, within the function body. Are there ...
2
votes
1answer
50 views
ORM Entity with many similar relationships
To provide some background context, I'm implementing a web-based solution (Java, Spring, Hibernate) that allows the creation/authoring of task-centric workflow documents. Basically a workflow ...
4
votes
1answer
67 views
ObservablePriorityQueue<T> Implementation
I have a requirement in my current project that will need a Prioritised Queue that supports the IObservable interface. Please share any problems with the current implementation below:
...
0
votes
0answers
79 views
Please criticize my mini-app for android
Began to read the book "Clean Code" by Robert Martin, I had a strong desire to learn how to write clear, easy to understand other people code.
ColorViewer application allows you to view the color in ...
3
votes
2answers
107 views
An abundance of ternary operators
I'd like to find a way to do what this method does in a cleaner, less hacky looking way
def self.create_from_person!(person)
spi = new(:person => person, :provider => person.provider)
...
3
votes
3answers
63 views
Reading image from jar
I am using this static method in my class IconManager to read an image from jar file. imagepath is a relative path to the image.
This code works perfect. But I was told it is error-prone, because ...
1
vote
1answer
35 views
A better approach to coding the a string value test for a NSDictionary object
My conditional code here seems repetitive and long. Is there a better approach? I want to test for a string value in a NSDictionary object and then depending upon the value prefix a UILabel with $, £, ...
3
votes
2answers
120 views
Help in refactoring this method - Ruby
def divisible?(n)
if n % 1 == 0 &&
n % 2 == 0 &&
n % 3 == 0 &&
n % 4 == 0 &&
n % 5 == 0 &&
n % 6 == 0 &&
n % 7 == 0 &&
...
2
votes
1answer
22 views
How to refactor that content_tag adding method?
How to get rid of that duplicaiton in if conditional?
def set_caption(result)
if result.respond_to?(:app_name)
content_tag(:div, result.type_name, class: 'type-name') +
content_tag(:div, ...
1
vote
1answer
26 views
Refactoring same code across 2 classes in python
I have two classes with similar first checking codes, but different behaviors. I supose there is a way to refactor the code, so I dont need to retype it every time.
It is possible to refactor this, ...