2,232 questions
Best practices
2
votes
8
replies
141
views
Optimization and performance of multiple array iterations for WooCommerce role-based discounts
I have a WooCommerce discount system that calculates fees based on product categories (box sizes) for a specific user role. The code is functional, but I am concerned about the execution cost of ...
Best practices
1
vote
5
replies
159
views
Pydantic- avoid duplicating validation logic for create and update models
I'm building a REST API using FastAPI and Pydantic.
This is an example for my problem:
class RestaurantCreate(BaseModel):
name: str
email: EmailStr
max_tables: int
@field_validator(&...
Score of 2
3 answers
120 views
How to refactor repeated conditional logic for multiple similar elements in JavaScript?
I have a restaurant ordering app where I'm displaying order items. Currently, I have repetitive code for each menu item (pizza, hamburger, beer).
Current code (simplified):
function displayOrder(...
Score of 3
1 answer
116 views
Nested ordered list items to any depth
Is there any cleaner/DRYer way to get nested ordered list items (e.g. list item number 2.4.1.7.8.2.9) that are indented properly, rather than defining every single level manually up to 10 levels deep?
...
Score of 2
1 answer
224 views
How to reduce the redundant code in a header file?
I'm writing unit tests to test C++ classes. The functions below test to see how a function handles an exception. The code below does work, but it is very redundant, I would prefer to have 1 template ...
Score of 0
3 answers
388 views
How are headers not a DRY violation? [closed]
In C++, if you make a separate .cpp file to keep classes separate like you might in Java, you also have to make a .h file declaring everything you want to make.
I understand why you must, since the ...
Score of -1
1 answer
87 views
Prevent duplicate boilerplate operator overloads [duplicate]
This is a follow up of this question, which might have been a XY problem. Anyhow, that was closed within a few minutes, before I could get any real help.
In our large code base (measurement processing ...
Score of 1
2 answers
261 views
Prevent duplicate operator overloads implementations
In our large code base (measurement processing application) we've had bugs where values were mixed up because everything is a double. So we decided to try-out strongly named types for things like ...
Score of 1
1 answer
158 views
How do I not-repeat-myself with this redundant method name prefix?
I'm writing a library which has a queue object (not std::queue and not usable as a C++ container). On this queue, we can enqueue commands, with arguments. Let's say the commands are foo, bar and baz, ...
Score of 0
1 answer
59 views
Overloading + Operator in C++ for Symbol Class and Avoiding Code Duplication
I am creating a binary tree representation of a mathematical expression for performing differentaiton and integration on it.
I am trying to overload the + operator for my Symbol class. It works fine ...
Score of 0
1 answer
74 views
How to prevent duplicate code for exception handling in Python [closed]
What is a Pythonic way to not have duplicated code for exception handling?
In a real code base, it is not 2 / 0, but reading or writing from an async socket, and I have more exceptions to catch, ...
Score of 0
1 answer
63 views
How can I avoid interface repetition in Python function signatures and docstrings?
I'm looking for a standard, lightweight (preferably built-in) way to handle the problem of interface repetition—where the same parameters (with annotations, defaults, and docs) are repeated across ...
Score of 0
1 answer
107 views
Can a generic Blazor Component handle multiple classes with the same structure?
I have created a Blazor Component to handle editing a database record which is defined by a class.
I would like this component to be reused to edit other records - that have the same structure, but ...
Score of 1
0 answers
107 views
How do you idiomatically DRY up rust code without upsetting the borrow checker?
Context
Coming from OOP I'm finding as I implement various traits and functions on my structs, I want to abstract bits and pieces of them out. But whenever I do, I run into borrow checker issues. I'm ...
Score of 0
1 answer
86 views
Does it makes sense to DRY a non-async method using its async version? [duplicate]
Trying to do so involves using the weird syntax of GetAwaiter().GetResult().
Beside that fact, it works; but does it sucks to do so? If yes, then why?
Original:
internal static ISector Read(ISector ...