David Harkness
  • Member for 10 years, 10 months
  • Last seen more than a month ago
1 answers
4 votes
618 views
Constructing a URL for execution using RestTemplate
Accepted answer
4 votes

For adding and removing single host names, I would use a simple ConcurrentHashMap without the AtomicReference. Initialize it with an empty map and drop the additional latch. Update: I really doubt ...

View answer
2 answers
5 votes
10k views
Java implementation of the Factory Method pattern
4 votes

This pattern can be difficult to understand outside of an application with many modes of operation. There are two advantages to using the factory patterns: you can swap in different implementations, ...

View answer
2 answers
2 votes
3k views
Assigning array keys based on the values of another array
Accepted answer
4 votes

PHP provides functions to parse CSV files and strings line-by-line. $file = ... $headers = fgetcsv($file); $lines = array(); while (($line = fgetcsv($file)) !== false) { $lines[] = array_combine($...

View answer
2 answers
6 votes
212 views
Calling functions with variable functions
Accepted answer
4 votes

There are several other ways to solve this, but which one I'd go with depends a lot on the Group class. Do you plan to have more than a handful of properties to choose from? Do you have control over ...

View answer
1 answers
8 votes
4k views
Private methods and properties in jQuery "lightweight start" pattern
Accepted answer
4 votes

You could use a closure to create your own proxy. Plugin.prototype.init = function() { var self = this; rows.each(function (groupIndex, group) { self.initGroup(groupIndex, group); ...

View answer
2 answers
6 votes
244 views
Clean, efficient and extensible code for querying collections
4 votes

When using a generic name for a field (which is often fine), avoid the names of the actual type's superclasses: collection -> items since Collection lacks the ordering semantics of List. Name ...

View answer
2 answers
11 votes
17k views
Pattern for storing object of varying type
4 votes

You are breaking encapsulation and the Liskov substitution principle by exposing the individual parameter objects to clients. I expect getting a value looks something like this: String host = dataObj....

View answer
4 answers
4 votes
147 views
Array of Objects or Array of arrays?
4 votes

Keep related data together. Does your application need a list of contacts? Keep each contact intact with the second approach. Merging data to create and pass a single contact takes work and is error-...

View answer
1 answers
7 votes
5k views
Multi threaded circular buffer
Accepted answer
4 votes

I highly recommend Java Concurrency in Practice by Brian Goetz et al. This will give you a solid foundation for building multi-threaded Java applications. The biggest problem with RingBuffer is that ...

View answer
6 answers
16 votes
1k views
Multiple returns in a method or one return
4 votes

While I'm fine with multiple returns for longer methods or throwing exceptions from guard clauses, I would probably rewrite the code as below. I find it easier to think of the method as "get the ...

View answer
4 answers
21 votes
2k views
Possible improvements to Risk board game?
4 votes

To reiterate Bobby's point about using meaningful names, please consider spelling words with all their vowels in tact. This has several benefits: The code is much easier to read. You improve your ...

View answer
4 answers
6 votes
724 views
A character array of arbitary length with 'R', 'B', 'W' is needed to be sorted in that order
4 votes

One quick improvement to your solution would be to use switch(arr[i]) instead of the if-else chain. The only real problem I see beyond the complexity and non-obviousness of the algorithm is that it ...

View answer
1 answers
3 votes
998 views
How to implement a dynamic temperature conversion method based on given enums?
Accepted answer
4 votes

The java.util.concurrent.TimeUnit enum class provides both a generic conversion method and specific methods for each modeled unit, e.g., toHours. You could do something similar and still provide the ...

View answer
3 answers
0 votes
577 views
Tips to refactor code : very repetitive code
4 votes

For the code repetition you can use $(function () { // shorter form of document-ready $.each([dr1, dr2, dr3], function (index, data) { $('#drawing' + (index + 1)).lazylinepainter({ ...

View answer
1 answers
5 votes
5k views
Simple multithreading task
4 votes

The main issue here is that everything synchronizes on the restaurant object. This means that the chef cannot prepare food while the client is placing an order, waiters cannot serve while the chef is ...

View answer
4 answers
20 votes
17k views
Finding all k-subset partitions
4 votes

Here's the obligatory recursive version: def k_subset(s, k): if k == len(s): return (tuple([(x,) for x in s]),) k_subs = [] for i in range(len(s)): partials = k_subset(s[:...

View answer
2 answers
4 votes
4k views
PHP Bitmask class
4 votes

If you really want to drink the PHP Kool-Aid, you could implement the ArrayAccess interface so that clients could use $bf[UserPermissions_BitField::PERM_READ] = true; ... if ($bf[...

View answer
2 answers
6 votes
2k views
Optimizing WordWrap Algorithm
4 votes

I have some high-level advice based on a cursory read of your code. It would really help if you could refactor it into multiple methods as this function is too long and does too much. That won't help ...

View answer
3 answers
4 votes
282 views
Cycling between element above and below current array element
4 votes

The sequence you describe is +1, -2, +3, -4, +5, -6, ... so my idea was to keep two mutating values var step = 0; var direction = 1; where step is incremented and direction is negated step++; ...

View answer
2 answers
6 votes
287 views
Extract a joined result set into a parent-child hierarchy
Accepted answer
4 votes

First Cut: Since every row contains the same set of fields, you can clean up the code considerably by skipping the inner loop. $question_array = array(); foreach ($quiz_data as $row) { $qId = $...

View answer
1 answers
10 votes
111 views
Class that represents starting points and endpoints of a game
Accepted answer
3 votes

You can create EndPoint as a more specific version of MajorPoint and move the extra logic there. var MajorPoint = function(game, x, y, state, callback, callbackContext) { Phaser.Sprite.call(this, ...

View answer
2 answers
10 votes
1k views
PDO connection via INI parser classes
Accepted answer
3 votes

Parser The code as written solves the small problem of configuring a PDO connection via an INI file, but it won't scale to handle other connection types. As tim pointed out, IniParser barely ...

View answer
3 answers
5 votes
10k views
Binary search tree with tree traversal
3 votes

I try not to cover ground already touched upon by @janos's great answer except to emphasize formatting. You mostly do a good job with the indentation which is clearly the most important factor, but ...

View answer
3 answers
7 votes
238 views
Creating check parameter class instead of repetitive null checks
3 votes

Check out Guava's Preconditions helpers for some ideas, specifically checkNotNull. But first let's look at what you have in the first method: public boolean insert(User user, Boolean active) throws ...

View answer
2 answers
4 votes
71 views
Avoiding code duplication for attribute-removal code
Accepted answer
3 votes

Yes, you can either assign the function to a variable or name it. You can further clean it up (slightly) by storing the jQuery selectors. Finally, I've wrapped it all up in an IIFE to avoid polluting ...

View answer
2 answers
4 votes
4k views
Returning nested array with added key=>value
3 votes

PHP provides quite a few handy array-manipulation functions. This is a case for array_map which passes each value to a callback and assigns it to the same key in a new array. function add_tax(array $...

View answer
1 answers
2 votes
150 views
Does my controller code look good?
Accepted answer
3 votes

On the surface the code in your question seems fine, but I can tell you from experience that once this door is cracked just a bit, it will continue to creak open wider over time. "I'm just checking ...

View answer
1 answers
1 votes
443 views
Tag Cloud using 2 queries (one nested), and a foreach loop: Is there a better way?
Accepted answer
3 votes

Note: Your table and especially column names are quite verbose. I would remove the table name from each column as a first start and be more consistent with the names. I'll use better names below. You'...

View answer
7 answers
15 votes
8k views
Why does caching of string objects perform faster?
3 votes

Another aspect to consider is cache locality as the pool grows. The cost of allocating a small temporary string may be far less than an L1/L2 cache miss. You could randomly generate strings such that ...

View answer
2 answers
3 votes
62 views
HTML/Attrs Tester Environment
Accepted answer
3 votes

One problem I see is that moveVal is shared across every click, but I suspect you want to bind it to each addition so that removing it removes the approach damage. To do this you'll need to store each ...

View answer