Questions tagged [clean-code]
The term "clean code" is used to describe computer programming code that is concise, easy to understand, and expresses the programmer's intent clearly. Questions with this tag relate to the process of writing clean code, or refactoring old "dirty" code to be clean code.
535 questions
1
vote
1
answer
225
views
How should domain models be designed — rich domain models with encapsulated logic vs. anemic models with separate service/util layers?
I'm learning Domain-Driven Design (DDD) and studying different architecture patterns, and I’ve come across two seemingly conflicting design philosophies around domain modeling.
1. Rich Domain Model ...
2
votes
2
answers
154
views
When using Clean Code with Ports and Mappers: which layer declares the Entity (Aggregate) factory interface and which one implements it?
So I was reading Eric Evans DDD book and one point that was not clear to me is which Layer (in case of using Clean Code) should be reponsible to:
define the interface contract for an Entity (...
2
votes
3
answers
1k
views
Scope of integration tests
I'm programming a .NET WebApi application from services.
What is the scope of an integration test within the following schema?
Order creation scenario:
Order is created
-> stored in db
-&...
6
votes
7
answers
720
views
Why do "protected variables" tend to violate open closed principle?
According to Why is Clean Code suggesting avoiding protected variables?, I know there are tons of reasons to avoid protected variables.
However, there is a reason at the currently highest voted answer ...
0
votes
2
answers
364
views
Which is better: a chain of OR statements or IF in a loop? (Java)
Which code style is preferred in Java?
final boolean result = isCausedBy(e, ExceptionType1.class)
|| isCausedBy(e, ExceptionType2.class)
|| isCausedBy(e, ExceptionType3.class)
|...
2
votes
1
answer
245
views
checkNotNull vs. JEP 358: Helpful NullPointerExceptions: Should we remove existing null checks?
With the introduction of JEP 358 in Java 14, which provides more informative NullPointerException (NPE) messages, is it advisable to remove existing explicit null checks in cases where the null-check ...
0
votes
1
answer
112
views
Orchestrating Events & Third Party Services
I am developing a hobby project utilizing an event sourced + event based architecture. In this application, I can allow my users to schedule automated calls in the system. Once the call has been done ...
2
votes
4
answers
205
views
Where are random numbers generated in Clean/CQRS architected application
Assume I have a value object in my application with random numbers for underlying values. These value objects will serve as attributes to my entity (aggregate root). In my specific case (just for ...
6
votes
5
answers
839
views
doSomethingIfCondition(). Is it good naming? [closed]
Suppose, I have some method that performs some job. In this example, it's going to be void, but it doesn't matter
public void doSomething() {
// doing something
}
But then, let's imagine, I got ...
2
votes
1
answer
174
views
Should I create another API to “move” content or modify my existing API that already does something similar?
I have a Spring Boot REST API that will copy content from one Amazon S3 bucket to another. The source and destination buckets are specified in the body of a POST request sent to the API. This works ...
35
votes
11
answers
13k
views
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
As developers, we often face the challenge of balancing meaningful variable names with code readability. Long, descriptive names can make code harder to read, while short names may lack context. For ...
0
votes
0
answers
107
views
Is it really difficult to test these “Service” methods in this Rust Clean Architecture proposal? Is there some other catch I'm not considering?
I reproduced a small example of kerkour's Rust Clean Architecture on the Rust Playground.
The code is just an example and the methods code makes no sense at all.
This architecture leaks DB information ...
2
votes
3
answers
242
views
Is it still "feature envy" if the state to make decision or the action to take after asking the state involves other classes to participate?
According to https://softwareengineering.stackexchange.com/a/212130/432039, if a class asks another class for the state, and then call methods of that class, it is called "feature envy", eg:
...
5
votes
1
answer
576
views
Confused on how abstraction and encapsulation is helpful
Using assimp I've created a function to load 3D models and it does everything I need and I don't plan to use another library or write something custom, however, I am curious how techniques such as ...
3
votes
3
answers
170
views
Is Each Table Columns on Separate Pages Considered Duplication in code?
Each table is on separate pages. Is it considered duplication (written in Angular)?
The DevOps team ran SonarQube and detected the th tags as code duplication. How can I explain to them that it's not ...
-2
votes
1
answer
533
views
Defining functions inside vs outside a class
Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered ...
4
votes
4
answers
1k
views
throwing an exception or returning Optional
In a service daemon coded in Java, we have services for getting various objects.
When we use these services forgetting an object, I wonder if
the method should return the object and throw an ...
5
votes
1
answer
514
views
How does one add observability to python code without affecting code quality?
We're working an agile project and designing as we go on a new python commandline app / systemd service for some fancy in-house project. Right now, we're supposed to be adding an observability / ...
-3
votes
2
answers
817
views
Is throwing an error in programming is good Idea at all? [duplicate]
I am wondering if throwing an error in programming is good Idea at all ?
While programming we throw error when something unexpected or invalid has occurred. But if we have thrown exception and it is ...
-1
votes
1
answer
251
views
Best practices for organising PHP files?
I am currently writing a PHP / JavaScript application library, which I intend to use in the future for several other applications. The library includes functionality for handling a database, creating ...
0
votes
1
answer
140
views
Return response from controller or raise exception from service
I need some guidance on how to send error responses to client from WebAPI controller for an update operation. I need to check if data is changed and if it has duplicate data. I have service class that ...
1
vote
3
answers
483
views
Is low code quality and lack of testing the norm in B2B software development?
I've been working at this company for about a year now. it's a growth company working in B2B. I'm one year out of university, with a major in computer science. I work in the web team, using an Angular ...
0
votes
1
answer
278
views
How to deal with very complex codebase? [duplicate]
I recently changed my job to a big MNC and the code I am exposed to is highly complicated, difficult to read and understand. Although it is divided in microservices and runs on local I have to keep ...
-2
votes
1
answer
124
views
Clean code: formatting rules, dependent functions, multiple calls [closed]
About formatting
Here are two scenarios in which the details of the formatting are described.
How should in the clean code way, this be formatted
Scenario 1: Dependent functions of dependent functions
...
0
votes
2
answers
404
views
Does Clean Code suggest avoiding all base class instance variables (include private instance variables)?
According to Why is Clean Code suggesting avoiding protected variables?, I should avoid protected variables because of the reason : "closely related concepts should not be separated into ...
13
votes
4
answers
5k
views
Is there a clean way to model methods that only make sense depending on the current state of the object?
Let's say that I have a class called Mission. This class is intended to represent a mission requested by a professor working in a faculty. Mission has a private enum field representing whether the ...
0
votes
3
answers
252
views
Is "the boolean value is from dynamic loaded data (eg:user input, database)" a reason to use boolean parameter?
I know there are some questions about boolean flags: Is it wrong to use a boolean parameter to determine behavior?, Multiple boolean arguments - why is it bad? which indicates the following code is ...
6
votes
2
answers
13k
views
How can I nicely pass on a class instance in python?
I am rewriting a streamlit app that is an interface to a laboratory management system (LMS). This means, that I have to make a lot of requests to that LMS through its python library (PyBIS). The way ...
3
votes
5
answers
254
views
Add new condition or create a separate method?
At first I had the following logic of user creation:
class UserService {
constructor(userRepository, postRepository) {}
createUser({user}) {
this.userRepository.create(user);
}
}
Then I ...
10
votes
2
answers
10k
views
Key difference between Hexagonal Architecture vs Clean Architecture?
I am trying to improve the architecture for my React app project that has grown in size lately.
I'm looking at Hexagonal architecture and Clean architecture, and I couldn't really see the difference ...
-1
votes
1
answer
201
views
How to integrate business logic from Domain layer with UI
I have a rich client with quite complex domain logic and I want to keep the domain and presentation layer separated. How would you advice to intergrate the domain logic with the UI in this case.
I ...
0
votes
1
answer
373
views
Should you add the name of the package to the module/package name in Python? [closed]
I'm looking for some best practices for readability (and clean code in general) for naming modules/classes within more extensive projects. More specifically, is it reasonable to add the package's name ...
0
votes
4
answers
222
views
Should we create a new function for every multiple conditions?
I have the existing code provided below:
if (!$existing_records && !$has_required_field) {
return 'Skip Update, no records found';
} elseif (!$existing_records && $...
9
votes
4
answers
3k
views
Is it okay to have misleading struct and function names for the sake of encapsulation?
I'm writing this library in which the user can provide custom code defining the algorithm used for finding an optimal solution. In the papers that I have read, the targeted user thinks in terms of ...
37
votes
11
answers
13k
views
Multiple boolean arguments - why is it bad?
This seems to be a commonly encountered and brought up code smell, and is a common feature in multiple coding design books which I wasn't fully able to understand.
Say I have a function
def ...
1
vote
1
answer
292
views
Working with a poorly written piece of software as an entry-level developer with no support or documentation [duplicate]
I'm and entry-level developer straight out of college. I am the only developer at this company.
I feel the challenges I face are different to most people working with sub optimal or very poorly ...
19
votes
11
answers
6k
views
Code readability and debugging
Sometimes, I have a hard time deciding between two good code traits: debuggability and readability. The snippets below are an oversimplification, but they illustrate my pain.
Example 1:
if(...
0
votes
2
answers
386
views
How to refactor this tightly-coupled method and (mostly) preserve its encapsulation?
I have recently encountered this problematic method (minimal reproducible sample in C++ but this question aims to be language agnostic past the syntax):
void MyObject::twice_bind_cycle() {
_bind1()...
0
votes
4
answers
360
views
Object matching using generic method? [closed]
I have a piece of code where two objects (incoming request object and a profile object) are to be checked for matching conditions.
So the first method is as below where I check whether the profile ...
3
votes
3
answers
1k
views
How to write effective automated tests when building GPT4 powered software?
Part of my current product uses GPT-4 with a prompt to extract information from plain text. As behaviour of large language models is inherently opaque and failure cases unpredictable, I was wondering ...
0
votes
1
answer
86
views
Avoiding repeating code when assigning pointer addresses based on branch
I have the following code in C that reads data off a char array and makes decisions on what variables should some pointers point to.
char preset1[20];
char preset1Name[20];
int preset1Temp;
int ...
3
votes
1
answer
307
views
Composite repositories: minimizing dependency injections
I have an application with dependency injection that consumes a REST API. The API calls are abstracted into entity-specific repositories. Some of the repositories are: HttpNotebookRepository, ...
0
votes
1
answer
611
views
Separating Application (Business Logic) Layer to Multiple Module Layers in Clean Architecture
Currently, I'm designing the Project based on Clean Architecture composing with 4 layers:
Domain
Application
Infrastructure
Presentation
Since the project is pretty huge, I'm now considering to ...
-1
votes
1
answer
217
views
What is point of having certain microservices making another API call in the same service
I was going through an old, largely untouched part in my company’s codebase and found one API. It does the following things.
A POST API with path host/entities/trigger which fetches a list of entity ...
1
vote
2
answers
844
views
Best practice for updating object fields (with values calculated in methods)
What's the cleanest and most coherent way to assign a new value to an object field when the calculation of that value is outsourced to another method?
The most obvious way would just be to update the ...
1
vote
2
answers
645
views
Software Design: Decoupling when highly dependent on a third party library
As part of an university project I am currently working on an eeg-biosignal classifier. While the project itself doesn't really focus on design ("anything that works") I am trying to learn ...
0
votes
2
answers
725
views
Is there some easy way to refactor deeply coupled python code
I recently took on a long ago python project which has some weird code style that I can't pinpoint. e.g.
# this is a params and value package?
opts={
infile="xxx",
outfile="xxx"
}
...
1
vote
0
answers
225
views
Typescript Clean Architecture: How to tackle use case that needs multiple repositories?
I am implementing clean architecture in my new back-end project and getting stuck with a particular use case.
I have a CreatePhysicianUseCase what is responsible for creating a new physician user in ...
-1
votes
1
answer
149
views
Using helper libraries to improve readability and conciseness of code vs adhering to native functions
currently in my place of work I'm headbutting with some coworkers from other teams (but same repo), since they are in a line of thinking where they prefer this:
const connectionList = Object.values(...
2
votes
4
answers
456
views
Looking for a sanity check, do you alphabetize your code? Is that even a thing? [closed]
I'm currently working with a group of developers (as outside contractors) on an existing codebase (in kotlin) and the lead dev (the client) is blocking merging of pull requests (GitHub) because ...