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
15
votes
2answers
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 ...
12
votes
2answers
982 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 ...
2
votes
3answers
127 views

Finding the next available filename

We're having a small debate in work about the following piece of code: ...
0
votes
1answer
24 views

Imperative and functional-programming solutions for Jumping on the Cloud challenge

Problem Statement Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud ...
8
votes
2answers
76 views

Function invocation every second time

Recently I got test task at the project, it is fairly simple: ...
4
votes
2answers
63 views

Displaying IP addresses in Go using Stringer interface

I am trying out some Go code examples (while coding a private project) to get more "in-depth" knowledge about the language. I have come across an exercise from the Go Tour website about displaying ...
5
votes
1answer
85 views

EEPROM-based filesystem, aiming for Misra C++ 2008 compliance

I'm writing code for embedded systems (IAR compiler) and it should adhere to Misra C++ 2008. This limits the available features of the language quite a bit: no C++1x features use of the standard ...
0
votes
1answer
72 views

Adding a Node to the Linked List is taking a longer Time when using a reference to the Tail Node

I am trying to add two numbers in the form of linked List and return their result in a Linked List as given in https://leetcode.com/problems/add-two-numbers/ Question: You are given two non-empty ...
7
votes
1answer
85 views

Function to send email, refactored based on SOLID principles

I have the below method that is responsible for generating and sending a MailMessage using a SmtpClient object: ...
3
votes
1answer
41 views

Evaluating π using Monte Carlo methods - Serial vs OMP

I wrote this simple code for evaluating the π using Monte Carlo method. This is the serial version: ...
3
votes
3answers
78 views

Find minimum value in a BST: using boolean operators, and using conditionals

I've been studying the BST code in Paul Graham's ANSI Common Lisp. He provides (my comments): ...
5
votes
1answer
121 views

Detecting the presence of multiple URL segments

I have two versions of a function that performs the same task, however I'm not sure which one to use. Speed is something to take into consideration, but I also want to know what the best practice is. ...
1
vote
1answer
90 views

Two solutions to 2-sum

I have written a solution for two sum problem in variation. first is simple with two loops and another one is one loop and hash map. But I wonder why the simple solution is faster than the efficent ...
3
votes
1answer
70 views

Pre-calculate the winning combinations for an n-sized tic tac toe board using C++ functional programming

This code pre-calculates the winning combinations for an n-sized tic tac toe board. I first created my function using an imperative approach. This is just how I naturally write code most of the time....
0
votes
1answer
67 views

One try with multiple catch blocks vs nested try

I am creating program which will parse .csv file and I am using FileReader and Scanner classes from java API. ...
2
votes
1answer
4k views

Substring vs Split performance test

Some people are telling me that using string.Split is very inefficient since it creates a lot of arrays in the heap. So, what are the alternatives if its real? The challenge? Taking a large string ...
4
votes
3answers
183 views

SkipLast of an IEnumerable<T> - Linq Extension

As my answer to this question, I came up with this solution: ...
2
votes
1answer
45 views

Nodejs functions to load and save RSA keys

I'm creating my first real life Nodejs project. I've created some files and all work, but I'm not satisfied about what my files look likes. It's unreadeble. For example, I work of a file to manage ...
3
votes
3answers
1k views

Extracting the longest line from standard input

I'm reading the K&R book and I'm interested in a program which finds the longest line on the input. I've written the same code as in the book, but I wanted to do it myself. I tried to write my own ...
1
vote
1answer
35 views

Aspect-oriented vs explicit style in Python [closed]

Readability counts, but is very subjective. The following snippets are equivalent in functionality and turn a generator iterator into a chunked HTTP response in Flask. Which of those two patterns is ...
4
votes
1answer
100 views

Two ways of implementing a combat system

Below is a segment of a game I am writing to stay in practice with my Java. EDIT The class this method is in is the Encounter class. This class just manages the encounter between the player and an ...
6
votes
1answer
795 views

Avoid using C Strings on C++ code to trim leading whitespace

I'm learning C++ and wrote a function to remove all the spaces and tabs at the beginning of the input string. It removes them until it find a character different of space and tabs. In the first ...
3
votes
2answers
68 views

Optimizing Luhn check digit algorithm

The internet as a whole and Code Review in special already provide a decent amount of implementations of the Luhn check digit algorithm. They often follow a relatively "naive" strategy, in that they ...
2
votes
1answer
32 views

Two ways to implement DND type abilities

I am creating a game in Java that uses the DND model for a lot of its components. I've hit a roadblock when it comes to creating and implementing abilities. I have ...
3
votes
1answer
81 views

Most efficient way to check if user is allowed to update an object

So I have an user which can create his own Locations. I implemented a webservice in order to add and update locations. If the user is not the owner of the location i want to throw an appropriate ...
2
votes
1answer
67 views

Fetching configuration or default value

I have two code snippets, doing exactly the same thing and both get the job done: use defaults use from config Which is cleaner to use in golang and why? first option: ...
4
votes
1answer
21 views

Showing a “back-to-top”-button after scrolling a certain amount

I have a "back to top"-button on the page, It should be visible after the user has scrolled a certain amount*. This means I have to add or remove a class active ...
4
votes
1answer
131 views

Object-Oriented Breadth-First Search Implementation

My goal is to write a GUI application where a user can create a maze and select an algorithm. The passage of the algorithm from a selected start and end point should be visualized. The following ...
0
votes
0answers
50 views

Merge Two Sorted Lists in JavaScript

The task is taken from LeetCode Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. ...
2
votes
1answer
51 views

Populating dropdown lists, using SQL vs. using expression trees and reflection

I'm working with a large enterprise SQL Server database with dozens of tables that are used mainly as lookups to populate dropdown lists, etc. These tables all follow the convention of having (table)...
11
votes
1answer
330 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 ...
5
votes
0answers
92 views

Java multithreading many readers one writer implementations

I have a shared data object with writer threads and reader threads. Number of threads can read from the shared data at the same time (in the code getmethod), and ...
2
votes
1answer
57 views

Length of the longest consecutive run of 1s in its binary representation

The task Given an integer n, return the length of the longest consecutive run of 1s in its binary representation. For example, given 156, you should return 3 My function solution 1 Style 1 ...
0
votes
1answer
31 views

Filling in the card expiry date from transaction data

I'm integrating my app with a payment gateway, and have the following code which returns an array to populate the transaction info: ...
3
votes
2answers
116 views

Evaluation of a variation of quick-sort (pivot-selection)

Here is a variation of quick-sort where-in the pivot selection is based on calculating the average of the values of the highest and lowest numbers. ...
4
votes
1answer
107 views

Bridge-Pattern or Entity-Component-System

I want to learn how/when/why to use the GoF Design Patterns. These last days are dedicated to the Bridge Pattern, which means: Decouple an abstraction from its implementation so that the two can ...
-3
votes
3answers
126 views

Python program to find the first missing positive integer in a list

This is a Leetcode problem: Given an unsorted integer array, find the smallest missing positive integer. Note: Your algorithm should run in O(n) time and uses constant extra space. ...
2
votes
2answers
70 views

Python program for Jump game II

This is a Leetcode problem: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length ...
14
votes
5answers
22k 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 ...
5
votes
1answer
79 views

Container with most water

The task is taken from leetcode Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of ...
2
votes
1answer
2k views
3
votes
1answer
79 views

API for multiple printer attributes [closed]

I have network printers which have different attributes (e.g. supported network protocols, languages, status, print modes). ...
7
votes
1answer
201 views

Usage of TPL vs Parallel.ForEach() on file processing

i've been working on an implementation of a my own from scratch set of library Astron and I wanted to get my TPL usage reviewed because i'm not confident with this technology :/ My app is currently ...
4
votes
2answers
85 views

Create a min stack

The task is taken from leetcode Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes ...
3
votes
3answers
46 views

Counting duration values

I have a durations as such: [{'duration': 3600}, {'duration': 3600}] And I want the output to be ...
4
votes
1answer
166 views

Bubble sort algorithms and unittest in Python

I am quite new in Python and I am starting my journey with sorting algorithms, PEP8 and the Zen of Python. So far i wrote a post BubbleSort and I drew conclusions and followed the advices. I ...
6
votes
1answer
1k views

Scroll animations - setInterval vs. setInterval & requestAnimationFrame

I've recently made a slightly similar question with respect to the rendering performance, however my question this time is a bit different and with respect to which kind of JavaScript approach is the "...
4
votes
1answer
158 views

Return all valid paren strings with a given length

The problem is Given a non-negative integer n, return all valid parentheses strings with n open parens and ...
8
votes
1answer
204 views

Hash-optimization and Wilson-maze generation algorithm

I've finished optimization of Wilson algorithm (maze generation) from "silly and slow" algorithm of choosing unvisited cells: ...
6
votes
5answers
564 views

Checks user level and limit the data before saving it to mongoDB

I have a function that checks user level and limits the data before saving it to mongoDB database (pre 'save' middleware for mongoose). It have been getting complexity warnings and tried to rewrite it,...