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.

2
votes
4answers
225 views

Clean code vs clean git/jira history

I started reading the book Clean Code by Robert C. Martin and at the start I found this idea of his interesting, "Leave the code cleaner than you found it" adapted from the "Leave the campground ...
2
votes
3answers
360 views

What is a good place, in OO, to store a string that is used many places?

I have a string that is used in a few places. string portalLoginPath = $"{Request.Scheme}{Uri.SchemeDelimiter}{Request.Host}/Account/Login"; I was thinking of creating a static class with a string ...
4
votes
2answers
559 views

Clean Code: Another question about boolean as function parameters [duplicate]

I had a discussion, if the code for calling information from a database can have a switch to show also deleted entries. Simplyfied the code (C#) look like this: void searchEntry(string searchValue, ...
1
vote
4answers
77 views

Is it a good idea to share repositories across microservices in Spring Boot Application?

We are migrating a desktop application into web based Spring Boot micro services application with a client imposed mandate of using their existing MySQL database, so all micro services share a common ...
0
votes
1answer
45 views

Java convention - Implementing two similar functions for two different objects

I have two classes, let's call them Foo and Bar. They both extend different classes (Foo extends X, Bar extends Y), which have some common ancestor "way up" the inheritance tree, something like this: ...
0
votes
1answer
127 views

Unit testing protected methods which have no reason to be public

I need to make a sponsorship system with complex business requirements. Basically, after a user makes a payment, the system should get triggered. There are many different types of sponsoring, so I ...
1
vote
3answers
127 views

Always return the parameter when it has changed inside the method?

When you have a method that is doing stuff and changes occur in the parameters, should you always return the changed object like this: public SomeModel DoSomething(SomeModel someModel) { [something ...
0
votes
2answers
173 views

Uncle Bob's clean architecture refactoring techniques for data objects / entities / LINQ queries

I have relatively simple controller logic with ugly parts of object creation. Some of the objects are really huge and even though all the controller does is to return the object, it looks messy and ...
131
votes
17answers
12k views

How do I know how reusable my methods should be? [closed]

I am minding my own business at home and my wife comes to me and says Honey.. Can you print all the Day Light Savings around the world for 2018 in the console? I need to check something. And I am ...
-2
votes
0answers
114 views

Is this a valid example of Demeters Law?

I'm tasked to explain certain concepts from 'Clean Code' to my colleagues. I prepared a power point presentaion to explain some of the core concepts. I now wonder if my explination of Demeter's law is ...
3
votes
3answers
124 views

One try/catch for each error, or one global try catch and switch error?

I have multiple methods that could fail, let's say : readFile() parseFile() compute() Each of them could fail (e.g file not found, wrong file type, wrong data), should I put each of them in their ...
8
votes
2answers
149 views

fall-through switch for executing a sequence of steps

My program needs to execute a sequence of steps from start to end. But based on different input the start point will vary, e.g, some will run from the first step to end, some will run from the 2nd ...
3
votes
3answers
234 views

what is the best way to write a recurring code with different enums

Basically what this code does is to determine whether the user is allowed access to the page or not from list of pages as enum, each one has the role name as its name and the page name inside it. The ...
-1
votes
1answer
76 views

How to ensure separation and inward dependencies between architectural layers in Python?

Suppose a large-scale project is being developed in Python 3.7. Some layered architecture is chosen: "clean architecture", "onion" or "hexagonal". The Dependency rule in it only allows inward-...
92
votes
9answers
14k views

Why should I use dependency injection?

I am having a hard time looking for resources on why I should use dependency injection. Most of the resources that I see explains that it just passes an instance of an object to another instance of an ...
1
vote
3answers
483 views

Convincing a development team to use a better design pattern [closed]

I recently joined a company where I was tasked with building a system for one of their clients. The work I've done is so far working well, but the most senior developer on the team who's been with the ...
2
votes
1answer
183 views

How do I use polymorphism instead of instanceof?

I'm trying to make an abstract board game. In the game, a player can choose to make multiple actions within one turn such as placing, moving, or rotating a piece. I'm not sure if whether or not my ...
0
votes
2answers
115 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() { ...
-1
votes
1answer
96 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 ...
2
votes
0answers
75 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
83 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
1answer
292 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 ...
14
votes
6answers
1k views

Clean Code - Should I change the literal 1 to a constant?

To avoid magic numbers, we often hear that we should give a literal a meaningful name. Such as: //THIS CODE COMES FROM THE CLEAN CODE BOOK for (int j = 0; j < 34; j++) { s += (t[j] * 4) / 5; } ...
1
vote
1answer
214 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
3answers
95 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
115 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
354 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
194 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
17k 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
162 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
424 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
26 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 ...
162
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
223 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
685 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
216 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
261 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
308 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
273 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
4answers
139 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
104 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
283 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
1k 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
870 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
223 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
828 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
78 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
308 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 ...