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.
116
votes
11answers
41k views
Why is Clean Code suggesting avoiding protected variables?
Clean Code suggests avoiding protected variables in the "Vertical Distance" section of the "Formatting" chapter:
Concepts that are closely related should be kept vertically close to each other. ...
100
votes
16answers
5k views
Should I remove unreferenced code?
I'm working on a medium sized (100k lines) code base, it's all relatively recent code (less than a year old) and has good unit test coverage.
I keep coming across methods which are either no longer ...
80
votes
23answers
8k views
Is fewer lines of code always better? [closed]
Which of these programming styles is better?
var result = methodOne(methodTwo(a, methodThree(b)), c, d);
or
var result3 = methodThree(b);
var result2 = methodTwo(a, result3);
var result = ...
65
votes
9answers
8k views
Clean Code comments vs class documentation
I'm having some discussions with my new colleagues regarding commenting. We both like Clean Code, and I'm perfectly fine with the fact that inline code comments should be avoided and that class and ...
57
votes
4answers
7k views
Is it better to call a function that doesn't have an effect at that point, IF it improves code clarity?
I have three views in my program (iOS app). Only one of them is ever active at the same time so I set the visibility off for two of them and switch visibility as the user presses buttons. The views ...
44
votes
20answers
5k views
Should you write good documentation and clean code to increase the “Bus Factor”?
One of the main goals of software development companies is to increase their Bus factor This is also advocated in a talk that was organized by Google.
That means that you should code and document ...
43
votes
12answers
3k views
Coding style (do more, then simplify) [duplicate]
I'm a CS student and I have been coding for a few months shy of a year now, and I seem to have developed what I think may be a "bad" habit and I'm wondering if anyone does the same (or whether it's a ...
37
votes
14answers
5k views
How important is it to clean up someone else's code when faced with a tight deadline? [closed]
(I'm talking about HTML / CSS code (not programming languages) but I think we also face the same issue as with programmers.)
I am the senior front-end designer in a team and I often have to re-work ...
37
votes
13answers
5k views
What is best practice on ordering parameters in a function?
Sometimes (rarely), it seems that creating a function that takes a decent amount of parameters is the best route. However, when I do, I feel like I'm often choosing the ordering of the parameters at ...
28
votes
9answers
2k views
How clean should new code be? [duplicate]
I'm the lead designer in our team, which means I'm responsible for the quality of the code; functionality, maintainability and readability.
How clean should I require my team members' code to be if ...
27
votes
5answers
3k views
Pass ID or Object?
When providing a business logic method to get a domain entity, should the parameter accept an object or an ID? For example, should we do this:
public Foo GetItem(int id) {}
or this:
public Foo ...
26
votes
6answers
1k views
What payoffs have you seen from taking care of technical debt?
This article on technical debt has some good points, including:
Working on the "technical matters" works best when it is driven by stories. The code base is probably in need of work everywhere, ...
22
votes
18answers
2k views
Why write clean, refactored code? [duplicate]
This is a question I've been asking myself for a long time. Thought of throwing out it to you.
From my experience of working on several Java based projects, I've seen tons of codes which we call ...
22
votes
6answers
1k views
How do I prevent unknowningly duplicating code?
I work on a rather large code base. Hundreds of classes, tons of different files, lots of functionality, takes more than 15 minutes to pull down a fresh copy, etc.
A big problem with such a large ...
21
votes
7answers
1k views
Does software rot refer primarily to performance, or to messy code?
Wikipedia's definition of software rot focuses on the performance of the software. This is a different usage than I am used to; I had thought of it much more in terms of the cleanliness and design of ...
19
votes
3answers
2k views
Does TDD lead to the good design? [duplicate]
I'm in transition from "writing unit tests" state to TDD.
I saw as Johannes Brodwall creates quite acceptable design from avoiding any of architecture phase before. I'll ask him soon if it was real ...
18
votes
4answers
3k views
Is code like this a “train wreck” (in violation of Law of Demeter)?
Browsing through some code I've written, I came across the following construct which got me thinking. At a first glance, it seems clean enough. Yes, in the actual code the getLocation() method has a ...
17
votes
15answers
2k views
Is there any benefit to obsession with making code “look pretty”?
Sometimes I spend ridiculous amounts of time (hours) agonizing over making code "look pretty". I mean making things look symmetrical. I will actually rapidly scroll through an entire class to see if ...
16
votes
6answers
6k views
The rule of 5 - to use it or not?
The rule of 3 (the rule of 5 in the new c++ standard) states :
If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to ...
16
votes
16answers
2k views
Do else blocks increase code complexity? [closed]
Here is a very simplified example. This isn't necessarily a language-specific question, and I ask that you ignore the many other ways the function can be written, and changes that can be made to it.. ...
16
votes
8answers
2k views
Ways to break the “Syndrome of the perfect programmer” [closed]
I am probably not the only one that feel that way. But I have what I tend to call "The syndrome of the perfect programmer" which many might say is the same as being perfectionist but in this case it's ...
13
votes
3answers
412 views
Low impact refactoring and code cleaning of sloppy code while waiting for requirements
I inherited an existing code base for a product that is reprehensibly sloppy. The fundamental design is woefully inadequate which unfortunately I can do little about without a complete refactor (HIGH ...
12
votes
6answers
994 views
Is there a case study that convincingly demonstrates that clean code improved development?
I'm in my first real job as programmer and what I see is just the "Big Ball of Mud" code (without useful comments too), but I like to do clean code, and it's really hard for me to code in a worse way.
...
12
votes
4answers
4k views
Good use of try catch-blocks?
I always find myself wrestling with this... trying to find the right balance between try/catching and the code not becoming this obscene mess of tabs, brackets, and exceptions being thrown back up ...
12
votes
4answers
1k views
Do I suffer from encapsulation overuse?
I have noticed something in my code in various projects that seems like code smell to me and something bad to do, but I can't deal with it.
While trying to write "clean code" I tend to over-use ...
10
votes
6answers
1k views
Best practice boolean assignment [closed]
I came across the following conditional in a program that I have taken over from another developer:
if (obj.Performance <= LOW_PERFORMANCE)
{
obj.NeedsChange = true;
}
else
{
...
10
votes
7answers
1k views
Is it bad style to redundantly check a condition?
I often get to positions in my code where I find myself checking a specific condition over and over again.
I want to give you a small example: suppose there is a text file which contains lines ...
10
votes
6answers
898 views
Which is more maintainable — boolean assignment via if/else or boolean expression?
Which would be considered more maintainable?
if (a == b) c = true; else c = false;
or
c = (a == b);
I've tried looking in Code Complete, but can't find an answer.
I think the first is more ...
10
votes
5answers
705 views
Can a pure-functional solution to this problem be as clean as the imperative?
I have an exercise in Python as follows:
a polynomial is given as a tuple of coefficients such that the powers are determined by the indexes, e.g.: (9,7,5) means 9 + 7*x + 5*x^2
write a function to ...
9
votes
6answers
580 views
Is it better to have one method that takes a bool as a parameter or two methods with different names? [duplicate]
I'm writing a factory class for a Selenium Web Driver and I came across a choice that I couldn't figure out which is cleaner. Having two methods with the same parameter.
GetWebDriver(string browser)
...
9
votes
6answers
2k views
Checking if a method returns false: assign result to temporary variable, or put method invocation directly in conditional?
Is it a good practice to call a method that returns true or false values in an if statement?
Something like this:
private void VerifyAccount()
{
if (!ValidateCredentials(txtUser.Text, ...
9
votes
3answers
869 views
Clean Code and Hybrid Objects and Feature Envy
So I recently made some major refactorings to my code. One of the main things I tried to do was split out my classes into data objects and worker objects. This was inspired, among other things, by ...
8
votes
6answers
3k views
How to reduce a switch in a switch statement?
So I'm making a method to create a salutation line based on two people from a database.
There are four parameters: the two names (name1 and name2) and the two genders (gender and gender2).
For every ...
8
votes
4answers
879 views
Using a “dead man's switch” to manage time-sensitive code
In our software environment, we often run a/b tests, as is probably good practice. However, our environment is set up such that, in very short order, the code starts to become very crufty with dead ...
8
votes
2answers
863 views
Applying the principles of Clean Code to functional languages
I'm currently reading Robert Martin's Clean Code. I think it's great, and when writing OO code I'm taking his lessons to heart. In particular, I think his advice to use small functions with meaningful ...
8
votes
4answers
1k views
Design pattern for handling a response
Most of the time when I'm writing some code that handles the response for a certain function call I get the following code structure:
example: This is a function that will handle the authentication ...
8
votes
6answers
659 views
What are good ways of balancing informative exceptions and clean code?
With our public SDK, we tend to want to give very informative messages about why an exception occurs. For example:
if (interfaceInstance == null)
{
string errMsg = string.Format(
...
8
votes
3answers
689 views
Implementing a complexity hiding layer
As part of the dependencies that the project I'm working on has, we use several core services. These services, to which we can't make big changes, are a big mess. Depending on the method we invoke, we ...
8
votes
3answers
323 views
Where should I put methods that make an Http Request to get data from a web service in iOS development?
I have a Model Car in my iOS application where it's parameters like name, year, value etc are fetched from a web service in order to fill a list with cars data.
Where should I put the method that ...
7
votes
3answers
648 views
Cleaning Up Generated Code: Refactor or Map?
Context:
I recently had to deal with a class file generated by XSD.exe. It was 3500 lines long with ridiculously-verbose class / variable names (think ...
7
votes
5answers
894 views
Is it best to use “get” as a prefix for getters?
if I had a boolean (property) shouldAutoLogin is it better to name the getter getShouldAutoLogin or just shouldAutoLogin so that it reads more like English?
ex :
if(shouldAutoLogin){
...
}
or ...
7
votes
4answers
354 views
How do I link an domain object in memory to its database records without cluttering the domain with database concerns?
Your domain model contains a set of objects. I'm here presenting a side project, but I have a much more complicated work project falling to its knees because I didn't do a good separation of the ...
7
votes
2answers
337 views
How to hide AOP implementation dependency without breaking encapsulation?
I have the concept of a SlowLoading thing:
public interface SlowLoading {
boolean hasLoaded();
}
I also have a component MyComponent:
public interface myComponent{
void doSomething();
}
My ...
6
votes
6answers
307 views
Is code maintenance typically a special project, or is it considered part of daily work?
Earlier, I asked to find out which tools are commonly used to monitor methods and code bases, to find out whether the methods have been getting too long.
Most of the responses there suggested that, ...
6
votes
3answers
303 views
Dependency Injection vs Mixing Levels of Abstraction
I've been reading Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin. One point he makes:
G34 Functions should descend only one level of abstraction
However, I'm ...
6
votes
4answers
733 views
How do I introduce clean code?
My team has about 18 members and the code is generally good, using TDD and good specifications makes for working code. But I think they abuse comments in favor of writing cleaner, more readable code.
...
6
votes
2answers
408 views
Web API URI Schema Design
I'm in the middle of designing an API for a very basic flashcard application for learning purposes and I'm wondering if you all think there can be any improvements.
In the app, a Folder contains ...
5
votes
5answers
2k views
How should I handle exception that *should* never be thrown? [duplicate]
What is the best way to handle errors that shouldn't ever happen?
My current way to do this is to throw an exception if the 'thing that shouldn't happen' does happen, like so:
/*
* Restoring from a ...
5
votes
3answers
733 views
Is the COCOMO model a good argument when defending a programming language choice?
Currently, I'm following a course on embedded software development. The lecturer has chosen J as an architecture language for model-driven software development. J itself is a very terse programming ...
5
votes
3answers
2k views
Is there a cleaner way of caching data
We've started implementing some caching code in our MVC project. We've derived from .Net's ObjectCache to store it in a SQL database.
What we do right now is in each method we want to cache we have ...