10
\$\begingroup\$

Please post your nominations for the Best of Code Review 2015 - Night and Day category.

The most dramatic improvement (nominate both the question with the most hopeless code and the answer that cleans it up the best).

In your nomination post, please make sure to include a link to the nominated answer. Include a short explanation of what makes the suggestions in the answer exceptional.


Small characters at the bottom: Only one nomination per post, nominated answers must have a creation date in 2015, downvotes don't count, and Santa reserves the right to award the top-voted nominee pair a special bounty as a token of appreciation on behalf of the Code Review community.

\$\endgroup\$

8 Answers 8

6
\$\begingroup\$

Nominating Shorter cleaner technique of writing code with variables and lists? for the "night" and my answer for the "day". More because the night was so dark than the day is bright.

The code went from extremely long, almost all-in-one, function involving tons of copypasta and similarly purposed nameX variables to OOP approach involving a list of objects parsing their attributes. Reducing the length of the code by 83% while largely increasing readability.

\$\endgroup\$
1
  • \$\begingroup\$ +*copypasta*+ \$\endgroup\$
    – greybeard
    Commented Jan 10, 2016 at 6:57
4
\$\begingroup\$

Nominating myself: the "Night" was a weird busy loop involving the current time and an atomic long, as well as a vague specification. "Day" is atomically incrementing a long.

Generating a 64 bit unique integer in Java

\$\endgroup\$
3
\$\begingroup\$

Question: Find the Nth number divisible by only 2, 3, 5, or 7

Deceptive problem, where OP fell into the easy trap of thinking his solution was \$O(n)\$ simply because he was doing a constant number of operations per iteration. Additionally the code was mixing many responsibilities and could needed some love.

My answer provides both a cleaner way to solve the problem using his algorithm, but more importantly goes through and describes why his solution wasn't actually \$O(n)\$ and was actually exponential (with tables!) and then provides an actual \$O(n)\$ solution, complete with timing differences.

Exponential time to Linear time seems like night and day to me.

\$\endgroup\$
3
\$\begingroup\$

I'm personally against nominating mine own question/answer.
After some thinking on what a good candidate could be I remembered this one :

Question : Creating a numeric phone keypad

It's a very simple program to set text to digits on the phone.

The answer is just not only a code review but also inspect on performance.
There was a whole discussion about this one in the chat.
With working together in the chat we got to this great answer with a lot of benchmarks, where we can see on how simple things already could improve our performance.

While I'm only a small part of the solution, I'm not the only one who took part of this.
This answer also helped me succeeding a technical test for new work and it will always remember me that a team effort can create awsome code/answers.

\$\endgroup\$
1
\$\begingroup\$

@coderodde's project was posted as two questions: Discrete event simulation of a prioritized lunch queue in Java (Data structures) and Discrete event simulation of a prioritized lunch queue.

On the surface, it looked like very nice code:

Population population = 
        RandomPopulationGenerator 
        .withRandom(random)
        .with(15).peopleWithDegree(AcademicDegree.DOCTOR)
        .with(40).peopleWithDegree(AcademicDegree.MASTER)
        .with(100).peopleWithDegree(AcademicDegree.BACHELOR)
        .with(250).peopleWithDegree(AcademicDegree.UNDERGRADUATE)
        .withMeanLunchTime(10800.0)
        .withLunchTimeStandardDeviation(1200.0);

However, my answer pointed out that this cool-looking fluent interface had significant disadvantages. Moreover,

  • literal modelling of the problem hindered code reuse and understanding
  • statistics gathering was cumbersome
  • non-chronological progression of time necessitated a time-travel hack
  • it wasn't obvious that the simulation would terminate cleanly

An event-driven complete rewrite addressed those issues, and also reduced the code from ~1200 lines to ~400 lines.

\$\endgroup\$
0
\$\begingroup\$

Question: A little integer parser vs clean code

Not hopeless per-se, just a verbose solution to a simple problem from a beginner (we all have to start somewhere, don't we?).

(My) answer attempts to explain:

  • Why the problem is simple enough to warrant a simple answer,
  • How learning APIs is valuable for beginners,
  • an alternative solution,
  • and also provided additional advice if 'reinventing-the-wheel' in the question is a form of academic exercise.
\$\endgroup\$
0
\$\begingroup\$

Question: Sonar remarks make SQL unreadable?

This started off as a question about concatenating literal strings and how that can be improved to 'pass' Sonar.

(My) answer introduced a builder-like approach to the query construction, which is well-received by the OP.

\$\endgroup\$
0
\$\begingroup\$

Question: Writing a program to read a maximum of 99 elements in an iteration from a list

Relatively simple problem, but the code had major problems with comments and hard-coding.

(My) answer started by focusing on the comments, and then concluded with an alternative solution based nothing more than Java's built-in methods.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .