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
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
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
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
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
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
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
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
Spliterator implementation
2 votes

From my reading of the Spliterator documentation, your code looks like it should handle parallel usage. Each list of URLs is owned by exactly one spliterator while they all share the thread-safe set ...

View answer
Geographic search
6 votes

If after adding the index on latitude and longitude you still see poor performance, trim the query down to the bare minimum to isolate the source. Remove all of the joins and the where clause ...

View answer
FileWriter supporting writing to multiple files
5 votes

Given that FileWriter works solely with files, you can drop File from the public method names leaving Open and Close, assuming these aren't reserved in vba. It looks like the built-in Print ...

View answer
Series of repetitive content-loading functions
2 votes

Joseph cleaned up the code a lot so let me point out a few things that you'll be able to use in your future code. Consistent formatting greatly improves code readability and maintenance. There are ...

View answer
1
2 3 4 5 6