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
5answers
743 views

Reducing the ripple effect in my code

I want to find out what part of "loosely coupled" code it is im failing to understand. Say I have an object Person im using in both my client and server code. If I change a certain field in Person ...
3
votes
5answers
413 views

Is consequent and efficient refactoring enemy of code reviews in a legacy code context?

There are some Tech Leads that perform code reviews for each Git pull request. Apparently, a very good practice to be aware of the evolution of code. Basically, they expect to inspect a few lines of ...
5
votes
2answers
7k views

Default value variable, null vs empty string vs false

I would like to initialize my variables at the top of my file, to prevent any Undefined variable notices. But what do you guys think is considered to be best practice (in PHP), in case of a string ...
10
votes
2answers
973 views

Avoid getters and setters, displaying user informations

Background I m reading the "Clean Code book", and, in paralel, I m working on calisthenic objects Kata like the banker account, and I m stuck on that rule : The 9th rule of calisthenic objects is ...
3
votes
2answers
2k views

Correct usage of Property vs Field vs Function in C#

I've been bothered by this line of code I've written and I've been a bit confused into what should be written instead. class SomeClass { IBeneficiary _latestBeneficiary => new Beneficiary(...
102
votes
14answers
12k views

At what point is brevity no longer a virtue?

A recent bug fix required me to go over code written by other team members, where I found this (it's C#): return (decimal)CostIn > 0 && CostOut > 0 ? (((decimal)CostOut - (decimal)...
5
votes
6answers
2k views

Command Pattern vs Functional Decomposition

Android developers probably are familiar with Ceja's Clean Architecture, where use cases are classes that implement the Command Pattern. Shvets defines the pattern intents as follows: Encapsulate a ...
4
votes
5answers
310 views

Should I take the time to clean my code once it's working if it isn't clean? [closed]

Should I take the time to clean my code once it's working if it isn't clean? Cleaning can take a lot of time. My boss just need it working but I feel unhappy if my code isn't clean.
12
votes
8answers
634 views

Proper design for a class with one method that can vary between customers

I have a class used to process customer payments. All but one of the methods of this class are the same for every customer, except for one that calculates (for example) how much the customer's user ...
1
vote
0answers
259 views

How do you manage object lifetimes in a use-case-centric architecture?

I've been experimenting with layered, use-case-centric architectures as described in Uncle Bob's Clean Architecture blog post. Most of the examples I've seen are simple "update the customer record" ...
7
votes
6answers
1k views

Calling the next method from a previous one… Why is this bad design?

If createWorld() is really long and I need to split it, I can split it to createLight(), createEarth(), createPlants(), and createAnimals(). So, naturally I do: function createLight(){ //work 1 ...
16
votes
9answers
2k views

Is redundant condition checking against best practices?

I have been developing software for the past three years, but I just recently awoke to how ignorant I am of good practices. This has led me to begin reading the book Clean Code, which is turning my ...
2
votes
2answers
647 views

Choose best function name in repository [duplicate]

In my code, I'm using repository pattern. For example, I got deal repository. I just curious how you decide the name of the function when the function itself doing so many things. I think when the ...
210
votes
16answers
45k views

My boss asks me to stop writing small functions and do everything in the same loop

I have read a book called Clean Code by Robert C. Martin. In this book I've seen many methods to clean up code like writing small functions, choosing names carefully, etc. It seems by far the most ...
15
votes
3answers
3k views

Clean code: consequences of short methods with few parameters

Recently during a code review I came across code, written by a new colleague, which contains a pattern with a smell. I suspect that my colleague's decisions are based on rules proposed by the famous ...
1
vote
2answers
313 views

Which programming technique can trim extra code generated by Dependency Injection?

Original Class class HomeController { function __construct() { $this->setPhpRenderer('Module'); $this->repository = new HomeRepository($id); $this->...
2
votes
1answer
95 views

How to Structure Data Models as Detail or Stub in C# using Code First?

Say I have a product listing backed by a SQL table. It's a large data model (truncated here for brevity) but let's say looks like this: public class Product { public int Id { get; set; } ...
3
votes
1answer
98 views

Should security code review involve stylistic culprits?

I have worked in company A for less than 1 year and I was given the task to do a security code review for a small project I was involved in the late 3 months. Until now I have been mainly been reading ...
13
votes
4answers
6k views

Techniques for minimising number of function arguments

In Clean Code, it is written that "the ideal number of arguments for a function is zero". The reasons why are explained and make sense. What I'm after is techniques to refactor methods with 4 or more ...
8
votes
1answer
309 views

What to do after after completing the project, but before I move on to the next one?

I have studied computer science and am now working in a company as a single developer on a pretty agile Java-project for almost a year. The project will soon be successfully (at least I hope so!) ...
3
votes
3answers
4k views

C# WPF : Clean Architecture

I am trying to implement Uncle Bob's Clean Architecture in my C# WPF Application. Currently my view (form) calls the presenter and the presenter calls the interactor. Should the interactor return a ...
69
votes
8answers
11k views

Naming issues: Should “ISomething” be renamed to “Something”? [closed]

Uncle Bob's chapter on names in Clean Code recommends that you avoid encodings in names, mainly regarding Hungarian notation. He also specifically mentions removing the I prefix from interfaces, but ...
1
vote
0answers
963 views

Bootstrap components and SMACSS/BEM methodologies

I have no enough experience in web development and I need advice from more experienced developers. I have been exploring SMACSS and BEM methodologies for a few weeks, and I like them, it makes really ...
4
votes
6answers
45k views

How to create multiline strings for sql queries that are readable, maintainable and fast?

I have some SQL commands that I am trying to figure out the best way to have them in code so that: 1. They are very readable 2. They would be easy to update 3. They won't be performance overhead due ...
0
votes
2answers
701 views

When to use classes, and when to use POD (PDS) + functions

Recently, I've read a blog post, that I can't find back, about how we should "free the data". The main point of the post was that we use classes and encapsulation too much since a lot of problems can ...
2
votes
1answer
834 views

What is the equivalent of Puppet's Roles/Profiles Pattern in Ansible?

In order to prevent spaghetti code in Puppet I am applying the Roles/Profiles pattern. Now I am learning Ansible and I am concerned whether spaghetti code will occur like back in the days when I ...
5
votes
3answers
7k views

Recommended value to pass instead of String parameter for a method in java

We have a method called attachDevice(Device device) which has only one argument. We had a situation to overload this method with one more parameter as like attachDevice(Device device, String ...
6
votes
1answer
1k views

Clean OOP-Design: How to implement single responsibility and no procedural programming

I am currently trying to refactor a piece of C# code that is somewhat procedurally written. I want to make the design clean, object oriented and using classes with single responsibilities. The code ...
1
vote
1answer
624 views

According to Clean Code, is it okay to add a single constructor to a plain data structure?

I have question based on the following fragments of "Clean Code". Those have been cited before, but I wanted to ask more basic question. 1. Data/Object Anti-Symmetry (...) the difference ...
4
votes
2answers
256 views

Create a new variable to shorten code

I have often wondered about the implications of shortening code by assigning temporary variables with shorter names to data accesses with long names. It's best illustrated by an example (in Ruby, but ...
14
votes
2answers
6k views

What is an output argument, as refered to in Martin's Clean Code?

On page 45 of Robert C. Martin's Clean Code: A Handbook of Agile Software Craftsmanship, Martin writes that output arguments should be avoided. I'm having trouble understanding the meaning of "output ...
-2
votes
3answers
144 views

What kind of projects/fields do programmers who enjoy code optimization find themselves in? [closed]

I think most of us strive to make our code run smoothly, read logically, and altogether function well. But for those who really enjoy taking the time to figure out how to optimize already good code (...
1
vote
2answers
3k views

Get variable with accessor method or just use dot notation?

So, I recently noticed something on some code I was writing. I could get a variable for a different class/object using dot notation to get the variable: object.someVarable or I could do it the way I ...
4
votes
1answer
310 views

Default move assignment and destruction order of members versus the rule-of-zero

Members must frequently be destroyed in the correct order. As member creation is in forward order and destruction is in reverse order this will usually work fine. However, when assignment operators ...
20
votes
3answers
3k views

Duplicating constants between tests and production code?

Is it good or bad to duplicate data between tests and real code? For example, suppose I have a Python class FooSaver that saves files with particular names to a given directory: class FooSaver(object)...
63
votes
5answers
8k views

Why are large amounts of magic numbers acceptable in CSS and SVGs?

Often times I see questions on the Hot Network Questions list like this that basically ask "how do I draw this arbitrary shape in CSS". Invariably the answer is a couple of blocks of CSS or SVG data ...
4
votes
2answers
509 views

Clean code deep parameter pass

So Clean Code says you should separate each task to a single function (and add these functions a correct name). I like the idea, but I've faced this problem so far: you receive a parameter which you ...
19
votes
5answers
2k views

Can a programming language by design enforce “clean code”? [closed]

So I'm coding my first projects in C++ and it seems that it takes more effort to make the code "clean", rather than merely work. I.e. it seems as if C++ "allows" to write ugly, but working code. ...
2
votes
3answers
177 views

What is the least bad way of knowing the type of a key from Json file

in our app we have a Json file that contains several properties used for menu configuration. One of the keys should tell us if a menu item should be displayed. The thing is that the value for this key,...
0
votes
1answer
99 views

Bound Variadic Arguments: Bitmask vs Array

Intro The following assumes C# as a reference language, or any other statically typed language. This is not a question specific to Unity's API, but it is used as an example. In Unity's API, most ...
3
votes
2answers
2k views

Dependency injection use in “Clean Code” by Robert Martin

In Robert Martin's Clean Code, I encountered the following method on p. 195: private void parseSchemaElement(String element) throws ArgsException { char elementId = element.charAt(0); String ...
3
votes
3answers
2k views

Private string constants for map keys

Is it good practice to define private constant strings that have the same name as their values? Take the following code for example. public class Example { private static final String FIRST_KEY =...
6
votes
1answer
4k views

Why use Razor Syntax?

Was doing some reading today about Razor Syntax with MVC Framework and was wondering why would/should I use Razor? What benefit does it provide over doing the same thing in the code behind and/or ...
5
votes
3answers
1k views

Are for loops allowed in the “Clean Code” set of rules?

Given the set of rules explained in "Clean Code", can one really use for loops? My confusion stems form the fact that a for loop is a low-level construct per se, thus it can only be used at the very ...
2
votes
2answers
1k views

Code size overhead by including unnecessarily extra header files

I have a program which includes lots of header files but it do not uses all the header files. I have removed some of them although it is working fine. I did not notice any changes in the performance. ...
0
votes
2answers
127 views

Design pattern to holds API exchanges? [closed]

I'm developing a simple application that crawls in web pages to obtain some information. For this I used and tested some libraries, like crawler4j, jsoup, jaunt and htmlunit. I exchanged several ...
1
vote
1answer
245 views

Sorting Array before looping : best practice

I was going through JBAKE code at https://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58 PFB the code. Why are we sorting the array here? if (assets != ...
5
votes
1answer
9k views

flatMap with if else vs combine with filter

When using reactive frameworks I have seen both solutions below to make a mutually exclusive selection of which stream will be forwarded (rxjava in this case) Observable.merge( Observable.just(...
-3
votes
2answers
855 views

Why ever use exception throw (in C#) except for Class Library development? [duplicate]

Why would I ever throw exceptions in code? (except for one specific scenario where I am developing a class library which the consumers of it, will not want / be able to change). To be more specific, ...
8
votes
1answer
1k views

Refactoring byzantine payment processing code on a limited budget [closed]

I've been working on a large Ruby on Rails application for several years. It was inherited in a poor state but most of the production bugs have been ironed out with time. There are some sections that ...

1 4 5 6 7 8