David Harkness
  • Member for 10 years, 11 months
  • Last seen more than a month ago
JSX render method in react
0 votes

While Drew and Peter provide some useful techniques when dealing with complex components, this case cries out for the simplest solution possible. render () { if (showContainer) { return ( ...

View answer
Extracting elements from multiple sorted lists efficiently
2 votes

\$O(k + \log n)\$ seems highly suspect to me. For large values of \$n\$ this is equivalent to \$O(\log n)\$ while for small values it's equivalent to \$O(k)\$. That they specified \$O(kn)\$ up-front ...

View answer
Simplifying a repetitive event handler
Accepted answer
9 votes

Yes! Introduce a keyCode variable to hold the one difference between the two otherwise identical code blocks. var keyCode = isNodeWebkit ? 113 : 112; $(document).keyup(function (e) { if (e.which =...

View answer
Decoding name/value pairs
Accepted answer
6 votes

Implementing Iterator is the way to go in my opinion. As for your question, array_map would allow you to decode the values but not the keys. But you can combine that with other array operations to get ...

View answer
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
Unit testing a click event with asynchronous content
Accepted answer
6 votes

Without the real code to be tested, I can only provide an example of how I've tested something similar with QUnit and Sinon.JS, though you don't even need the latter. You can either use Sinon.JS to ...

View answer
Inefficient hash map operation provokes OutOfMemory: Java heap space error
1 votes

Deduplication If you're reading in a file with a lot of repeated sentences, you could force them to be unique and hold only one instance of each unique sentence. Combining this with storing the ...

View answer
Creating chat commands properly with Socket.IO
Accepted answer
5 votes

You can gain a lot of readability by formatting the code (every decent editor will do a passable job at this with a single keystroke) and extracting several small functions (refactoring). I'll start ...

View answer
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
Random walk terrain generator
Accepted answer
4 votes

Style Variables and functions (except constructors) in JavaScript are normally written in camelCase with a leading lowercase letter. It's not mandatory, but you'll find it easier to work with others ...

View answer
Graph (adjacency list) for BFS and DFS in Golang
2 votes

I don't know Go so I'll limit this to a more general review. If I recommend non-standard things, please point them out so someone else doesn't pick up bad habits. :) Unless the error result is always ...

View answer
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
Rendering a person's time clock record in HTML, with many color-coded conditions
18 votes

That's a big mess of conditionals. Hopefully they all match the comments near them, though given typical code that's likely not the case. You are repeating a lot of HTML code that could be addressed ...

View answer
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
Necessary singleton in DAO
Accepted answer
2 votes

Double-checked locking is broken and must be avoided. The article explains the reasons in depth and provides several alternatives. In this case you can follow the advice in the "Making it work for ...

View answer
"Remove Friends" problem time efficiency too slow
1 votes

You've missed several variable renamings which keeps your code from compiling, and those names were truly atrocious! p, c, fD? Create more descriptive names as an opportunity to practice your touch-...

View answer
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
Database schema for holding game save files
6 votes

I've found over the years that it's best to start with the normalized form and denormalize as-needed to solve performance and usability problems. Without a full description of what troop_class stores, ...

View answer
Create thumbnail in the original ratio without empty space
Accepted answer
5 votes

The first thing that's really needed are some comments describing the different conditions. Sure, you can work them out every time you read the code, but that's error-prone busy-work that you can ...

View answer
Implementation of merge sort and bubble sort
1 votes

Not Go-specific since I've only skimmed the syntax, but good advice in any language, you should refactor main to extract several methods: readInteger(description, min, max) to prompt for, read, and ...

View answer
Proper prototypal programming with Node.JS
Accepted answer
8 votes

The Basics As you said, I think as each handler grows in size and the base handler adds more common features, the prototypes will pay off in spades. Honestly, the biggest readability issue for me is ...

View answer
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
Iterator for a list of objects
5 votes

I'm going to hit the high notes on my phone and swing back later with details unless someone beats me to it. Allow null Add a separate boolean field to track when next holds the actual next element ...

View answer
PHP Trait Singleton
Accepted answer
2 votes

Contract Enforcement There are a few ways to violate the singleton contract, but none need to be guarded against. Create a subclass with a public constructor. This obviously is pilot error. Use ...

View answer
Comparing pivot choosing methods in quicksort
Accepted answer
5 votes

Sorting the Three You only need at most three swaps to sort the three elements: at most two swaps to place the smallest element into the first slot since it must be compared to the other two, and at ...

View answer
Catch-style unit testing in JavaScript (phase 1)
2 votes

Overall Design For such a small amount of code, I find the logic extremely difficult to follow. I understand what is happening at a high level, but it seems every function is responsible for managing ...

View answer
Git script that fast forward merges and deletes the old branch
2 votes

Here are a few more simple improvements: Quote your branch names in case someone gets creative with Unicode. I don't think you need to quote $# and $? since they are integers, but I did so below just ...

View answer
Script to convert Shift + Spacebar into four spaces on SE
Accepted answer
5 votes

You can use charAt to grab the preceding character and check it against a space, newline, or empty string (the case when the cursor is at the start). But let's not stop there. white and pad are okay ...

View answer
Extending Object.prototype in JavaScript
1 votes

You could define it on Object itself instead of adding it to the prototype and exposing it to every object. This would parallel how Object.keys works: Object.defineProperty(Object, 'removeMatching', {...

View answer
Rendering tab data
Accepted answer
5 votes

You can combine these using a helper function with a closure. function bindTabClickToSetPageContent(selector, content) { $(selector).click(function () { $("#page").html(content); $...

View answer
1
2 3 4 5 6