Formatting plays a crucial role in creating code that is not only functional but also readable and maintainable. In this article, I will shed light on the code smell associated with neglecting proper line wrapping and explore why it's considered bad practice, too long, isn't it?
TL;DR: Extract and wrap your code
Problems
Hard to read code (especially in small devices)
Demeter's Law violation
Solutions
Wrap the code to at most 75 characters
Break and concatenate long strings. Compilers optimize them. Thinking there's a speed penalty is a premature optimization mistake.
Don't use Abbreviations
Context
Unwrapped code formatting refers to the absence of line breaks or appropriate indentation.
It yields excessively long lines of code that extend beyond the typical width of a code editor or mobile screen.
While writing my last book, people read the code on small devices so line wrapping is critical.
Sample Code
Wrong
struct ExtraterrestrialSignal { signal_frequency: f64, signal_strength: f64, signal_duration: f64, }
fn perform_signal_processing_and_analysis(extraterrestrial_signal: &ExtraterrestrialSignal,
) {
println!(
"Extraterrestrial Signal processed - Frequency: {} Hz, Strength: {}, Duration: {} seconds", extraterrestrial_signal.signal_frequency, extraterrestrial_signal.signal_strength, extraterrestrial_signal.signal_duration);
if extraterrestrial_signal.signal_strength > 0.8 && extraterrestrial_signal.signal_duration > 10.0
{
println!("Potential Extraterrestrial Signal of interest!");
} else {
println!("Signal does not meet criteria for further investigation.");
}
}
Right
struct ExtraterrestrialSignal {
signal_frequency: f64,
signal_strength: f64,
signal_duration: f64,
}
fn perform_signal_processing_and_analysis(
extraterrestrial_signal: &ExtraterrestrialSignal,
) {
println!(
"Extraterrestrial Signal processed" +
"- Frequency: {} Hz, Strength: {}, Duration: {} seconds",
extraterrestrial_signal.signal_frequency,
extraterrestrial_signal.signal_strength,
extraterrestrial_signal.signal_duration
);
if extraterrestrial_signal.signal_strength > 0.8
&& extraterrestrial_signal.signal_duration > 10.0
{
println!("Potential Extraterrestrial"
+ " Signal of interest!");
} else {
println!("Signal does not meet criteria"
+ " for further investigation.");
}
}
Detection
[X] Automatic
This is a formatting and syntactic smell
Tags
- Formatting
Level
[X] Beginner
AI Assistants
AI assistants sometimes bring short code and not real production code.
You can use the assistants to wrap and format your code.
Conclusion
Ensuring readability is consistently paramount, with various facets warranting consideration.
When lines of code are excessively long, developers may find it challenging to understand the structure and flow of the code.
Relations
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 48 - Code Without Standards
Maxi Contieri ・ Dec 10 '20
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 164 - Mixed Indentations
Maxi Contieri ・ Sep 21 '22
More Info
Disclaimer
Code Smells are my opinion.
Credits
Photo by Olesya Yemets on Unsplash
The objective of cleaning is not just to clean, but to feel happiness living within that environment.
Marie Kondo
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (0)