Skip to content

Commit a330e7a

Browse files
authored
Merge pull request #401 from manuGil/develop
2 parents c461d76 + 0a8132e commit a330e7a

File tree

10 files changed

+1012
-76
lines changed

10 files changed

+1012
-76
lines changed

_quarto.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ website:
172172
href: docs/software/code_quality/code_smells/hardcoded_values.md
173173
- text: Deep nesting
174174
href: docs/software/code_quality/code_smells/deep_nesting.md
175-
- text: Many inputs
175+
- text: Many arguments
176176
href: docs/software/code_quality/code_smells/many_arguments.md
177177
- text: Inappropriate intimacy
178178
href: docs/software/code_quality/code_smells/inappropriate_intimacy.md
179179
- text: Side effects
180180
href: docs/software/code_quality/code_smells/side_effects.md
181-
- text: Commented out code
181+
- text: Dead code
182182
href: docs/software/code_quality/code_smells/dead_code.md
183183
- text: Online services
184184
href: docs/software/code_quality/online_services.md

docs/software/code_quality/code_smells.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ date: 2025-01-31
77

88
# We use this key to indicate the last modified date [manual entry, use YYYY-MM-DD]
99
# Uncomment and populate the next line accordingly
10-
date-modified: 2025-09-19
10+
date-modified: 2025-12-03
1111

1212
# Do not modify
1313
lang: en
@@ -116,7 +116,7 @@ Each guide provides an overview of a code smell, its symptoms and an example on
116116

117117
::: {.g-col-4}
118118
::: {.code-smell-card}
119-
##### Many Inputs
119+
##### Many Arguments
120120
**Problem:** Functions require a long list of parameters.
121121

122122
::: {.refactor-link}
@@ -149,11 +149,11 @@ Each guide provides an overview of a code smell, its symptoms and an example on
149149

150150
::: {.g-col-4}
151151
::: {.code-smell-card}
152-
##### Commented out Code
153-
**Problem:** There is a significant amount of outdated or commented-out code.
152+
##### Dead Code
153+
**Problem:** There is unused or commented-out code.
154154

155155
::: {.refactor-link}
156-
[{{< fa wrench >}} Refactor commented code](./code_smells/dead_code.md)
156+
[{{< fa wrench >}} Refactor unused code](./code_smells/dead_code.md)
157157
:::
158158
:::
159159
:::

docs/software/code_quality/code_smells/dead_code.md

Lines changed: 254 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ date: 2025-02-04
77

88
# We use this key to indicate the last modified date [manual entry, use YYYY-MM-DD]
99
# Uncomment and populate the next line accordingly
10-
date-modified: 2025-09-19
10+
date-modified: 2025-12-05
1111

1212
# Do not modify
1313
lang: en
@@ -17,7 +17,7 @@ language:
1717

1818
# Title of the document [manual entry]
1919
# Uncomment and populate the next line accordingly
20-
title: Commented-out code
20+
title: Dead code
2121

2222
# Brief overview of the document (will be used in listings) [manual entry]
2323
# Uncomment and populate the next line and uncomment "hide-description: true".
@@ -27,7 +27,7 @@ title: Commented-out code
2727
# Authors of the document, will not be parsed [manual entry]
2828
# Uncomment and populate the next lines accordingly
2929
author_1: Maurits Kok
30-
#author_2:
30+
author_2: Manuel Garcia
3131

3232
# Maintainers of the document, will not be parsed [manual entry]
3333
# Uncomment and populate the next lines accordingly
@@ -47,7 +47,7 @@ categories:
4747

4848
---
4949

50-
Dead code refers to unused or unreachable code that remains in the codebase but serves no functional purpose. Commented-out code consists of inactive code blocks that developers have disabled rather than deleting. Both contribute to clutter and reduce maintainability.
50+
Dead code refers to unused or unreachable code that remains in the codebase, but serves no functional purpose. Commented-out code consists of inactive code blocks that developers have disabled rather than deleting. Both contribute to clutter and reduce maintainability.
5151

5252
## Symptoms
5353

@@ -60,6 +60,253 @@ Dead code refers to unused or unreachable code that remains in the codebase but
6060
Do not use comments to change the behavior of the code. Instead, make use of input parameters or configuration settings to control the behavior of the code.
6161
:::
6262

63-
## Solution
64-
- If code is not needed, delete it. Use version control (e.g., Git) to restore it if necessary. Commit the removal of the commented-out code with a meaningful commit message explaining why it was removed. This allows you to track the change and easily revert it if necessary.
65-
- To change the executation of your code, use [input parameters or configuration settings](./hardcoded_values.md) to control the behavior of the code. This makes the code more readable and maintainable.
63+
## Example - Dead code
64+
65+
Unreachable or unused code that remains in the codebase but serves no purpose.
66+
67+
::: {.panel-tabset}
68+
69+
## Python
70+
71+
```python
72+
def calculate_discount(price, customer_type):
73+
if customer_type == "premium":
74+
discount = 0.20
75+
elif customer_type == "standard":
76+
discount = 0.10
77+
else:
78+
discount = 0
79+
80+
final_price = price * (1 - discount)
81+
82+
# Dead code - this variable is never used
83+
unused_result = final_price * 2
84+
85+
# Dead code - this function is never called
86+
def log_transaction():
87+
print("Transaction logged")
88+
89+
return final_price
90+
```
91+
92+
## R
93+
94+
```r
95+
calculate_discount <- function(price, customer_type) {
96+
if (customer_type == "premium") {
97+
discount <- 0.20
98+
} else if (customer_type == "standard") {
99+
discount <- 0.10
100+
} else {
101+
discount <- 0
102+
}
103+
104+
final_price <- price * (1 - discount)
105+
106+
# Dead code - this variable is never used
107+
unused_result <- final_price * 2
108+
109+
# Dead code - this function is never called
110+
log_transaction <- function() {
111+
print("Transaction logged")
112+
}
113+
114+
return(final_price)
115+
}
116+
```
117+
118+
:::
119+
120+
#### Issues
121+
122+
- `unused_result` is computed but never returned or used, wasting memory and computation.
123+
- `log_transaction()` is defined but never called, adding unnecessary clutter.
124+
- Dead code confuses readers about the actual functionality of the function.
125+
- Makes refactoring and maintenance more difficult.
126+
127+
### Solution - Dead code
128+
129+
Remove unused code completely. Use version control (e.g., Git) to restore it if needed later.
130+
131+
::: {.panel-tabset}
132+
133+
## Python
134+
135+
```python
136+
def calculate_discount(price, customer_type):
137+
if customer_type == "premium":
138+
discount = 0.20
139+
elif customer_type == "standard":
140+
discount = 0.10
141+
else:
142+
discount = 0
143+
144+
final_price = price * (1 - discount)
145+
return final_price
146+
```
147+
148+
## R
149+
150+
```r
151+
calculate_discount <- function(price, customer_type) {
152+
if (customer_type == "premium") {
153+
discount <- 0.20
154+
} else if (customer_type == "standard") {
155+
discount <- 0.10
156+
} else {
157+
discount <- 0
158+
}
159+
160+
final_price <- price * (1 - discount)
161+
return(final_price)
162+
}
163+
```
164+
165+
:::
166+
167+
## Example - Commented-out code
168+
169+
Disabling code with comments instead of deleting it or using configuration settings to control behavior.
170+
171+
::: {.panel-tabset}
172+
173+
## Python
174+
175+
```python
176+
def process_data(data, debug=False):
177+
# Debug logging is controlled by comments instead of a parameter
178+
# print("Processing started") # Commented-out for production
179+
180+
result = []
181+
for item in data:
182+
value = item * 2
183+
# print(f"Processing: {item} -> {value}") # Commented-out debug statement
184+
result.append(value)
185+
186+
# Entire feature disabled via commenting - no way to re-enable without editing code
187+
# if debug:
188+
# with open("log.txt", "w") as f:
189+
# f.write(str(result))
190+
191+
return result
192+
```
193+
194+
## R
195+
196+
```r
197+
process_data <- function(data, debug = FALSE) {
198+
# Debug logging is controlled by comments instead of a parameter
199+
# cat("Processing started\n") # Commented-out for production
200+
201+
result <- c()
202+
for (item in data) {
203+
value <- item * 2
204+
# cat(sprintf("Processing: %d -> %d\n", item, value)) # Commented-out debug statement
205+
result <- c(result, value)
206+
}
207+
208+
# Entire feature disabled via commenting - no way to re-enable without editing code
209+
# if (debug) {
210+
# write.table(result, file = "log.txt", quote = FALSE, row.names = FALSE)
211+
# }
212+
213+
return(result)
214+
}
215+
```
216+
217+
:::
218+
219+
#### Issues
220+
221+
- Commented-out code is confusing: is it there for a reason or was it forgotten?
222+
- It blocks maintenance - developers won't refactor around dead code they don't understand.
223+
- The `debug` parameter exists but is never used because the logging is disabled via comments.
224+
- Makes version history harder to follow with unclear code blocks.
225+
226+
### Solution - Commented-out code
227+
228+
Use [input parameters or configuration settings](./hardcoded_values.md) to control behavior (execution) instead of commenting out code.
229+
230+
::: {.panel-tabset}
231+
232+
## Python
233+
234+
```python
235+
def process_data(data, debug=False):
236+
"""
237+
Process data with optional debug logging.
238+
239+
Parameters:
240+
-----------
241+
data : list
242+
The data to process
243+
debug : bool
244+
If True, enable debug output
245+
"""
246+
if debug:
247+
print("Processing started")
248+
249+
result = []
250+
for item in data:
251+
value = item * 2
252+
if debug:
253+
print(f"Processing: {item} -> {value}")
254+
result.append(value)
255+
256+
if debug:
257+
with open("log.txt", "w") as f:
258+
f.write(str(result))
259+
260+
return result
261+
262+
# Usage
263+
process_data([1, 2, 3], debug=False) # Production mode
264+
process_data([1, 2, 3], debug=True) # Debug mode
265+
```
266+
267+
## R
268+
269+
```r
270+
process_data <- function(data, debug = FALSE) {
271+
# Process data with optional debug logging
272+
#
273+
# Parameters:
274+
# -----------
275+
# data : vector
276+
# The data to process
277+
# debug : logical
278+
# If TRUE, enable debug output
279+
280+
if (debug) {
281+
cat("Processing started\n")
282+
}
283+
284+
result <- c()
285+
for (item in data) {
286+
value <- item * 2
287+
if (debug) {
288+
cat(sprintf("Processing: %d -> %d\n", item, value))
289+
}
290+
result <- c(result, value)
291+
}
292+
293+
if (debug) {
294+
write.table(result, file = "log.txt", quote = FALSE, row.names = FALSE)
295+
}
296+
297+
return(result)
298+
}
299+
300+
# Usage
301+
process_data(c(1, 2, 3), debug = FALSE) # Production mode
302+
process_data(c(1, 2, 3), debug = TRUE) # Debug mode
303+
```
304+
305+
:::
306+
307+
## Key takeaways
308+
309+
- **Delete dead code immediately** - use version control to recover it if needed later.
310+
- **Do not use comments to disable code** - use parameters, configuration settings, or conditional logic instead.
311+
- **Commit removals meaningfully** - explain in the commit message why code was removed.
312+
- **Keep code clean** - cluttered code with dead sections is harder to understand and maintain.

0 commit comments

Comments
 (0)