Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
2answers
108 views

Is there a coding style principle for making class usage consistent

I get to review code that often looks like this: class MyClass1 { constructor MyClass(initialiseParam1: Object) { // do something with initialiseParam1 } initialiseStep2() { ...
0
votes
1answer
61 views

How to model either relationship?

I am working on a project where a user can send email to either a contact from his directory (Internal) or type an email address explicitly (External Contact) or a mix of both. We also show the read ...
-1
votes
0answers
42 views

how i read a code split into multiple classes and files? [duplicate]

I'm studying some Code languages OO and always see people saying its easy to put your code in multiple files and multiple classes instead to put everything in one single ROOT class/file. ok, I ...
-3
votes
0answers
64 views

Typescript: Native-style setter/getters vs. Java-style

I was wondering if it is considered an anti-pattern to use Java-style setter/getter declarations in Typescript-applications (more specifically: Angular). I have a Java background and know the ...
2
votes
0answers
68 views

Is “equivalent” functions / methods readability worth its maintanability cost?

First of all, I am sorry if it is a duplicate, I could not think of any "correct" wording to search about this. Second let me clarify: By "equivalent" I mean methods that are meant to have the same ...
1
vote
1answer
72 views

Change request of a behaviour implemented deep in the call stack but is configured in the first calls

Problem: I have a complex system with many layers of abstractions. I need a different behaviour low in the abstractions, but to be configured high in the abstraction. Solution 1: Having a parameter ...
-2
votes
0answers
73 views

Why is all security examples using exception based programming [duplicate]

My last task at work was to implement some security using JWT with Oauth2 and Spring boot. But to the point. All the guides and examples I found about security along with all the libraries for ...
2
votes
1answer
156 views

Access multiple entities in repository - clean architecture

I have a API call that returns a list of Payments, each Payment contains a User. I need to call it, save the results in the database, fetch all the payments and users and return them all to the ...
8
votes
5answers
854 views

Clean Code - Should I change the variable 1 to constant?

To avoid magic numbers, we often hear that we should give a meaningful name for a variable. Such as: //THIS CODE COMES FROM THE CLEAN CODE BOOK for (int j = 0; j < 34; j++) { s += (t[j] * 4) / ...
2
votes
1answer
124 views

Clean Architecture and MVC controllers + entities

In Uncle Bob's clean architecture, why can't MVC controllers call entities directly? It seems like since the controller depends on the inner layer, they should be able to call entities when needed. If ...
0
votes
0answers
47 views

How do I correctly separate the code that does three things and when to use events?

I have in controller: (pseudo code) public class MainController { public void accept(RequestData requestData) { Ticket ticket = this.generateTicket(); DB db = this....
0
votes
3answers
89 views

Updating nested entities with constraints without getting spaghettimadness

I have an endpoint "import" that is supposed to accept an entity. This entity can contain several sub-entities with yet again some sub-entities. Since this is an "import" endpoint, some of the entites ...
0
votes
1answer
114 views

Simple but frequently throwing vs reasonable but rarely throwing vs complex but never throwing [duplicate]

A lot of code that's designed to convert or parse some data of type Foo into a Bar is written with the assumption that one wouldn't intentionally pass it invalid inputs. As such, it assumes that ...
2
votes
3answers
215 views

Try/catch or add exception to method signature for method used by a larger system?

public Path createPath(String name){ return Files.createFile( Paths.get( name ) ); } In createPath(String name), the java.nio.file.Files throws a java.io.IOException. This is however to be used ...
0
votes
3answers
188 views

Naming variable which describes that something is currenty in use

I'm creating a variable which should describe that an object is currently in use. Lets say we have a form with a password input, and I want to describe the state when someone is using that input. Is ...
78
votes
9answers
16k views

How do huge open source libraries get maintained while having code far from “clean code” practices?

I'm still inexperienced to write high quality code, so I read books addressing the issue such as Clean Code by Robert C. Martin, and keep checking code of well-known libraries to improve my skills. ...
2
votes
6answers
156 views

Main function's subfunctions should terminate main function

So if I have a big function I break it to smaller ones to increase readability. If I have parts in my big function where I want it to return. I want my subfunctions to have the ability to make the ...
3
votes
3answers
252 views

Changing large number of if-elif-else statements to use underlying structure

I have a function that looks something like this: function_name(step, ... , typ): if typ == 'some type of calc method': if step == 1: do_me_at_step_1(...) elif step ...
1
vote
0answers
25 views

Applying an overloaded/templated function depending on the type of a QVariant

I'm trying to take a QVariant (in this case from a QProperty generated on a class) and based on the type, return a QWidget that allows for it to be edited appropriately. a signature for the ...
159
votes
23answers
22k views

Programming cleanly when writing scientific code

I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code. My code is primarily "scripting" type stuff - things to test mathematical ...
47
votes
8answers
6k views

Why isn't encoding the names of arguments in function names more common? [closed]

In Clean Code the author gives an example of assertExpectedEqualsActual(expected, actual) vs assertEquals(expected, actual) with the former claimed to be more clear because it removes the need to ...
-2
votes
1answer
162 views

How to clean a refactor Java for-if-try-catch-else kind of messy code [duplicate]

Often I came across situations like this, how to write this code in a neat and clean way. One more issue I find here is performance as I am iteration a list and then it's properties. Edit : - while ...
1
vote
4answers
412 views

Separation of application logic and domain logic in Clean Architecture

I'm struggling with the separation of logic between entities and interactors or Use Cases. If I design the entities with DDD principles, each entity would have methods corresponding to use cases, ...
0
votes
3answers
211 views

less functions and complex code or more functions and readable code?

I am currently making an html-based editor, i know there are lots of editors out there especially html based but i want to do it myself once. What i am concerned about is, for e.g, when a user press a ...
4
votes
2answers
238 views

Clean Architecture: get instance in inner circle of class from outer circle

This might be an easy question for experienced software engineers. I'm currently reading the book Clean Architecture by Uncle Bob, and I'm trying to implement clean architecture in a Java project. ...
1
vote
0answers
247 views

Should Flux mutation logic always be done in the store, or can it be delegated to a class?

(I am using Vuex but the question applies to any Flux architecture.) I don't have a great understanding of the reasoning behind Flux architecture. I understand that it is nice to have all of the ...
1
vote
3answers
267 views

Bug-preventing vs clean code

If I'm faced with the decision to write clean, beautiful code, but I'd be opening up a new path bugs could take to appear, should I take it? (Provided I'm not willing to spend the time to come up with ...
0
votes
0answers
52 views

Best practice to populate a `container` class

lets say I have several objects Object A,Object B,Object C in different parts of my project. I have defined a Object X that is only used to store some specific values from the objects mentioned above ...
0
votes
3answers
102 views

Approaches for not complex conditions

I encountered code like this below. I was told its fine, and boilerplate is not always better. I agree boilerplate might be bad, but I am not sure about something like this: if (entity....
-1
votes
1answer
260 views

How to split an onion-style DDD project without using microservices?

Background: I'm building a prototype scientific app with the "Exploration" and "Optimisation" parts. (Other parts might be needed later.) Each part uses functional reactive programming (FRP) and has ...
5
votes
4answers
957 views

Refactoring many else if, else if, else if, etc. statements

I'm trying to parse readable data from PDFs and I keep ending up writing code like the following: if (IsDob(line)) { dob = ParseDob(line); } else if (IsGender(line)) { gender = ParseGender(...
2
votes
1answer
608 views

Clean architecture - How do I deal with use case reuse?

Trying to apply Uncle Bob's clean architecture to an application I'm maintaining and I'm having difficulties with use cases and duplication/reuse. It's my understanding that usecases should be self ...
6
votes
1answer
199 views

How to tackle extensibility considering the Data/Object Anti-Symmetry?

In Clean Code by Uncle Bob, page 124-125 he states Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the ...
0
votes
6answers
817 views

Are vague variable names more maintainable?

I'm looking for some advice about which line of code is more appropriate, readable, and maintainable. 1: var result = mediator.Send(query).Result; 2: var accountDetails = mediator.Send(...
2
votes
2answers
69 views

How to separate construction and runtime logic in iOS?

I've been reading Robert Martin's Clean Code book where it suggests we should separate a programs startup/construction process from its run time logic. In Java (the language the book uses) this ...
2
votes
4answers
301 views

code smell: passing through variables [closed]

Consider this simple example: def a(val_x, val_y): return 5 + b(val_x, val_y) def b(val_x, val_y): return 1 + c(val_x, val_y) def c(val_x, val_y): return val_x * val_y Passing the ...
1
vote
1answer
71 views

Best way to code actions and their criteria/checks

I've been trying to make my code cleaner and I have came across an issue which I don't know what the best solution is. This is the code I want to "clean". Please bare in mind that I am refactoring ...
1
vote
0answers
101 views

What is the best way to manage a class with lots of properties?

We have one table meta where there is key and value so each of the row represents one component or service and then we have api system which manages the metas database. And it was going pretty well ...
4
votes
5answers
437 views

Is it a bad practice to use conditionals with functions that change program's state?

The title might be a bit vague, so let me explain. Let's assume we have a function that does something (changes state of the program), for example a function that creates a file. That function returns ...
1
vote
4answers
250 views

Put conditional logic inside method for DRY, or keep it outside for readability?

Take the following example which loads an interstitial ad every 10 times user does XYZ in the app, under certain conditions. It is called in multiple places in the code base: public class AdHandler { ...
1
vote
1answer
361 views

Clean architecture with C#: A better design to perform validation in Value Objects

I'm creating an application which's architecture is based on Uncle Bob's Clean Architecture concepts and DDD. Note that it is BASED on DDD, so I gave myself the freedom to differ from strict DDD. To ...
2
votes
4answers
291 views

Why chain assignments to clear variables?

Why do some developers in C for example chain together assignments of variables like this: i = j = k = 0; or sometimes formatted like i = j = k = 0; Is there some underlying complier optimization ...
4
votes
4answers
209 views

How to deal with Law of Demeter in the product - owner relationship?

I want to display the product, and the product card has a lot of information about the product and the owner. How to deal with Law of Demeter in this product - owner relationship? In controller I ...
-1
votes
3answers
122 views

How to build your class in a readable way [closed]

When developping a project I follow the OOP principles and break my code into classes and most of the time I go for one file = one class. Now I dont know how to organize my code into the class and ...
67
votes
12answers
18k views

Prevent deprecated code from compiling after reaching a deadline [closed]

In my team we have been cleaning a lot of old stuff in a big monolithic project (whole classes, methods, etc.). During that cleaning tasks I was wondering if there is a kind of annotation or library ...
2
votes
2answers
128 views

Is replacing the call to mutual exclusive methods by an injected strategy overengineering?

I recently gave advice to refactor some code along the lines of class Validator<T> : IValidator { public ValidationResult Validate(Validatable thingToValidate) { if(typeof(T)....
5
votes
5answers
962 views

What is more important? SOLID or KISS?

What is more important? SOLID or KISS? To be consistent with SOLID I would have to inject Decorator and Footer classes and create interfaces for them. public class HelloService { String name; ...
2
votes
1answer
39 views

Aliasing repeated function calls

I'm working on a JavaScript user script to assist my team in making assembling support tickets on a web interface more efficient. In doing so, I'm finding myself making a lot of calls to DOM API to ...
4
votes
5answers
1k views

What to do with long variable names?

First example: countryRepository.getCountriesFromAsiaWhereAreTheMostPlanesAndBoats(); countryRepository.getCountriesFromAfricaWhereAreTheMostCars(); Are these names too big? How else do we call ...
2
votes
4answers
318 views

Clean Code and the Principle of Least Astonishment

In Java's documentation, it states: Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the ...