Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
1
vote
1answer
31 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
26 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
65 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
26 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
76 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
43 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
163 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
93 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 ...
2
votes
0answers
24 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
49 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
59 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
62 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
95 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
59 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
25 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
113 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
20 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, ...
1
vote
1answer
29 views
How to improve this functional python trial division routine?
The following functional program factors an integer by trial division. I am not interested in improving the efficiency (but not in decreasing it either), I am interested how it can be made better or ...
3
votes
2answers
56 views
Refactorize my python code for GET and POST
I'm newbe with Python and GAE, but years of procedural programming. I have this code, but I know that there must be some better solution to avoid retyping code.
The first and last section of code are ...
0
votes
0answers
28 views
Testing scala code which depends on objects
I am writing a small oauth2 library for Play! 2.1 scala. As I am still learning I got stuck trying to TDD and ended up writing the code first, then refactoring for testability. By testability I mean ...
3
votes
3answers
191 views
How to refactor this property's code?
I have a view model with properties that represent years and months:
public IEnumerable<SelectListItem> Years
{
get
{
return new SelectList(
Enumerable.Range(1900, ...
3
votes
2answers
66 views
Can I improve the way this model RSpec is written?
I'm new to RSpec and testing in general. I've come up with a spec for testing my Content model and I need some feedback 'cause I think there are many improvements that can be done. I don't know if the ...
3
votes
1answer
132 views
Refactor if statements
I am importing an excel document into my project.
I then use the following code to verify that columns have data and display the appropriate error message.
My code is working but it just doesn't ...
5
votes
2answers
148 views
I there a better way to handle try in C#
I know that is generally expected that you should not swallow exceptions. In this code an Infragistic's UltraWinGrid is being configured. Is there a better way to handle the failed catch's are is this ...
3
votes
2answers
164 views
Where to start refactoring?
I am currently programming, but i messed up my code, what is the best way to clean my code.
I really need to know this, next week i must be done with the assignment, but the teacher said that the ...
1
vote
1answer
94 views
Clean code for array comparison
Following snippet reads CSV Line count using BinaryReader.
Currently it checks \r and \n for line delimiters.
private static int GetLineCount(string fileName)
{
...
1
vote
2answers
110 views
Ruby implementation of Conway's Game of Life
I'm just starting with Ruby and decided to do two different implementations of Conway's Game of Life. In this first one I'm not using cells as a separate class and instead just track everything as a ...
1
vote
1answer
101 views
Help me simplify this gigantic class
I'm trying to think of ways to separate things out. I'm open to ideas, or if you see anything blatantly wrong, I'd like to know that too.
Generally, I'm happy with this, but the sheer size of the ...
0
votes
0answers
32 views
How to create 3 objects using one form only if each one is valid?
I want to create Job, Employer and Company using one form.
Model
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company, :employer
belongs_to :company
belongs_to :employer
...
0
votes
2answers
119 views
Review code for optimization and implementation
The below code does Read csv files and process it to do some sort of
cacluations.This is more of small picture code , to introduce big picture.
The csv file contains information of seeds of crop which ...
0
votes
3answers
70 views
RLE encoding/decoding tool, review source
I've developed mini utility, the source code can get reviewed here:
http://rle.codeplex.com/SourceControl/BrowseLatest
It's developed in C#, in the general page there is a description.
I'm listening ...
2
votes
1answer
120 views
Recursion and iteration how can i optimize this code
I have written some code which will fetch contents from a resource which is actually stored in a tree format. Since it is in a tree format, there will be a parent-child relation and hence recursion.
...
2
votes
3answers
110 views
Python function speedup/optimization
everybody...I am working on a p2p streaming sim (which I think is correct), however I've discovered a huge bottleneck in my code: During the run of my sim, the function I include below gets called ...
1
vote
2answers
102 views
Refactor this Ruby code for me, please
I always seem to struggle refactoring my code. I can look at any other code and know exactly what is going on, but when it comes to cleaning up my code, I get writers block.
The following code ...
4
votes
2answers
88 views
Refactor highlight matched word in string
I have following method which highlight matched word in text:
# Hightlight matched term
#
# Ex(for term: some):
# "<span class="bold">Some</span> CEO Event"
#
def ...
2
votes
1answer
49 views
Should we create a new constant or modify an existing one
I was fixing a bug in the code and it was something like this:
int const ROW_LOC = 1;
//....
Foo ( x , y , ROW_LOC ) ;
so it was passing that constant to a method ... and this ROW_LOC constant is ...
4
votes
2answers
172 views
ElasticSearch, Tire - refactor autocomplete method for multiple resources
I am using ElasticSearch and Tire for search in my app across all models.
I would like to refactor autocomplete method because it looks to complex for me.
Am I using a good pattern for searching ...
3
votes
1answer
195 views
Refactoring a Telnet parser
I'm writing a Telnet parser in Haskell that can run within any monad that implements some event-handling functions. When it finishes processing a block of input - whether because it read it all or ...
1
vote
1answer
104 views
Refactor or simple code RoR
I have a GiftCard class where I have method can_deliver? and deliver! that I think can be refactored to better looking code :
def can_deliver?
(self.sent_at.nil? && ...
4
votes
2answers
76 views
Refactor when complex expression in multiple parts of list comprehension
In Python a have a nested for-loop, that populates list with elements:
a = []
for i in range(1, limit+1):
for j in range(1, limit+1):
p = i + j + (i**2 + j**2)**0.5
if p <= ...
5
votes
1answer
260 views
Improve Graph Adjacency List Implementation and Operations
I am studying graphs and I am trying to make a library in C# for myself and anyone else interested, that is didactic and simple, so I can remember how to solve common problems.
I would like to ...
2
votes
3answers
109 views
Simple: Need help refactoring this awkward looking method
I need help refactoring this method. It is way too long.
def report_total(feed_event, advisor)
count = 0
advisor.activity_feed_events.each do |lead|
if lead == SignupFeedEvent
...
0
votes
2answers
93 views
Better way to write this fold operation?
I often have code that looks like this:
boolean isFirst = true;
var builder = new StringBuilder();
foreach (var foo in source) { //source might be empty
if (isFirst) {
isFirst = false;
}
...
0
votes
1answer
78 views
Improving my code (make more generic)
namespace SFE.Workflow.Commands
{
public class SendEmailToAMCommand : ActionCommand
{
private readonly Func<IApplicationRepository> applicationRepository;
private ...
0
votes
4answers
105 views
Refactoring for elegance - how to improve this switch statement?
Below is my current code for sorting an array, "$jobs_list".
It's not very DRY and it's fugly. And there are several more similar case statements still to go in. How could I write it so my brain ...
0
votes
1answer
26 views
Created a DialogBuilder object, but having issues with subscribing/unsubscribing to dialog events
var WorkflowDialogBuilder = _.once(function () {
'use strict';
var workflowDialog = $('#WorkflowDialog');
var workflowDialogContent = $('#WorkflowDialogContent');
var events = {
...
0
votes
2answers
119 views
optimizing project euler #83 solution
I have solved the project euler problem 83 using uniform cost search. This solution takes about 0.6s to solve. I want to know if anyone can get the code to run relatively faster without changing the ...
2
votes
1answer
132 views
PEG parser in Python
Any suggestions to make this code clearer, more Pythonic, or otherwise better? I'm open to changes to the design as well as the code (but probably won't drop features or error checking since ...
1
vote
1answer
99 views
Refactoring code into a generic method [closed]
I have a reoccuring code block in my EntityFramework backed repository which I would like to genericise somehow and call as a method, so reuse the code rather than repeat it.
The current code block ...