refactor-my-code

Is there a concise way to test GET parameter combinations in PHP?

The code below checks for GET params. There are two selects on the page which filter a list by type and age group. I'm looking for a way to refactor the conditional which test the type/age combinations. Is there a more clear/concise way to write it? if ( isset($_REQUEST['type']) || isset($_REQUEST['age']) ) { // we need to do somethi...

How should I write a FindClosestMatching function?

Of all objects matching a condition, find the one closest to a position. Supposedly a very common problem. The current code looks like this: protected Foo FindClosestFooMatching (Vec pos, Func<Foo, bool> matches) { float bestDist = float.MaxValue; Foo bestFoo = null; foreach (Foo foo in AllFoos()) { if (matches (foo)) { float d...

How do I change this to "idiomatic" Perl?

I am beginning to delve deeper into Perl, but am having trouble writing "Perl-ly" code instead of writing C in Perl. How can I change the following code to use more Perl idioms, and how should I go about learning the idioms? Just an explanation of what it is doing: This routine is part of a module that aligns DNA or amino acid sequences...

AS2 to AS3 time function.

hello All. I use to do this on AS2 code. this.pause3Seconds = function () { trace(_global.something(_root.somthing)); clearInterval(myInt); }; var myInt = setInterval(this, "pause3Seconds", 3000); Now trying to worked out this into a class.as file got all type of migration errors and warning. So here I'm. anyb...

Need help on how to convert PHP code from object to procedural?

I was hoping someone can help me convert this piece of code from object form to procedural form. So I can have a sense of what to do for when I try to convert the larger piece of code that is all in object form that this snippet came from. I'm a brand new beginner. $sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY ...

Merge these two queries into a single query

I have the following queries: SELECT Sites.EDISID, Sites.[Name], (SUM(DLData.Quantity) / 8) AS TiedDispense FROM Sites JOIN UserSites ON UserSites.EDISID = Sites.EDISID JOIN Users ON Users.[ID] = UserSites.UserID JOIN MasterDates ON MasterDates.EDISID = UserSites.EDISID JOIN DLData ON DLData.DownloadID = MasterDates....

Can this implementation of an IEqualityComparer be improved?

I don't see any problems with this code, but it feels like I'm missing something. Maybe it is possible to reduce the number of lines. Or is there even a bug to be fixed? I'm open to any suggestions. public class NameComparer : IEqualityComparer<FileInfo> { public bool Equals (FileInfo x, FileInfo y) { if (x == null) { return y ==...

Looking for some refactoring advice

I have some code that I had to write to replace a function that was literally used thousands of times. The problem with the function was that return a pointer to a static allocated buffer and was ridiculously problematic. I was finally able to prove that intermittent high load errors were caused by the bad practice. The function I wa...

Architecture of player hand in JavaScript card game

So, i'm attempting simple card game. I have player "class" with draw function, and public members deck and hand, both are Arrays. I need to draw a card from the deck, put it in hand and show it in player "hand" area. I'm concerned about the way I do "flip" and "play" buttons (through closures). Here is the code: littlegame.player.prot...

How do I refactor three database operations that only differ by the method call?

I have three database operations like so: public void Add<T>(T entity) { using (var transaction = Session.BeginTransaction()) { if (entity is IEnumerable) { foreach (var item in (IEnumerable) entity) { Session.Save(item); } } else { ...

Refactoring help...property-based object or lots of member fields?

I'm currently refactoring a class which currently looks something like this: class SomeModel { private String displayName; private int id; private boolean editable; private int minimumAllowedValue; private int maximumAllowedValue; private int value; // etc. etc.... a bunch (10+) of other fields... // an...

Pimp my Perl code

I'm an experienced developer, but not in Perl. I usually learn Perl to hack a script, then I forget it again until the next time. Hence I'm looking for advice from the pros. This time around I'm building a series of data analysis scripts. Grossly simplified, the program structure is like this: 01 my $config_var = 999; 03 my $result_va...

Can this Sql statement be refactored to NOT use RANK/PARTITION?

Hi folks, I have the following sql statement, which works perfectly fine. I was hoping to see how this could be refactored so it doesn't require the use of RANK/PARTITION ... if possible. SELECT LogEntryId, FileId, CreatedOn, EventTypeId FROM (SELECT a.LogEntryId, a.FileId, a.CreatedOn, a.EventTypeId, RANK() OVER (PARTITION B...

Quick help refactoring Ruby Class

I've written this class that returns feed updates, but am thinking it can be further improved. It's not glitchy or anything, but as a new ruby developer, I reckon it's always good to improve :-) class FeedManager attr_accessor :feed_object, :update, :new_entries require 'feedtosis' def initialize(feed_url) @feed_object = Fee...

Loop each x elements

What's the beste way to show a list with 20 images in rows of 5? Or, in other words, how do I clean up this ugly snippet? <div class="row"> <% @images.each_with_index do |image, index| %> <% if index != 0 && index % 5 == 0 %> </div><div class="row"> <% end %> <%= image_tag image.url %> <% end %> </div> ...

How do I refactor these two C# functions to abstract their logic from the specific class properties they are acting on?

I have two functions whose underlying logic is the same but in one case it sets one property value on a class and in another case it sets a different one. How can I rewrite the following two functions to abstract away as much of the algorithm as possible so that I can make changes in logic in a single place? SetBillingAddress private ...

How would this code be refactored to use jQuery?

How would this code be refactored to use jQuery? function emleProcessOnLoad(aThis) { var result = document.evaluate("//span[@class='emleOnLoad']", aThis.document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); for (var jj=0; jj<result.snapshotLength; jj++){ eval("var emleThis=result.snapshotItem(jj);" + result.snapsh...

jquery/javascript - image resize to fit screen

not an expert programmer. i created this code to resize photos/images to fit the screen, considering the space available for the nav bar. the script happens on load of image, and on click of the navigation. is it a good piece of code, or could it be done better? any browser issues? in the html: $(document).ready(function(){ $("#photo"...

Java: Interface for this code

How can an interface be made for this code: package com.ejada.alinma.edh.xsdtransform; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import ...

Its all working so now I can chill

Most software houses I have been at have this attitude about their work. Get it done quick and move on. For programmers I find that the chill time is time that can be used to either improve infrastructure, refactor some old code or learn a new language. I would like to know what your throughts about 'chill time' if its the right attitu...