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.
90
votes
14answers
10k 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)...
4
votes
6answers
386 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
187 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
480 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
48 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" ...
6
votes
6answers
787 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
...
14
votes
8answers
638 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
84 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 ...
183
votes
16answers
38k 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 ...
6
votes
3answers
504 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 ...
0
votes
2answers
122 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
72 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
83 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 ...
6
votes
4answers
334 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
217 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
2answers
355 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 ...
67
votes
8answers
8k 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
173 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 ...
1
vote
0answers
42 views
What are the boundaries of the term 'Consistency' in the context of system's design?
In many books on code style or design of the software systems authors often stress out the importance of being consistent, yet they do not give a precise and clear definition for it.
Now I need that ...
0
votes
2answers
284 views
How to create multiline strings for sql queries that are readable and 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
116 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 ...
5
votes
3answers
974 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 ...
4
votes
1answer
482 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 ...
0
votes
1answer
168 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
...
5
votes
2answers
151 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 ...
9
votes
2answers
542 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
126 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
352 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
114 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 ...
16
votes
3answers
342 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)...
0
votes
1answer
116 views
Where to put peripheral use cases in android while using clean architecture
I'm trying to work myself into the Clean Architecture by Bob Martin with respect to android applications. It seems to me like there are Use Cases which do not encapsulate the Domain Layer, but belong ...
58
votes
5answers
7k 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 ...
5
votes
2answers
207 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 ...
18
votes
5answers
1k 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
143 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
44 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 ...
4
votes
2answers
184 views
Dependency Injection use in the “Clean Code” by R. Martin
In the Clean Code book by R.Martin, I have encountered the following method on p. 195:
private void parseSchemaElement(String element) throws ArgsException {
char elementId = element.charAt(0);
...
2
votes
3answers
254 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 =...
1
vote
1answer
574 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 ...
4
votes
3answers
475 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
147 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
106 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 ...
0
votes
0answers
113 views
Advice on program performance after changes
So, the situation is like this:
For the past two years, I have implemented a class in c# which is used to filter different fields from different tables in database (SQL), for reporting purposes. Now ...
1
vote
1answer
168 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 != ...
1
vote
1answer
717 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
338 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 ...
17
votes
3answers
8k views
Uncle Bob's clean architecture - An entity/model class for each layer?
BACKGROUND :
I'm trying to use Uncle Bob's clean architecture in my android app. I studied many open source projects that are trying to show the right way to do it, and I found an interesting ...
0
votes
1answer
75 views
Where should I log in the user based on http session: service or controller?
I know that best practice is to let everything about authentication/authorization to the service layer.
Controller should not be aware of that.
But how to let a service (from service layer so) to ...
2
votes
4answers
182 views
Is it a good idea to make method behavior depend on the calling thread?
I want to subclass a 3rd party class, in order to make it thread-safe.
I have a good idea of how to implement this, but there is a problem: the superclass has a property, which affects the behaviour ...