Code Smell 236 - Unwrapped Lines
Tags: webdev, programming, beginners, rust
Published: 2023-12-28 13:53:00

*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

1. Wrap the code to at most 75 characters

2. Break and concatenate long strings. Compilers optimize them. Thinking there's a speed penalty is a [premature optimization](https://dev.to/mcsee/code-smell-20-premature-optimization-4moe) mistake.

3. Don't use [Abbreviations](https://dev.to/mcsee/code-smell-33-abbreviations-356o)

# 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](https://cleancodecookbook.com/), people read the code on small devices so line wrapping is critical.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/23e0186c3fb8b7abe64ca491570ddb13)
```rust
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

[Gist Url]: # (https://gist.github.com/mcsee/83600dadd790c861d6faa89b63be0e81)
```rust
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

{% post https://dev.to/mcsee/code-smell-33-abbreviations-356o %}

{% post https://dev.to/mcsee/code-smell-48-code-without-standards-2f5m %}

{% post https://dev.to/mcsee/code-smell-164-mixed-indentations-3gfc %}

{% post https://dev.to/mcsee/code-smell-211-tab-over-spaces-1d41 %}

# More Info

[Clean Code Cookbook](https://www.amazon.com/-/Maximiliano-Contieri/dp/1098144724)

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Olesya Yemets](https://unsplash.com/@ladymilkydeer) on [Unsplash](https://unsplash.com/photos/cooked-pasta-fzXVmIUsEbM)
    
* * *

> The objective of cleaning is not just to clean, but to feel happiness living within that environment.

_Marie Kondo_ 
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}