339 questions
0
votes
1
answer
75
views
Is it possible to reuse property code to get rid of annoying property copy/paste boilerplate in a dataclass?
This is going to be a bit hard to explain, but I'll try nevertheless:
So, I'm a novice trying to turn a Kahoot Generator I made into a library to share on GitHub. After thinking about how I wanted the ...
5
votes
2
answers
115
views
How to simplify template member function definition?
I have a class that looks like this
template<class T, class THash, class TEqual, class TCompare>
class GraphT {
public:
typedef NodeT<T, THash, TEqual, TCompare> NodeInt;
typedef ...
-2
votes
1
answer
80
views
Naming of file while saving with extension
How can I save a file with the name as: oldname+"_new"+extension in an elegant way?
I currently do:
ext = os.path.splitext(file)[1]
output_file = (root+'/'+ os.path.splitext(file)[0]+"...
1
vote
1
answer
110
views
Is conditional awaiting misleading?
Consider the following two code snippets (assumed to be inside an async function that returns void / we don't care about the return value):
Long version (without await):
if (condition) {
...
-1
votes
1
answer
78
views
How do I debug the issues in my code so that my subfunction can also print?
This is a code of my function, I had to breakdown the main function into subfunctions. When I tested the main function it worked but not when I tried to print the output of subfunction. I tried to ...
0
votes
2
answers
133
views
throw exception and then catch vs duplicate error handling
I have a code logic as below:
// Option 1
try {
String result = x.getResult();
if (result == null) {
// some handling
logger.error("xxx");
}
} catch (Throwable t) {
// ...
0
votes
0
answers
61
views
Angular & RxJS - Seeking suggestions on the best practices & code refactor
I created a mini app which shows the Continent & the Country selections.
just to practice RxJS concepts.
(You can checkout to ng-conf/learn-rxjs-01 branch on https://stackblitz.com/edit/stackblitz-...
1
vote
1
answer
1k
views
Visual Studio Format getter and setter on one line
I just scaffolded a project using Template Studio WinUI v5.3 and when I press Ctrl K D to format document, the getter and setter is placed on new lines.
current.cs
public class MyClass
{
public ...
-2
votes
1
answer
160
views
indentation to make software control flow apparent. What do you think about it? [closed]
How many times have you encountered a function with a return statement deep within the code? Or failed to notice a break or a continue in a loop? While I have not been blindsided by return recently, I ...
0
votes
0
answers
85
views
Do compiler optimize memory usage of temporary variables used for better code readability?
Edit: Changed bools to const bool, Added argument to anotherValueTest. May have oversimplified it before.
I am using a MSP430 and the integrated Compiler from the IAR Workbench. Link to workbench
...
-1
votes
4
answers
133
views
What are the implications for the compiler of using a for loop with ternary statements as conditions?
Instead of having two different loops that have identical printf statements, you can make a for loop that uses a boolean and ternaries to switch it forward or backward.
Why is this bad form? Because ...
-1
votes
1
answer
64
views
which of these ways to verify object is better in perspective of readable and maintainable? [closed]
I'm programming Human Resource data sync batch program.
This program reads user and department data from customer company's database and save to our database.
For example, there is class named 'User' ...
1
vote
1
answer
101
views
format dictionary for with new lines and tabs when converting to string?
Is there a simple way to format a string of a dictionary?
I have a dictionary that I want to save into a file, to do this I am passing it to the file as a string using str(dictionary).
This means that ...
-2
votes
3
answers
143
views
Explain this if-else condition in Python
Code
def addition(num):
if num:
return num + addition(num - 1)
else:
return 0
res = addition(10)
print(res)
Explanation
I know what the function is doing:
it is a recursive ...
0
votes
1
answer
330
views
This code calculates the total price of a list of items but how can i improve it?
function calculateTotal(items) {
let total = 0;
for (let i = 0; i < items.length; i++) {
if (items[i].type === "food") {
total += items[i].price * 1.1;
} else {
...
-1
votes
1
answer
51
views
Simulating C/C++ empty defines in Java
I know Java does not have pre-processor, but I struggle to find a way to do this.
I am looking to create macros to improve code readibility, to specify which of the functions or parameters are for ...
0
votes
1
answer
466
views
Right Join vs where a value exists in another table
Without realizing it I've switched to the first block of code as a preference. I am curious if it is a best practice or more efficient to use the first block of code over the second or vice versa?
In ...
0
votes
1
answer
98
views
What constitutes "one action" in "one line - one action"?
I was going through this Javascript tutorial when I came across a call for “one line – one action”.
alert( 2 * counter++ );
Though technically okay, such notation usually makes code less readable. ...
1
vote
4
answers
3k
views
Alternatives to if-else chains with complex conditions in Rust
In rust I sometimes need to write chained if-else statements. They are not the nicest way to manage multiple conditionals with multiple things to check in the conditions.
Here is an artificial rust-...
0
votes
1
answer
217
views
Am I using dynamic arrays correctly?
New programmer here. I need to write a program using dynamic arrays to simulate an election. The program should first ask for a number of candidates, then take the candidate names and the number of ...
0
votes
1
answer
139
views
How does one “document” the flow of a complex code base? After the fact…
I’m a one-man band that has developed a SaaS offering for an existing clientele of ~60k.
I am a self-taught guy, starting with my Tandy 2500XL.
And have worked on enterprise projects with a team since ...
0
votes
1
answer
122
views
Improve code readability of a Python lambda expression with condition
First of all I do not know if these types of questions are appropriate in stackoverflow.
I have the following dict:
file_dict = {"asda_20221003:ada":1, "scdfws_20221104eaf2we":5, &...
2
votes
1
answer
2k
views
Is there any way to indent code for full module/script?
VBA excel: Is there any way to indent code for full module/script in VBA at once?
It's ctrl+I in matlab and everything has the correct identation...
-1
votes
1
answer
217
views
Why using Macros in class for magic number readiblity is not right in class C++
I used Macros at top of class to tell what is 1 and 45.00 instead of just writing magic numbers.
Question is why using Macros in following class (see the default constructor) is not a good idea? Why ...
1
vote
3
answers
152
views
clojure-more readable way to write this function?
I have written this function to convert a vector of maps into string. There is a second map called field-name-to-columns which contains a mapping between the field-name and the actual name of columns ...
1
vote
2
answers
3k
views
Why would you declare a std::string using it's constructor?
Most people when declaring strings in C++, or most other languages, do it like so:
std::string example = "example";
However I've seen some code samples where it is done like this:
std::...
1
vote
0
answers
62
views
How can I improve the readability of my Swift data formatting function?
I have the following function in Swift and am looking to improve the readability:
Adding the variables to the Array below all of the let variables seems kind of funky to me and I was thinking maybe it ...
0
votes
2
answers
200
views
How to make code more readable and simpler
I have this simple code which i have reduced to as little lines and made as readable as possible. I feel i did a good job but was wandering if it is possible to reduce down further? Any help would be ...
-1
votes
2
answers
121
views
Shorten for-loops into one
is it possible to shorten these three for loops into one
the code works absolutely fine, I just want to know if there is a possibility to shorten these three for loops into one for exercising reasons ...
2
votes
1
answer
508
views
How can I access elements of cv::Vec4f by readable name?
I'm using the cv::HoughCircles() function from OpenCV to detect circles in an image. The function takes an output parameter of type cv::OutputArray to provide the results. Based on the example code I ...
0
votes
1
answer
124
views
Handling a class with a large amount of of parameters?
So i'm having a hard time with handling a large amount of class properties. Im making a "component" and want to make this "component" as customizable as possible. In doing so, I ...
0
votes
1
answer
1k
views
The Cyclomatic Complexity of this method is 13 which is greater than 10 authorized
Please help reduce the cyclomatic complexity of this method. I don't really understand how this can be done, I need all the conditions in this method and obviously there are no duplicates.
private ...
2
votes
2
answers
347
views
Java: Avoid too many parameters in constructor
I have multiple service classes ServiceTest1, ServiceTest2, SeviceTest3
I have a factory class UserFactory, which creates a new User instance on every request
UserFactory has a method createUser() ...
0
votes
1
answer
261
views
go structure filling: pointer receiver vs returned values
Looking for a reference to a good reading or sharing an experience of what is better from the code readability/maintenance/best-practice perspective.
There are two options on how to assign values to ...
1
vote
1
answer
37
views
Is there a readable built-in generalization of the `zip` method with automatic "unpacking" of tuples?
Given two lists a=[(1,11), (2,22), (3,33)] and b=[111, 222, 333] I would like to know if there is a syntactically easy-to-read solution to iterate over triples of values as follows:
for x,y,z in ...
2
votes
2
answers
213
views
Defining the values of a field in a database in the Laravel code to make the code more readable
Currently, I am writing a laravel application with a part for sending messages between the staff at the company and the clients.
So, I have a field named "status" in the database. In this ...
-1
votes
1
answer
46
views
How to best manage file either gz or not?
Hi I wonder if there is a better way in terms of code readability and repetition.
I have a large file that do not fit in memory. The file is either compressed .gz or not.
If it is compressed I need to ...
0
votes
2
answers
122
views
Improving code with reflection and generics
With the following code, how could I improve its readability using reflection and generics?
It's a lot of repeated code, but I'm new to these concepts.
"dl" is an interface but I'm not sure ...
1
vote
1
answer
193
views
Apache Thrift Struct for better readability
I just started working with thrift and created following thrift interface.
map<string, map<string, string>> getInformationByIds(1: set<string> Ids)
As you can see, the return type ...
0
votes
1
answer
65
views
Python panda datastructure
I had a datastructure as a numpy array. Python accessing a data structure
Instead, I see I can get it as a panda dataframe. To write readable code, I'd like to address the data in the values column (...
1
vote
5
answers
133
views
Improving readability [closed]
I need to find minimal date(year , month , day, hours , minutes , seconds), my code is working, but it look's terrible and it's very long. What can I do to avoid this ladder to make my code readable? (...
0
votes
4
answers
109
views
How could I reuse this JS code for other questions in my own online quiz? Objects? Frameworks?
How to go about reusing my js code without just copying it and changing the names of variables? I want to add more questions but reuse the same code? This is a simple web test/quiz.
CURRENT CODE ...
0
votes
1
answer
2k
views
Guard clause for a single statement function?
What is the most readable way of writing a very simple function that is effectively executing one statement, if a condition is met?
What I find most readable is this:
function doSomething(myNumber){
...
0
votes
1
answer
39
views
Is there any way for grouping cases in MySQL CASE statements?
Is there any short form for writing MySQL CASE statements as in PHP switch? For example, if i have:
SELECT
CASE Type
WHEN 1 THEN "A"
WHEN 5 THEN "B"
...
0
votes
1
answer
109
views
Initialize object within Hash
Which way is better? Or do you know more better way?
data = Hash.new { |h, k| h[k] = Hash.new { |h2, k2| h2[k2] = [] } } # 5 or more times
or
def object_in_hash(&object)
Hash.new { |h, k| ...
-1
votes
4
answers
2k
views
How to line wrap a long boolean expression with parentheses in java
How to wrap this expression in java for more readability with the constraint that the sub expression (shouldSendSomething(x) && loooongFunctionName1(x) && looooongFunctionName2(x)) ...
1
vote
1
answer
254
views
How to decide on the amount of test cases for smaller than functionality?
I have some function that does one thing for 5 cases that conveniently have the values 1 to 5 and something else for the other cases. The possible values come from an API that currently has ~100 ...
0
votes
0
answers
117
views
Is there a programming language that is close to english and read purely left to right?
Is there a programming language that is purely written left to right?
Example (close to Python syntax):
Normal Python: a: int = 2 + 3
Left to Right Python: 2 + 3 = a: int
The assignment should be ...
0
votes
1
answer
54
views
Compact way to filter a vector base on two vectors of position in R
I have a vector of text, e.g.
library(stringi)
MWE <- stri_rand_strings(200, 10, pattern = "[A-Za-z0-9]")
My actual example is not random, so I get to find some recurring occurences of ...
0
votes
3
answers
1k
views
Ignoring null parameter in where clause linq to sql
I need to ignore null parameters in the where clause so that i can fetch the appropriate data with the applied filters from the user side. Inorder to acheive this, I am currently using the if..else ...