DEV Community

loading...
Cover image for Code Smell 74 - Empty Lines

Code Smell 74 - Empty Lines

Maxi Contieri
Learn something new every day. - I am a senior software engineer working in industry, teaching and writing on software design, SOLID principles, DDD and TDD.
Originally published at maximilianocontieri.com Updated on ・1 min read

Breaking the code to favor readability asks for refactor.

TL;DR Don't add empty lines to your methods. Extract them!

Problems

  • Readability

  • Kiss

  • Low Reuse

Solutions

  1. Extract Method

  2. Refactor

  3. Remove unneeded lines.

Sample Code

Wrong

<?

function translateFile() {
    $this->buildFilename();
    $this->readFile();
    $this->assertFileContentsAreOk();
    //A lot of lines more

    //Empty space to pause definition    
    $this->translateHiperLinks();
    $this->translateMetadata();
    $this->translatePlainText();

    //Yet Another empty space
    $this->generateStats();
    $this->saveFileContents();
    //A lot of more lines
}
Enter fullscreen mode Exit fullscreen mode

Right

<?

function translateFile() {
    $this->readFileToMemoy();
    $this->translateContents();
    $this->saveFileContents();  
}
Enter fullscreen mode Exit fullscreen mode

Detection

This is a policy smell. Every linter can detect blank lines and warn us.

Tags

  • Readability

  • Long Methods

Conclusion

Empty lines are harmless, but show us an opportunity to break the code into small steps.

If you break your code with comments, it is also a code smell asking for a refactor.

Relations

Credits

Photo by Sigmund on Unsplash


It’s OK to figure out murder mysteries, but you shouldn’t need to figure out code. You should be able to read it.

Steve McConnell


This article is part of the CodeSmell Series.

Discussion (4)

Collapse
tudorhulban profile image
Tudor Hulban • Edited

hi,
the title is misleading. maybe it could be revisited. it covers also the case where you have a 5 lines function and recommends not to use empty lines:

func max(x, y int) int {
    if x > y {
        return x
    }

    return y
}
Enter fullscreen mode Exit fullscreen mode

the empty line provides better readability and should be there.

Collapse
mcsee profile image
Maxi Contieri Author

Hi Tudor
I disagree.
The line does not add readability, and else statement will

Collapse
tudorhulban profile image
Tudor Hulban

hi,
no issues.
else is another anti pattern.

Thread Thread
mcsee profile image
Maxi Contieri Author