Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Use this tag only when you're looking for constructive feedback for rewriting parts of your existing code into better, cleaner code.
8
votes
1answer
108 views
Refractoring OOP vs. Functional Programming Approach
I am writing a python script that will call mandrills export activity api for every blog on a worpress mu install except for the main site. The script is designed to be called from a cron job on a ...
8
votes
1answer
60 views
Switch-case Monstrosity for CPU Emulator
I am still working on A Z80 CPU emulator and I have decided to prototype it in javascript and then translate it into a faster language, It might be ported to C but more likely is Java - this means I ...
3
votes
2answers
54 views
Readability and cyclomatic complexity of this Ruby code
I consider this two options exactly the same. I prefer the first one but I'm not sure if it's better the second option in terms of cyclomatic complexity or readability.
Assuming that this is not pure ...
0
votes
2answers
31 views
Avoiding code duplication in mapping layer between dataRecord and
The following code is for mapping a dataRecord to a class. I don't like this solution since it requires to duplicate the logic of mapping for each field in every method which access to the same table.
...
-3
votes
2answers
70 views
Can this be written more optimally? Linq? Switch? Something? [closed]
if(_toTreatmentInfo == null)
{
medInfo = treatmentList.FirstOrDefault(item => item.PharmacyRXCode == _toPatientTreatment.PharmacyRXCode);
if (medInfo != ...
4
votes
5answers
458 views
How to shorten this code or make it more efficient?
How to shorten up this code? I know there's a way that this kind of code could be shortened, but I don't know how by myself yet. Who can help me?
//CHOOSE LEVEL: FROZEN
case 1213:
...
3
votes
1answer
78 views
Genericizing PropertyValues
This should be the last iteration for this code.
Question 1
Does this approach make sense or am I heading down the wrong path?
Question 2
The only thing I can see to make this better is to ...
7
votes
4answers
189 views
User search implementation
In my Java code I have these three classes. Basically I have a User class, and also a GUI class that holds TextViews and other things (GUI related) for the properties that show on the screen.
The ...
6
votes
1answer
100 views
How can this python quadtree be improved?
Here's an attempt I've made of implementing the Barnes-Hut nbody algorithm, or its initial stage - The quadtree. That there're lots of lengthy doc strings might excuse the lack of detailed explanation ...
6
votes
3answers
229 views
Next Instance of DRY Refactoring
I'd like some additional assistance on refactoring. This is my latest update for this iteration.
#region WMI Classes
/// <summary>
/// The Win32_OperatingSystem WMI class represents a ...
0
votes
0answers
28 views
Implementing a login client
I have a lot of functions in this class to implement a login client. How do I modularize it ?
from PyQt4 import QtGui, QtCore
from xml.dom.minidom import parseString
from datetime import datetime
...
2
votes
1answer
73 views
How does this refactored code make my code better? [closed]
According to my lecture notes, there are benefits to refactoring the code from original code to refactor code.
Reasons for refactoring:
Replace Temp with Query
You are using many ...
1
vote
1answer
95 views
Update user's last login date
This function updates the user last login date on the user collection. I guess there are too many brackets and much spaghetti. How can I shorten this code?
(function () {
'use strict';
var ...
2
votes
1answer
46 views
Refactoring a String Calculator
I've refactored the string calculator kata as much as I could and tried to maintain single responsibility. Is there anything I could factor differently? The specs can be reviewed here.
module ...
6
votes
3answers
465 views
Lots of repeating code, trouble refactoring
I'm looking for a design pattern or suggestions that can help refactor my code into something a bit less repetitive. I have a method that has several sequential steps (10-15) that if any of them fail ...
3
votes
2answers
39 views
Elegant way of making a List of an attribute from a list of objects containing that attribute
If this is my class:
class Something
{
UUID id;
// Other members
}
Given a List<Something> things and I want to get a List<UUID> ids, this is what I usually do:
...
2
votes
1answer
95 views
Refactoring a Delimiter
Is there a more cleaner approach to this? I'm doing the String calculator Kata posted by Roy Osherove. I realized that my calculator has no way of clearing out a previous expression after calling add. ...
3
votes
0answers
38 views
Improving XNA's default project template
I hope this isn't getting annoying, but I've asked a lot of questions about improving my game lately:
How can I improve my game project design?
Using the observer pattern with collision detection
Is ...
6
votes
2answers
81 views
Read CSV, compute some stuff and prepare data to be sent to GNUPlot
I would really appreciate some good advice on how to refactor a small app:
It basically reads a CSV (energy/heat values?), computes some stuff, and then prepares some data which should later be sent ...
10
votes
2answers
147 views
Setting up keyboard bindings using JSON (no reflection!)
I got a lot of good feedback on my other question. So I will take another swing at it, post my revisions, and hopefully get some more.
First, the configuration hasn't changed. Here's the JSON file:
...
20
votes
6answers
3k views
Refactoring large switch statement
Lately I have been trying to extract till I drop. Well not necessarily till I drop, but I've been trying to be more strict and look at some metrics of my code.
I have now come along an old class of ...
0
votes
1answer
21 views
Is it reasonable to have separate controller actions for different user registration paths? Or should I refactor into a fewer number of actions?
I have two registration paths in my app. They are very similar and I am curious if I should simplify 4 associated actions down into two actions that handle the same amount of work.
Here are my ...
7
votes
4answers
55 views
What is the best way to refactor out “temporal dependence�? of instance methods?
For example, suppose in a view class we need to instantiate some UI components and then populate the fields. Something like:
var View = function() {
this._initUI();
this._populateFields();
};
...
2
votes
1answer
76 views
Refactor ConvertorToString class
Help me refactor this class that helps to represent an object state as String:
public class ConvertorToString {
private static final String SEPARATOR_BETWEEN_FIELD_NAME_AND_VALUE = "=";
...
3
votes
2answers
65 views
One-file script that detects changes in file-systems via polling and Pythonic idiomatic-ness
This is a (relatively) cleaned-up version of a Python script I've been working on for the past day or two based on Tim Golden's first solution to the above problem. I know about watchdog but I'm ...
3
votes
1answer
55 views
PHP class design with methods requiring database access
I've been a procedural PHP programmer for a long time but I'm getting to learn OOP and would like some advice. I'm designing a class which currently is composed mainly of simple getters/setters, but ...
3
votes
1answer
54 views
cleaner python — refactoring to reduce noise? (twirling cursor)
Questions:
(1) Is the cost of calling subroutines in Python cheap enough to support refactoring, i.e. pulling out flash() as its own subroutine, halftwirl() as its own, and making twirl() call these ...
14
votes
5answers
2k views
Tic-tac-toe 'get winner' algorithm
As a first step to attempting the Weekend Challenger Reboot, I decided to first implement a regular 'tic-tac-toe' game and then expand it later to be 'ultimate tic-tac-toe'.
Below is the code for the ...
3
votes
1answer
98 views
Refactor C# code for creating XML to not use intermediate dictionary
If have the following code that via an SQL query retrieves data from a database in the form:
Id subschemaName locusName
MLST MLST LMO0558
MLST MLST LMO0563
MLVST ...
10
votes
2answers
171 views
Knockout.js binding 2D table with rowspan
The app I'm working on should allows it's users to create tables. I have a view in which users are able to create a table. They should be able to define here the table's columns. The columns that the ...
2
votes
1answer
75 views
Code reuse while keeping meaning clear and avoiding unforseen consequences
I like to reuse code as much as possible and keeps things DRY. But I'm afraid that sometimes it creates too many layers of indirection. It then becomes a chore to find where code exists that is ...
2
votes
1answer
119 views
How to make string.Format template more readable?
Have a look to the following example. Clearly it's very hard to understand the meaning of each parameter passed to the string.Format to be replaced to the numeric sequence of {0}, {1}, ...
I want to ...
5
votes
3answers
82 views
Could random unique selection using while cause an infinite loop?
I recently came across code that looked something like this (generates 4 random numbers in an array, this is not the actual code, I just wrote this up now, so it's untested):
var uniqueNumbers = new ...
5
votes
1answer
62 views
Refactoring Java class with lots of constants
class MyObject
{
public static enum Type {A, B, C, D;}
public static final int ID_MAIN = 1;
public static final int ID_MAIN_UK = 2;
public static final int ID_MAIN_US = 3;
public ...
3
votes
1answer
92 views
Refactoring calculator expression
I was doing a simple exercise to calculate the amount based on an expression.
For example:
(((2*2)+(3*5/5)+(2*5))) outputs 17
((2*2)(3*5/5)(2*5)) output 120 etc
I have written the code, but I am ...
4
votes
3answers
72 views
Number to words problem
This is a very long solution I've been working on to a Number to Words problem. I've identified that there's a lot of repeating logic (e.g. 'if writing > 0', 'writing = integer /',).
How would you ...
5
votes
1answer
44 views
One-line initialisation for a constant list with all entries except one from another list
I'm looking to simplify the initialisation of the second constant, preferably to just one line. Any ideas? Anything from Guava or JDK (up to 1.6) is ok to use.
enum Box {
PROMO_HEADER,
...
4
votes
1answer
112 views
Refactor jQuery code to use fewer selectors
At this point I've written a fair amount of code to deal with dynamic line items. I've slowly refactored most of the code into an Order object that's created at page load which contains the methods ...
1
vote
1answer
152 views
Changing html element with click event: how to restore default state of element using jQuery if/else statement
I have a click event that calls a function (below, txtResize) which makes some CSS changes to html text. (Acts on Text of 3 unique elements).
Q: What is the best way to refactor this code to limit ...
4
votes
1answer
67 views
Refactoring RadioGroup setter code?
I am setting different RadioGroup like this:
fragment.setT1RadioGroup((RadioGroup)fragView.findViewById(rowids[0]).findViewById(R.id.t1_radiogroup));
...
4
votes
2answers
66 views
Changing text with a click event: how to restore default state of text in jQuery if/else statement
I have a click event that calls a function which makes some CSS changes (acts on Text in 3 unique text elements).
The function (below) works, but is lacking a 'restore default' function (to return ...
4
votes
1answer
48 views
Project Euler Problem 1 - Multiples of 3 and 5
I wrote this function for Project Euler Problem 1.
prob001 :: (Integral a) => [a] -> [a] -> a
prob001 a b = sum [x | x <- a, product ( map (x `rem`) b ) == 0]
The use is like this
...
4
votes
1answer
41 views
Refactoring elisp code that generates files
I wrote a simple emacs module that generates standard templates that I use for my blog's static site generator.
(defun hakyll-site-location ()
"Return the location of the Hakyll files."
...
2
votes
2answers
192 views
Calculating Julian dates
Refactor this code so that I don't have to insert the parameters any time I need to use one of the functions.
I have a class, say julian, for calculating Julian dates of today, a date I insert and ...
-2
votes
2answers
182 views
Trivia game in Java [closed]
I have been given this piece of code to refactor. I have refactored it, but I am not sure whether I missed something on refactoring. Maybe someone has a better idea.
Here is the given un-refactored ...
2
votes
2answers
68 views
Unwanted return values in lambda functions?
From a previous question, I got the idea of moving boilerplate code structures into a helper function and passing in a lambda function to it.
I then applied the idea to a data access class (code ...
5
votes
1answer
114 views
Refactoring chart generator in Linq
I'm kinda curious how I'd refactor this in the best way. I've only used Linq for simple queries.
What it does:
Depending on the type that is submitted, we're grouping events or payments by ...
4
votes
3answers
97 views
Extracting pattern and simplifying testing
I observe the same pattern in a number of my classes but can't extract/abstract it due the tight coupling inside each particular implementation.
Having the class accepting an optional number of ...
6
votes
2answers
122 views
Hangman game code refactoring
I would like some help refactoring this code. This is to improve some of the nonfunctional attributes of the software to
improve code readability
reduce complexity to improve the maintainability of ...
8
votes
5answers
281 views
Abstract Pet class
As a student of C# right now, I am trying my best to get a grasp on many of the systems being taught. I just completed Week 1 of a C# 102 class and the class was assigned a bit of homework. I am ...