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.

Filter by
Sorted by
Tagged with
2 votes
1 answer
2k views

How to handle input validation in microservices for duplicated data

What would be the best practice of handling input validation in microservice? Especially for the duplicated data? To give context, say I have 3 services: User Typical user service with User object ...
  • 283
-3 votes
1 answer
189 views

What is the rule for making functions private in Python modules?

I was wondering what's the principle behind it, and whether I should treat all functions from module that are not used anywhere else at the time as private, and mark them with underscore prefix so ...
  • 1
52 votes
10 answers
15k views

Clean Code: long names instead of comments

Don't be afraid to make a name long. A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment. Robert C. Martin Did I ...
1 vote
1 answer
3k views

C# | How to Unit test a Timer (in a daily job run scheduler)?

I wrote this class: using System; using System.Threading; using System.Threading.Tasks; public interface IJobScheduler { void RunDaily(Task task, int hour, int minutes ); } ...
  • 111
-1 votes
2 answers
98 views

Execute sequence of methods conditionally and log possible error

I'm not sure if this is the adequate site (maybe CodeReview?) but it's the only one here in StackExchange that have got a "clean code" tag ... There is no need for downvotes if this is not the correct ...
  • 397
4 votes
0 answers
800 views

Identity and access management in Clean Architecture and MVC design

The application user has a lot of standard functionality we see in most applications. At a high-level, this includes some form of authentication, authorization, and session management. At a low-level, ...
  • 181
3 votes
3 answers
2k views

Multiple classes depend on one object reference

I have several classes like Button, Textbox and so on but at instantiation of those object they all need one object reference. The button represents a physical button on the screen but it is not an UI ...
  • 171
2 votes
2 answers
503 views

Does it make sense to use meaningless named constants?

For example, does it make sense to refactor the following code: a = a * 2; as: const int INT_TWO = 2; // ... a = a * INT_TWO; My question hinges on the fact that the new constant conveys no ...
1 vote
2 answers
62 views

How to pass variable to function that may or may not be available based upon options?

I have a question and it is quite simple. Here are the details: I want to pass an options object into my mainFunction and some other functions inside of it, i.e. someFunctionCall and ...
-4 votes
1 answer
203 views

How to decide what should be bounded context

I am trying to develop a demo application and confused while designing context. Let's say we have three entity and their properties User Id Email Todo Questions Answers Value ...
  • 101
0 votes
3 answers
156 views

Should a function that returns something be named as an accessor (getSomething)?

I understand accessors have to do with OOP. For other languages such as C, imagine you have a function that given 2 numbers returns their sum. Should you name it getSum() or sum()?
7 votes
2 answers
735 views

Is there a correct way to use abbreviations in code? [closed]

Oh, abbreviations in code, the bane of every developer. Consensus seems to be against them, because readability might be affected. But the examples given are often extreme: like "repr" for "represent"...
4 votes
4 answers
337 views

Measuring the success of coding dojos

I'm interested in starting coding dojos at the company I work at (during or after working hours). I think it will help to spread knowledge on how to write clean, concise and consistent code. I've been ...
  • 85
0 votes
2 answers
422 views

Design pattern for executing and reverting the tasks in same order

I have a Task in my project , to complete that i have call(REST) multiple external systems. If my call fails at some level , i have to rollback all my previous calls(making call with undo action) . ...
  • 149
-2 votes
1 answer
1k views

Pass ID or Object which has irrelavant details as RequestBody in Rest Call?

Situation: I am designing a REST API that needs one or more, potentially large objects to do it's work I am facing a decision to either Pass the large object by reference and have the API retrieve ...
  • 109
67 votes
13 answers
17k views

How to avoid comments about one line of code for cleanliness

I'm trying to clean up some of my code using some best practices, and read that comments are almost always a bad idea for future maintainability. I've managed to get rid of most of them by refactoring ...
  • 1,496
1 vote
2 answers
478 views

How to declare and share a lot of variables to provide best performance

My question is not about dilemma between clean code vs performance, but I want to understand exact issue with declaring variables and sharing them between functions. I read in many threads, that from ...
  • 123
46 votes
8 answers
12k views

Should I use AND in a function name?

I am in a predicament. I am working in a repository that makes a lot of database changes within its functions. The function I am dealing with returns responseIds (but from a transformation, not from ...
5 votes
3 answers
486 views

How to manage chaotic code explosion in React application

So we decided to redo UI of our web application in React. Six months down the lane and we have a complete mess of components and reducers and thunks and actions and god knows what not. We have ...
  • 295
4 votes
4 answers
364 views

Grouping of methods

We have multiple pages searching for users, each site having different search parameters. Sometimes, we have 2 parameters, sometimes 4 and most of these parameters overlap. So we have kind of (...
2 votes
2 answers
301 views

Refactor approach for huge if else Clutter based on many independant conditions

Currently I am cleaning up hard to maintain and test if else clutter which is based on conditions which have to be checked in isolations: What is the basic semantic of the conditions? Big Entity ...
  • 136
-3 votes
1 answer
95 views

Should vanilla solutions be avoided in Spring web applications?

I am making a Java and Spring webapp that scrapes data from a web and then publishes it through an API. Some of the raw scrapped data is in the form of Set<SomeObject> which I then convert to a ...
  • 15
84 votes
9 answers
19k views

When to use / not use syntactic sugar

Currently I am working on a school project written in C#. Some teammates just started in C# and some are already familiar with C#. Today I had a discussion on whether to use syntactic sugar like this ...
-1 votes
1 answer
171 views

Proper program structuring in Python

So, recently I have been doing a lot of programming in Python. I have noticed that my programs can be somewhat hard to read. I usually have one main class which does everything, sort of like this: ...
2 votes
5 answers
389 views

Clean code vs ease of testing: Make method params explicit when they are class attributes just so unit testing is easier?

In Clean Code, we're advised to reduce, wherever possible, the number of arguments in a method's signature. This can often be achieved by using class attributes in lieu of parameters. Now in a code ...
  • 516
7 votes
4 answers
1k views

Are "in" parameter modifiers considered a code smell?

As of C# 7.2+, you can add a parameter modifier in before the parameter type to define a parameter as const, essentially. It is like the ref or out keywords, except that in arguments cannot be ...
  • 183
57 votes
6 answers
23k views

Functions that simply call another function, bad design choice?

I have a setup of a class that represents a building. This building has a floor plan, which has bounds. The way I have it setup is like this: public struct Bounds {} // AABB bounding box stuff //...
  • 2,032
2 votes
1 answer
165 views

Should we put small, potentially often-used pieces of code into tool methods in a shared library? [duplicate]

I recently switched jobs to a new company which develops a .NET framework based product which has been on the market for like 15 years already. Some of the engineers are in this project almost from ...
  • 129
2 votes
1 answer
269 views

Better way to implement a feature with turn on/off based on a flag

I'm trying to implement a feature with option to turn it on/off based on a flag. I'm doing it in the following way which I feel like a overkill, public interface Feature { public interface ...
1 vote
2 answers
318 views

Should a BDD feature be implemented by a one (and only one) clean architecture interactor?

TL; DR Given BDD documentation with features composed by several scenarios, should we: Create a single interactor per feature Create minor interactors per scenario, and composing the feature ...
12 votes
5 answers
1k views

How to introduce new language features in a legacy source code?

I have a coding style related question. In my example it is java, but I think this can be a generic question regarding languages that are changing rapidly. I'm working on a java code base which was ...
  • 243
21 votes
6 answers
3k views

How to quantify Code Quality [closed]

My Business unit has huge number of developers (About 100). Due to various reasons, the developers are nearly solely focused on delivery rather than quality. The bad quality code has already started ...
7 votes
4 answers
1k views

C# declaration redundancy

When declaring a member class MyClass { AnyClass<WithLong<Generic,Declaration>> myProp = new AnyClass<WithLong<Generic,Declaration>>(); } is quite redundant. ...
1 vote
3 answers
927 views

How should I name output variables that are the same as the function?

What are some conventions for naming a variable in cases where the name of the variable ought to be identical to the name of the function? I'm using VB.Net. I often have this problem when writing ...
3 votes
2 answers
1k views

Clean Code: Avoiding "Order Matters" without passing member variables to private methods?

I am trying to implement some of the principles laid out in Clean Code by Robert C. Martin. I had a class that was heavily suffering from the ordering problem. I have solved most of this by ...
  • 915
1 vote
3 answers
259 views

Which option is best, when you have an attribute that is a list?

I have a class containing a list, for example: class User{ .... List<String> cards ... } What's the best way to provide access to this member? List<String> getCards() or ...
  • 387
0 votes
0 answers
159 views

Where to put use case calling threads in clean architecture?

I've just started using clean architecture, It's clean and nice, but I've found a bit confusing use case. My application should be able to register to update a token after some time, so I handled ...
1 vote
2 answers
743 views

What is the cleanest way of writing a function with conditional statement with many returning options?

Maybe that's simple, but I'm a little confused. I have such a Ruby code: def my_function(found_objects) # ... if found_objects.second return CoreObjectFactory.get_object(found_objects.second,...
2 votes
1 answer
118 views

How to Avoid Copy Pasting Repetitive Log Calls

Currently working on a Go project of about 10k lines of code. Within the project, there's a logging library that requires a couple steps of initialization in order to actually write to the log ...
  • 139
1 vote
2 answers
937 views

c# Is using the assertion method bad or not for clean code?

I'm reading Uncle Bob Martin's Clean Code and now I have a question about the code I wrote. Is using a method as an assertion method a good or bad practice for clean code? Example // Simple example ...
  • 133
-3 votes
3 answers
1k views

Repeating CSS class contents. Structuring css code

I'm a backend developer working on a webpage, building the CSS part. I like to write as less code as possible and reuse them throughout the site, a lot of pages. I want to be able to reuse some CSS ...
  • 189
2 votes
3 answers
266 views

How to model a Struct with a limited set of values that behaves like an enum?

I have a general organization problem with my code. I'm modeling DNA, and I've created a Nucleobase struct to store the "letter" of the DNA. For ease of use, I would prefer the following functionality ...
  • 1,496
6 votes
2 answers
705 views

What could be a good pattern for a code base that does a lot of A/B testing?

I develop in an environment where we tend to do a lot of A/B testing. I am curious to learn from others on how A/B testing code is managed in your code base? Do you mix your A/B testing code with ...
  • 169
-1 votes
1 answer
860 views

How to avoid code duplication in this situation?

Case: Several methods with same params validation inside, like: public Response method1(...params...) { if (someCondition) { //do some stuff with side effects ... ...
  • 29
2 votes
1 answer
240 views

Is it good approach to create new viewModel class for every new Ui(Fragment/Activity)?

I am working on a project in which I am following clean Architecture from Google ... and for making code testable I am following an approach in which,I am creating a new ViewModel Class ViewModel ...
0 votes
1 answer
156 views

Visibility of constants

Should constants of my class only be public if I know that they will be used from their users or doesn't it hurt to make all of my constants public per se? Is not exposing all constants cleaner?
1 vote
3 answers
223 views

Writing Single responsibility methods [closed]

Let's say we have a typical service class method who does few things, input validation, db object retrieval, some business logic execution and then saving and returning record to user. This is ...
  • 121
0 votes
1 answer
212 views

Should repositories always return Rx Single or Observable?

Let' assume we have an IFiltersRepository repository that provides data about a list of "filters" that are used on the search screen of an app (filter by color, by price etc). The repository has the ...
  • 195
1 vote
0 answers
136 views

SOLID Principles and writing clean code. Am I wrong? [duplicate]

I really need some advice as I've recently come into a conflict over a difference of opinion on the SOLID principles and writing clean code. One of our, relatively new developers but senior, does not ...
1 vote
3 answers
1k views

Code design: introduce new method vs add optional parameter to existing method

In JavaScript (ES6), I can have optional function parameter with default value. So to add a new behavior to the existing code base, I could either introduce new method(s) or extend the existing method ...
  • 55

1 2
3
4 5
10