Greg Burghardt
  • Member for 7 years, 6 months
  • Last seen more than a month ago
Returning status codes from business layer
35 votes

Status codes have long gone the way of the Dodo --- made extinct by exceptions, actually. The problem with a status code is that it can be ignored. An exception cannot. Consider these two examples. ...

View answer
PHP Register Form - OOP
Accepted answer
15 votes

When approaching object oriented programming, I always try to adhere to a few best practices: Do one thing, and do it well Don't Repeat Yourself (DRY) Favor composition over inheritance There are ...

View answer
Calculate subtotals and totals from a form with many decimal values
Accepted answer
14 votes

Two words: loops and arrays. There a bunch of variable names that differ only by a number. Whenever you see code like this, you can probably clean it up by creating a collection of those things and ...

View answer
To-do list in jQuery
14 votes

Use CSS classes rather than inline styles. Instead of modifying the style of each TODO item, use CSS classes. This offloads the styling into a style sheet where it belongs, and it still allows you to ...

View answer
Refactoring multiple `else if` code
Accepted answer
14 votes

I know I'm late to the game, but each filter function contains essentially the same lines of code. Only the container element differs: function processFilter($container) { $container.find("li....

View answer
C# function to execute queries in a SQL Server database
13 votes

There are two problems with this that are immediately apparent: You are executing arbitrary SQL. This is a SQL Injection flaw. You'll want to use parameterized queries. You are catching an exception ...

View answer
Categorising a number in JavaScript
Accepted answer
13 votes

You might not need to loop through an object, and a simple switch statement would suffice. function getSize(number) { switch (true) { case number < 100: return "url_t"; case ...

View answer
Data item prototype in a Javascript data structure where object instance functions see own data
11 votes

Naming Things The class name dataItem doesn't communicate what it is, or how it is meant to be used: There are two hard things in computer science: cache invalidation, naming things, and off-by-...

View answer
Reading a line of text from a socket
Accepted answer
11 votes

I don't see anything in this code that needs to be commented. To echo what other people said, comments should clarify code and they should never say what the code already says. I would recommend ...

View answer
Creating a class library in JavaScript
10 votes

I'm glad to see I'm not the only one who finds "interfaces" in JavaScript useful. Yes, the language doesn't support interfaces, but there is value in defining them anyway if you expect third party ...

View answer
Title case a sentence function
8 votes

A regular expression is really all you need: /(^| +)([a-zA-Z])/g Then use the String#replace method and pass in a function: function toTitleCase(s) { return s.toLowerCase().replace(toTitleCase....

View answer
Case Study - Basic sales tax
Accepted answer
8 votes

I think part of the problem was that your object model didn't implement the proper Separation of Concerns. Each product object was responsible for knowing whether it was imported, and if it was ...

View answer
"Guess a random letter" game with command line interface
7 votes

You are using exceptions for flow control, and that is considered an anti-pattern. Exceptions are meant for unhandlable conditions that would otherwise corrupt your application. Winning the game ...

View answer
Slim API with 3 layer architecture
Accepted answer
7 votes

I've seen this layering before, in fact I'm working on a .NET project with this exact layering (DataAccessLayer and BusinessLayer). The BusinessLayer object does absolutely nothing. I've seen this ...

View answer
Displaying a row of buttons
Accepted answer
7 votes

This can be shortened up quite a bit. First, you have presentation logic intertwined in your ASP code, which is the main cause for the code repetition. Shortening this code and DRY-ing it up involves ...

View answer
Navigating to a view
Accepted answer
6 votes

I think Bethan has the best approach, but you could take it one step further and use a generic Type: public void OnNavigatedTo(NavigationParameters parameters) { if (!IsNavigationFrom<...

View answer
Scroll Magic Multiple Scenes
Accepted answer
6 votes

I'm posting this with an assumption that the JsFiddle is missing something since it throws a "setTween is not a function" error. But given your code... What you really want is a factory object, which ...

View answer
Is it an anti-pattern to let ViewModel fill itself from a domain object?
Accepted answer
6 votes

It's not an anti pattern. At some point, one layer must know about the layer underneath. You also don't want to rewrite view-model mapping code in multiple controllers or actions, nor do you want to ...

View answer
Scaling jsTree performance
6 votes

I don't think you'll find a single line of C# or JavaScript that will make this run faster. You need lazy loading. When the page first loads, load just the first two levels. Then clicking on a level ...

View answer
Managing many availability status
5 votes

This method feels like it should be its own class, and the arguments to the method should be constructor arguments. This is really where a "service" class can clean things up, because this method is ...

View answer
Rails validating API parameters
5 votes

You've essentially created a "View Model" for your API requests, which there's nothing wrong with doing that. Sometimes you want to abstract away your internal Domain Model and not have it exposed as ...

View answer
Login validator class
5 votes

My first impression is that this class is doing too much. It is calling the database and manipulating the user interface. This needs to be separated. Exceptions are being swallowed and shown to the ...

View answer
Rendering a person's time clock record in HTML, with many color-coded conditions
5 votes

As usual, I'm a day late and a dollar short. :) You actually have a good case for using a view model. All of the ifs would be methods in your view model to return the proper CSS class for your <td&...

View answer
cURL based REST client library
Accepted answer
4 votes

When I look at the RestClient class, what is required to make a request? $client = new RestClient(); $client->get("http://codereview.stackexchange.com"); At first glance, this appears all I need ...

View answer
One program using three functions to compare two double-precision numbers
4 votes

Echoing others, your similarValues function should be returning a boolean. Calling it areSimilarValues reads better and is more descriptive. The two arguments are called smallerValue and biggerValue, ...

View answer
DatabaseManager
4 votes

The only thing I can add is that methods returning a boolean value are generally written as properties in C#. public bool connectionOpen() { return this.connection.State == ConnectionState.Open; }...

View answer
Avoid printing attributes of the nil object in Rails
4 votes

You need to create a delegate method on your Person class: class Person def team_name team.name unless team.nil? end end Then it's just a simple: <%= @person.team_name %> If you have a ...

View answer
What kind of Wall should it be? (for an RPG)
4 votes

Since there is a one-to-one relationship between wall patterns and tile types, model that in your enums: public enum WallPattern { //0 is empty //1 is wall //2 is optional, can be empty ...

View answer
Prompt user for input then create an acronym from it
4 votes

The procedure name acronym does not tell you what this procedure is doing. Naming it create_acronym would make the code more obvious. Error checking: You assume that the user only enters a single ...

View answer
Structuring a HTML5 game without a framework
Accepted answer
4 votes

Dependency injection will help, but not solve all the architectural issues. Whenever I embark on a large new project, I first answer this question: Where does my application start? It's a simple ...

View answer
1
2 3 4 5