# 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).

934 questions
Filter by
Sorted by
Tagged with
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 ...
• 581
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 ...
• 28.6k
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 ...
• 799
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: ...
• 1,066
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 ...
• 465
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 ...
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; ...
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. ...
• 1,326
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', ...
• 13.3k
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 ...
• 1,132
1k views

### Reversing a string

I had this as an interview question, and the interviewer pointed this out. Here's what I wrote: ...
• 1,470
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 ...
• 301
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 ...
• 403
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 ...
• 591
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 ...
• 263
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: <...
• 415
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 ...
• 23.2k
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 ...
• 479
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 ...
• 183
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 ...
• 251
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: ...
• 561
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) ...
• 320
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] ...
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 ...
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 ...
• 261
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 ...
• 131
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 ...
• 347
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, ...
• 2,345
1k views

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

I have the following functions in JavaScript: ...
• 707
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 ...
• 223
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: ...
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 ...
• 414
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 ...
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 ...
• 5,695
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 ...
• 223
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 ...
• 223
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 ...
• 229
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 ...
• 213
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, ...
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: ...
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 ...
• 23.2k
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 ...
• 30.8k
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 ...
• 425
13k views

### Calculate square of a number without multiplication

I wrote these two methods to calculate the square of a number: ...
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 ...
• 567
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'...
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; ...
• 1,935
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. ...
• 203
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 ...
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 ...
• 203