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).
937 questions
1
vote
1
answer
74
views
How can I refactor for handling multiple proto generated service clients by eliminating double await and the DRY code?
I'm having doubts about which is the best strategy to manage the many service clients in this web app.
"Best" in terms of a good compromise between user's device RAM and Javascript execution ...
1
vote
1
answer
253
views
Changing an element in a list when a predicate holds
My current project is a Settlers of Catan server in Haskell. I often manage to achieve my goals within Haskell, but the code is getting messier as the project grows.
My question is about the ...
1
vote
0
answers
55
views
Asking for feedback, when utilizing Pandas, why is it better to use pandas.Series than lists data type?
I found out the hard way, when utilizing pandas is better to use pandas.Series than lists data type to manipulate data.
Even if ...
2
votes
3
answers
223
views
Sort an array of integers with a hashset
I was trying to make a sorting algorithm for an array of integers. Here's are the steps/theory:
It turns an array into a HashSet, iterates over every integer value ...
0
votes
1
answer
73
views
need some guidance for better coding practice [closed]
Given a int optimumMemory and int[] array with list of available memories find the pair of indexes of memories whose sum of the values will be equal to the optimumMemory?
If there exists more than one ...
1
vote
0
answers
34
views
Spectrograph using array-of-slice-references
I've written a working and complete proof of concept that shows a spectrograph in Matplotlib. I want to nail down this proof of concept before I continue with development, and I'm not thrilled with ...
3
votes
2
answers
370
views
Debounce function that limits the rate a callback is triggered. Which implementation is better, and why?
The Debounce technique allow us to “group” multiple sequential calls in a single one - The debounce() function forces a function to wait a certain amount of time before running again.
I wrote both <...
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 ...
2
votes
1
answer
2k
views
C++: swapping bytes of an uint64_t
This question is about swapping bytes of an uint64_t value, or in other words, about converting between endianness of an integer value:
...
1
vote
1
answer
404
views
Perform various actions on a list
I am solving the following HackerRank problem:
Consider a list (list = []). You can perform the following commands:
insert i e: Insert integer e at position i.
<...
2
votes
0
answers
42
views
Which of my string indexing functions in Kotlin is semantically closer to my Java one?
I have a working string indexing method in Java and did convert in Kotlin.
All methods shown below produce the same result but as I am really new to the Kotlin language I wanted to ask which of my two ...
9
votes
3
answers
3k
views
Two ways to remove duplicates from a list
The question is - Write a program to remove the duplicates in a list
Here's how I did it-
...
1
vote
1
answer
59
views
Calculate how many days to add to a process based on destination country or state
I am refactoring a function that calculates how many days to add to a process based on destination country or state.
The original function called the database twice, even though the database table ...
3
votes
1
answer
229
views
Text centered in a circular component
I have surrounded my initials with a circle. This is intended to be a re-usable component in a responsive layout. (When it is re-used the text will always be two letters, and the font won't change. It ...
5
votes
2
answers
260
views
Solution discussion on the best way to reverse a number in Python
I'm practicing some python exercise and I have some doubts about the solution of reversing a number. Example, 42 -> 24, -314 -> -413.
The book has this solution:
...
3
votes
1
answer
243
views
LeetCode 1115: Print FooBar Alternately
I'm posting two solutions for LeetCode's "Print FooBar Alternately". If you'd like to review, please do. Thank you!
Problem
Suppose you are given the following code:
...
3
votes
0
answers
237
views
React Higher Order Components - export wrapped component or wrap it in parent?
I've been learning about about how to use context, and how to make a higher order component to provide context values through props.
So, I've created my HOC to pass down the props, and it looks like ...
3
votes
2
answers
11k
views
Convert array of u32 to Vec<u8> in Rust
In Rust, I want to take an array of u32 values, convert each to four bytes in big endian, and concatenate them to yield a ...
1
vote
1
answer
259
views
LeetCode 863: All Nodes Distance K in Binary Tree
I'm posting two similar solutions for LeetCode's "All Nodes Distance K in Binary Tree". If you'd like to review, please do so. Thank you!
Problem
We are given a binary tree (with root node <...
0
votes
2
answers
1k
views
Which is better practice for this if elif… else statement? [closed]
If below piece of code has to be restructured to use but one single if-elif block, which is better? 1. or 2.?
A = int(input("Enter an integer(1~30): "))
...
3
votes
2
answers
126
views
Is the following divide and conquer recursive algorithm for the exponentiation more efficient than the iterative one for large numbers?
I have the two following algorithms. My analysis says that both of them are \$\mathcal O(m^24^n)\$ i.e they are equivalent for big numbers (more than 32 bits). Is this right? Note that ...
9
votes
5
answers
9k
views
Print a word, one character per line, backwards
I have a problem that I solved in 2 different ways and I would like to know which one is better, cleaner and easier to read.
Problem:
Write a while loop that starts at the last character in the
string ...
2
votes
2
answers
95
views
Common Unit Testing Code – Follow Up
This question is a follow up question to the Common Unit Testing Code portion of my lexical analyzer questions.
My primary concern is the code in the header file and the C source file that implements ...
4
votes
1
answer
118
views
Efficiently use array values and variables in a WordPress template
Is it better to validate and put an array value into a variable at the top of a file or to check the array value directly right before output in the file?
I work at a WordPress shop and we have a ...
4
votes
2
answers
72
views
An example of transferring the C ++ class hierarchy to Golang. Is it done correctly?
I am trying to transfer my c ++ experience to the Go area. I'm new to Go, so not sure if the implementation is correct. In any case, it looks very ugly compared to C ++ code. Is there a nicer solution?...
3
votes
0
answers
83
views
Providing a connection pool
I need some opinions about method naming and single responsibility principle.
First of all, let's talk about method naming. The class below was created to provide a connection pool and whenever a pool ...
2
votes
1
answer
121
views
Any ideas how do I prettify this SQL query?
This question is abut coding style. I'd like to see how would you improve this code look considering readability as well. Feel free to upvote others solutions you find good.
#1 Initial version:
...
3
votes
2
answers
94
views
Granular vs a aggregated tests when dealing with arrays
I write code in a Test-Driven way and I often build my functions starting with tests for the easy edge cases.
For example, given a flat array of Items that have a <...
7
votes
6
answers
3k
views
Checking if an integer is a palindrome using either a string or a dict
I have solved the palindrome question on LeetCode today, but I wasn't happy with the original solution - I used strings, it worked slowly and used a lot of memory.
So I rewrote the answer using a ...
5
votes
2
answers
476
views
Print a right-justified list of a list
I'm working through Chapter 6 in "Automate the Boring Stuff, 2nd Edition." Here is the chapter project at the end.
Write a function named printTable() that takes a list of lists of strings ...
1
vote
2
answers
4k
views
Return row count from mysqli prepared statement
I want to know if it is okay/safe to use a prepared statement and mysqli_num_rows like this:
...
1
vote
2
answers
60
views
Looking at the hill of indents seems like this can be improved
I've spent some time on this as an answer elsewhere, and did my best to optimize it. But looking at the hill of indents seems like this can be improved. I've tried to implement an ...
6
votes
4
answers
1k
views
Computing the item price and vat for a given pricing, order, and exchange rate
How can I improve the readability of the below function using a list comprehension?
Also, is there a way to improve items() performance?
...
3
votes
1
answer
175
views
Checking inactivity with periodic tasks in Java
I have created a simple HealthCheck service. I have two approaches:
Create a task canceling the previous one if the system is active, rescheduling the new task.
Create a periodic task and regularly ...
4
votes
1
answer
534
views
Extracting member-accesses from an expression in C#
I need to extract all member accesses to expression parameter (and make sure, that they are not nested). For instance, for expression: a => a.A + a.B I need to ...
1
vote
1
answer
335
views
LeetCode 200: Number of Islands
I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!
Problem
Given a 2d grid map of '1's (land) and '0's (water), count the number
of islands. ...
5
votes
1
answer
2k
views
Laravel Scope get request data/param
I want to pass the param from the request on the scope method in the model and serve it as json to be rendered by select2. Which is better?
1. Pass request from the controller like this
controller:
<...
1
vote
0
answers
2k
views
What is the best implementation of a ignore case dictionary In C#
Imagine we have a custom dictionary class which is derived from Dictionary<string, object>. Dictionary is case-insensitive about keys and elements arrange is ...
1
vote
1
answer
163
views
Encasing a string in double quotes
I need to add some characters to a String before using it for an API call. The idea is to add a " char both at the beginning and at the end of the String, to make ...
3
votes
2
answers
723
views
Smartly joining possibly empty strings
I want to join two fragments into one string with a separator, but when one of the fragments is empty I only want one of them.
I have two implementations. The first is long and slightly repetitive, ...
2
votes
0
answers
40
views
What is a better practice in bash scripts to test if string is empty: [[ -z ${value//[0-9]/""} ]] or ! [[ ${value//[0-9]/""} ]]?
I test in a bash script if a variable value contains digits only.
There are two options, both give the valid results:
...
3
votes
1
answer
2k
views
In Memory Database in Unit tests, isolate the tests
I have stumbled across these unit tests in a code review that are using in memory db:
...
0
votes
4
answers
131
views
Java method refactoring: getting the file name or path
I have been learning about refactoring and I have this method that I'm wondering if I should split into two for code cleanness. First, the method as one, next the method split in two:
Method not ...
2
votes
0
answers
101
views
Why does my Clojure implementation of Conway's Game of Life run so much slower than my Java implementation?
I'm trying to learn Clojure coming from a Java background. I have written a simple implementation of Conway's game of life in both Clojure and Java, trying to keep the overall structure of the code as ...
1
vote
2
answers
53
views
Copying List Elements To Another List And Changing Them
I'm a beginner python programmer..I recently started reading a python book called: "Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming"...And I found an ...
6
votes
1
answer
1k
views
Recursive LinkedList. Which implementation is better and why?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
<...
3
votes
1
answer
923
views
Maximum distance between space stations HackerRank
I am just working on Flatland Space Stations problem, and it is not seeming to be a tough question, but my algorithm lags in some aspects, since it solves majority of the test cases but fails for some....
6
votes
2
answers
1k
views
Python Class Coding Simplification
Main purpose of this code,
Take some information for animal features
Clasification of user input
print those input into txt file
My problem is that, I think coding of animal features takes too much ...
3
votes
1
answer
378
views
A simple animation in pygame
I'm following a tutorial into pygame. I realized the example code was written for the sake of being easy to understand. I wondered if I could 'improve' the code, with a focus on keeping the exact same ...
4
votes
2
answers
360
views
Builder design pattern pass object
Good Morning
This is a builder design pattern .. the builder problem is duplication so
I want to know from an expert with explanation which is better on quality on this code:
(1)
...