Questions tagged [comparative-review]

This tag is used for questions which present a problem and multiple solutions, where the asker wishes to know which solution is best (and why).

Filter by
Sorted by
Tagged with
39 votes
10 answers
9k views

Which FizzBuzz is better, and why?

I was in an interview and the guy asked me to do the typical FizzBuzz question of printing numbers 1-100, but for each factor of 3 print Fizz, each factor of 5 print Buzz, and each factor of both ...
arc4randall's user avatar
36 votes
3 answers
11k views

Ensuring user input is an integer in a range

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed ...
Malachi's user avatar
  • 28.6k
33 votes
6 answers
3k views

To 'this' or not to 'this'?

I was given a homework and I have 2 solutions: one that uses this and other one that doesn't. I tested both solutions on jsPerf but sometimes it says the version ...
Rahul Desai's user avatar
30 votes
9 answers
58k views

Check a string to see if it is a palindrome

I have written two code pieces to check if a given string is a palindrome: ...
Rohit's user avatar
  • 1,066
30 votes
3 answers
2k views

Java method - levels of abstraction

In his Clean Code book, Robert C. Martin states that functions should do only one thing and one should not mix different levels of abstraction. So I started wondering whether the code below meets ...
cafe_'s user avatar
  • 465
27 votes
4 answers
102k views

Filtering a list of vertices that lie inside a cylinder, with and without LINQ

Since I don't know how LINQ works under the hood, I can't decide what version is best to use in term of rapidity of execution. I've done some testing with my testing data (Point Cloud) but I can't see ...
Jean-François Côté's user avatar
23 votes
11 answers
11k views

Find nth Fibonacci Number, using iteration and recursion

I'm a beginner programmer and I came upon this problem which is to find the n​th number in the Fibonacci series. I used to solve the problem using a for loop; ...
David Peterson's user avatar
23 votes
5 answers
2k views

When a constructor calls a helper function, should the helper mutate the object or return part of the object?

I have a private method that is called from the constructor to instantiate a bunch of objects. I can do it two ways. ...
dwjohnston's user avatar
  • 1,326
19 votes
2 answers
559 views

Count the occurence of nucleobases in DNA string

Inspired by this meta question I decided to take a look at Rosalind. Their first challenge seemed easy enough: An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', ...
Mast's user avatar
  • 13.3k
18 votes
4 answers
11k views

20 Ways to do Random Sampling

Here is a (small project) code for practicing Python, it comprises 20 variety of functions that each works to return \$n\$ random samples from a set of data. The code mostly uses the ...
Redsbefall's user avatar
  • 1,132
18 votes
11 answers
1k views

Reversing a string

I had this as an interview question, and the interviewer pointed this out. Here's what I wrote: ...
Billy ONeal's user avatar
  • 1,470
18 votes
1 answer
3k views

Subclassing pathlib.Path

This arises from the SO Question subclassing pathlib.Path. The pathlib sources show that Path, when invoked, selects one of ...
Carel's user avatar
  • 301
17 votes
3 answers
2k views

Shoe shine shop model in Rust

I'm learning Rust and a few questions did arise during translation of my C++ code to Rust. There are comments in Rust code I'd like to be answered. Is there an idiomatic way to solve this task? The ...
rogday's user avatar
  • 403
16 votes
4 answers
3k views

Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

A friend of mine recently started learning R, and she complained to me that R is very slow. She was working on Project Euler Problem 7, which asks for the value of the 10001st prime number. For ...
castle-bravo's user avatar
16 votes
4 answers
2k views

Get the first item in a sequence that matches a condition

I've been trying to figure out what's the best, most Pythonic way to get the first element of a sequence that matches a condition. Here are three options: I don't like that I've written ...
deepyaman's user avatar
  • 263
16 votes
4 answers
36k views

Split Collection to two list depend on criteria

Let's say I have a List contains numbers: 1,2,3,4,5,6,7,8,9 and I would like to split this to two lists: odd numbers even numbers So I can do this two ways: Solution 1: Create two methods: <...
chris's user avatar
  • 415
16 votes
3 answers
4k views

On Knuth's "Algorithm L" to generate permutations in lexicographic order

I needed a method to generate all permutations of given elements, so I decided to implement "Algorithm L (Lexicographic permutation generation)" from Donald E. Knuth, "GENERATING ALL PERMUTATIONS" in ...
Martin R's user avatar
  • 23.2k
15 votes
3 answers
2k views

Java and C++ stack implementations with dramatically different performance

I'm developing a small programming language for use in any project I have where I feel a small scripting language could be used well. I've written two emulators for the language, one in C++ and one in ...
J_mie6's user avatar
  • 479
15 votes
2 answers
2k views

Message-formatting code with pluralization, revised to be more functional

I'm a C++ dev, and I've recently started working my way through Clean Code*. Whenever I encounter an example I think I could improve, I try to re-implement it in C++. On pp. 28-29 there is an example ...
SG_90's user avatar
  • 183
15 votes
4 answers
7k views

Generating formatted multiplication tables in C++

I am self studying C++ from Robert Lafore's OOP in C++. I am solving the first exercise questions of chapter-3 loop and decisions. I've written three different codes using different approaches. I want ...
user31782's user avatar
  • 251
14 votes
8 answers
29k views

Two FizzBuzz solutions

Below are two solutions to the FizzBuzz problem in Python. Which one of these is more "Pythonic" and why is it more "Pythonic" than the other? Solution One: ...
Thomas Owens's user avatar
14 votes
3 answers
6k views

Unit tests for testing error codes

Both unit tests pass and they both test the same thing. The production code uses the right error codes. System Under Test (SUT) ...
Raj's user avatar
  • 320
14 votes
2 answers
27k views

Flatten an array - loop or reduce?

Problem: Concat all sub-arrays of a given array. Example input: [[0, 1], [2, 3], [4, 5]] Example output: [0, 1, 2, 3, 4, 5] ...
Erel Segal-Halevi's user avatar
14 votes
2 answers
714 views

Get the encrypted payload from an unencrypted wrapper PDF document

This is code, written by our dev team, and SonarQube tells me that the Cognitive Complexity is too high: 21, and the current (default, out of the box) metric limit is 15. I've had a go at reducing the ...
Amedee Van Gasse's user avatar
14 votes
2 answers
21k views

Forking with PHP (4 different approaches)

I thought about three (now four) different ways how to execute forks with PHP. What is the faster, maybe better solution and why? PHP script included ...
Aley's user avatar
  • 261
13 votes
5 answers
52k views

Simple countdown programs to print "3, 2, 1, Action!" using while loop

My program is a simple while loop making a countdown starting from 3 and shouts "Action!" upon reaching zero and then breaks. I would like to know which of the following ways is better in means of ...
U.Marvin's user avatar
  • 131
13 votes
6 answers
2k views

"Do you owe the government?" classes

Should my class pass parameters internally or reference class level scoped variables? I'm not sure on the best approach or style for procedure calls that have parameters. Should I go with the class ...
Rob's user avatar
  • 347
13 votes
6 answers
946 views

Assign a variant to a variant

Quick review: I've been writing API code which receives user input that can come as either an object or a value type - a problem outlined here. In order to write an unknown data type to a variable, ...
Greedo's user avatar
  • 2,345
13 votes
1 answer
1k views

Functions to hide and reclaim first visible publication on a page using Selenium

I have the following functions in JavaScript: ...
Yu Zhang's user avatar
  • 707
12 votes
4 answers
6k views

Extraction of value from a dict-tree, given a path of arbitrary length

Problem: We read a complex json-file which grants us a tree in terms of a python "dict of dicts". We're then requested to retrieve a specific path from the tree, represented by a list of elements. The ...
Wololo's user avatar
  • 223
12 votes
3 answers
7k views

Parsing an email string

My program needs to parse an e-mail string. There are two possibilities to enter an e-mail-address. Either with the alias or without one, plain the e-mail-address. 1st possibity: ...
Matthias Burger's user avatar
12 votes
2 answers
1k views

Calculating Fibonacci sequence in several different ways

I'm doing an experiment trying to freshen up my unit-testing and learn dynamic programming. Every test passes but I'm curious of the result of some of them and worried if I'm doing the testing ...
JsonDork's user avatar
  • 414
12 votes
2 answers
701 views

Printing removed items using lambdas and streams

I'm struggling to make my lambdas readable. I've seen various approaches. Below are three different examples that all do the same thing. Forgive the example: I suspect there are better actual ...
David Lavender's user avatar
12 votes
3 answers
639 views

Becoming the greatest tower builder

I am trying to learn how to write proper code in python, and stumbled upon the following problem. Bob is given a set of building blocks b, and a number ...
N3buchadnezzar's user avatar
12 votes
2 answers
17k views

Get values from Web service asynchronously

I am working on creating some libraries for a project at work and I wanted to make sure I have this pattern correct. Assuming that the GetWidgets() method is what I ...
Schandlich's user avatar
12 votes
2 answers
17k views

Packing and unpacking two 32-bit integers into an unsigned 64-bit integer

Does this look good for combining/retrieving two 32 bit integers (a type and an index) into/from an unsigned 64 bit integer (a unique ID)? The point is to hide in the API the composition of the ...
JDiMatteo's user avatar
  • 223
11 votes
4 answers
542 views

Function to format the local time

This is a statement that I use a lot in my code base: (LocalDateTime.now()).format(DateTimeFormatter.ofPattern("ssmmHHddMMMuu")) Should I create a static method ...
RajKon's user avatar
  • 229
11 votes
4 answers
2k views

Generating the sequence of the digits that appear in the sequence of natural numbers

This is a revisit to a question already asked here about a year and a half ago. Project Euler's Problem 40 involves generating the sequence of digits that appear in the natural numbers, all ...
Apalala's user avatar
  • 213
11 votes
4 answers
9k views

Trimming fgets(): strlen() or strtok()

After a successful fgets(buffer, ...), it is often desirable to trim a potential End-of-Line '\n'. Of the following 2 methods, ...
chux - Reinstate Monica's user avatar
11 votes
2 answers
17k views

Semantic markup for card blocks

I'm using Bootstrap. I'm doing my best to work with more semantic markup and getting away from divitis. Several of these "card" blocks appear on a page, like this. Original: ...
user avatar
11 votes
1 answer
512 views

Sequence-based enumeration of permutations in lexicographic order

This is follow-up on On Knuth's "Algorithm L" to generate permutations in lexicographic order, where I presented the following method to enumerate all permutations of array elements in ...
Martin R's user avatar
  • 23.2k
11 votes
1 answer
234 views

Counting times a file was committed to Git

A long time ago I implemented some code to walk the git commit graph. I recently updated the accompanying analytics part of the code base to follow file moves/renames in order to get a more accurate ...
RubberDuck's user avatar
  • 30.8k
11 votes
1 answer
436 views

Testing a list of proxies for validity concurrently

I have a class that tests a list of proxies for validity concurrently, which I have created in 2 different ways, one utilizing Parallel.ForEach and the other using a TPL Dataflow ActionBlock. I am ...
JohnWick's user avatar
  • 425
10 votes
8 answers
13k views

Calculate square of a number without multiplication

I wrote these two methods to calculate the square of a number: ...
user avatar
10 votes
6 answers
5k views

Length of a C string, capped to some maximum

If I have a oneliner in C such as size_t maxSizeOf(const char *s) { return strlen(s) > M_MAX_SIZE ? M_MAX_SIZE : strlen(s); } would it be better to have ...
Tom's user avatar
  • 567
10 votes
4 answers
5k views

Sum Square Difference, which way is more Pythonic?

I'm using python 3 and I am creating an algorithm to find the sum square difference for the first 100 (or 1 to x) natural numbers. This happens to be project euler problem 6 if anyone is wondering. I'...
takethelongsh0t's user avatar
10 votes
5 answers
4k views

"Of three points supplied, is one a midpoint of the other two?"

Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; ...
cody.codes's user avatar
  • 1,935
10 votes
6 answers
3k views

Getting filtered autolist

Is there a better way to write the following code fragment? I can't have more than 10 conditional statement in my method as it gives cyclomatic complexity. ...
Zeeshan's user avatar
  • 203
10 votes
4 answers
2k views

Shuffling multiple iterators in order

I have a Python function w/ specified functionality, and two proposed approaches - which do you prefer, and why? (performance, simplicity, clarity, etc) (both use ...
OverLordGoldDragon's user avatar
10 votes
4 answers
2k views

Title case a sentence function

I am new to programming. I have written a solution in two different ways, but would like to know what is considered a better solution, and why. Additionally, in terms of performance, why would one ...
Frenchiejnr's user avatar

1
2 3 4 5
19