aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Data
  • Practices
  • Programming
  • Tech

How to Find The Stinky Parts Of Your Code (Part X)

  • Ackley Wyndam
  • May 6, 2021
  • 6 minute read

More code smells. No joke.

We see several symptoms and situations that make us doubt the quality of our development.

Let’s look at some possible solutions.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Most of these smells are just hints of something that might be wrong. They are not rigid rules.

This is part V. Part I can be found here, Part II here, Part III is here, Part IV here, part V, VI, VII, VIII and IX.

Let’s continue…

Code Smell 46 — Repeated Code

DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.

Photo by Sid Balachandran on Unsplash

Problems

  • Code Duplication
  • Maintainability
  • Don’t Repeat Yourself

Solutions

  1. Find repeated patterns (not repeated code).
  2. Create an abstraction.
  3. Parametrize abstraction calls.
  4. Use composition and never inheritance.
  5. Unit test new abstraction.

Sample Code

Wrong

<?

final class WordProcessor {

    function replaceText(string $patternToFind, string $textToReplace) {
        $this->text = '<<<' . str_replace($patternToFind, $textToReplace, $this->text) . '>>>';
    }
}

final class Obfuscator {

    function obfuscate(string $patternToFind, string $textToReplace) {
        $this->text = strlower(str_ireplace($patternToFind, $textToReplace, $this->text));
    }
}

 

Right

<?

final class TextReplacer {
    function replace(string $patternToFind, string $textToReplace, string $subject, string $replaceFunctionName, $postProcessClosure) {
        return $postProcessClosure($replaceFunctionName($patternToFind, $textToReplace, $subject));
    }
}

//Lots of tests on text replacer so we can gain confidence.

final class WordProcessor {

    function replaceText(string $patternToFind, string $textToReplace) {
        $this->text = (new TextReplacer())->replace($patternToFind, $textToReplace, $this->text, 'str_replace', fn($text) => '<<<' . $text . '>>>');
    }
}

final class Obfuscator {

    function obfuscate(string $patternToFind, string $textToReplace) {
        $this->text = (new TextReplacer())->replace($patternToFind, $textToReplace, $this->text, 'str_ireplace', fn($text) => strlower($text));
    }
}

 

Detection

Linters can find repeated code.

There are not very good finding similar patterns.

Maybe soon machine learning will help us find such abstractions automatically.

For now, it is up to us, humans.

Examples

  • Automatic Code Generators

Tags

  • Duplication

Conclusion

Repeated code is always a smell.

Copying and pasting code is always a shame.

With our refactoring tools, we need to accept the duplication remove challenge trusting our tests as a safety net.

More info

  • Don’t Repeat Yourself

Copy and paste is a design error.

– David Parnas

Code Smell 47 — Diagrams

Diagrams are not code. They cannot be a code smell. They are just Diagram Smells.

Photo by Nick Seagrave on Unsplash

Problems

  • Maintainability
  • Trash code
  • Code Duplication
  • Diagrams focus only on structure (accidental) and not behavior (essential).

Solutions

  1. Use diagrams only to communicate ideas with other humans.
  2. Program on your favorite IDE.Thrash all diagrams.
  3. Even the ones generated from the source code.
  4. Trust your tests. They are alive and well maintained.
  5. Use Domain Driven Design technique.
Read More  Patterns For Better Insights And Troubleshooting With Hybrid Cloud Logs

Sample Code

Wrong

 

Right

w<?

final class BookItem {
    //No. We will not inherit from concrete class Book since this is another smell

    //No. We will not list a bunch of anemic attributes. since yet this is another smell

    function numberOfPages() {
        //
    }

    function language(): Language {
        //
    }

    function book(): Book {
        //Book can tell us a lot about her/his author, title etc.

    }

    function edition(): BookEdition {
        //..
    }

    //Loan and overdues are not book items responsibility

}

final class LoanTracker {
    function loan(BookItem $bookCopy, LibraryUser $reader, DatePeriod $loanDates) {
        //DatePeriod is better than anemic $fromDate and $toDate
    }
}

final class LoanTrackerTests extends TestCase {
    //Lots of maintained tests telling us how the system really works
}

Detection

We can remove all code annotations and forbid them by policy.

Examples

  • Automatic Code Generators

Conclusion

Designing is a contact sport. We need to prototype and learn from our running models.

Papers and JPGs don’t run. They live in the utopic world where everything works smoothly.

CASE was a very hot trend back in the 90s. No good system was developed with these tools.

More info

  • Laziness Chapter II: Code Wizards
  • Computer Aided Sofwtare Engineering
  • Domain Driven Design

The Diagram is Not the Model. The model is not the diagram. It is an abstraction, a set of concepts and relationships between them.

– Eric Evans

Code Smell 48 — Code Without Standards

Working on a solo project is easy. Unless you go back to it after some months. Working with many other developers requires some agreements.

Photo by De Jesus de Blas — Russia

Problems

  • Maintainability
  • Readability

Solutions

  1. Automate your styles and indentation.
  2. Enforce agreed policies.

Sample Code

Wrong

Correct sample taken from Sandro Mancuso’s bank kata

package org.craftedsw.domain;

import static org.craftedsw.domain.Amount.amountOf;

import java.io.PrintStream;
import java.util.Date;

public class MY_Account {
	//Some class have different case, underscores

	private Statement privStatement; 
	//Attributes have visibility prefixes
	
	   private Amount currentbalance = amountOf(0);

	public SetAccount(Statement statement) {
		this.statement = statement;
	}
	//Setters and getters are not normalized
	
	public GiveAccount(Statement statement) 
	{ this.statement = statement; }
	//Indentation is not uniform	

	public void deposit(Amount value, Date date) {
		recordTransaction(value, date);
		//some variables are named after type and not role.
	} 

	public void extraction(Amount value, Date date) {
		recordTransaction(value.negative(), date);
		//the opposite of *deposit* should be withdrawal
	}

	public void voidPrintStatement(PrintStream printer) 
	{
	statement.printToPrinter(printer);
	//Name is redundant
	}

	private void privRecordTransactionAfterEnteredthabalance(Amount value, Date date) {
		Transaction transaction = new Transaction(value, date);
		Amount balanceAfterTransaction = transaction.balanceAfterTransaction(balance);
		balance = balanceAfterTransaction;
		statement.addANewLineContainingTransation(transaction, balanceAfterTransaction);
		//naming is not uniform
	}	
}

 

Right

The right example has several other smells, but we keep it loyal to its GIT version in order to show only code standardization issues.

package org.craftedsw.domain;

import static org.craftedsw.domain.Amount.amountOf;

import java.io.PrintStream;
import java.util.Date;

public class Account {

	private Statement statement;
	
	private Amount balance = amountOf(0);

	public Account(Statement statement) {
		this.statement = statement;
	}

	public void deposit(Amount value, Date date) {
		recordTransaction(value, date);
	} 

	public void withdrawal(Amount value, Date date) {
		recordTransaction(value.negative(), date);
	}

	public void printStatement(PrintStream printer) {
		statement.printTo(printer);
	}

	private void recordTransaction(Amount value, Date date) {
		Transaction transaction = new Transaction(value, date);
		Amount balanceAfterTransaction = transaction.balanceAfterTransaction(balance);
		balance = balanceAfterTransaction;
		statement.addLineContaining(transaction, balanceAfterTransaction);
	}
	
}

Detection

Linters and IDEs should test coding standards before a merge request is approved.

We can add our own naming conventions related to Objects, Classes, Interfaces, Modules etc.

Read More  New Benchmarks For Securing Chrome From The Center For Internet Security

Examples

  • PSR2

Tags

  • Standardization

Conclusion

Use coding standards in your projects.

A well-written clean code always follows standards about naming conventions, formatting and code style.

Such standards are helpful because they make things clear and deterministic for the ones who read your code, including yourself.

Code styling should be automatic and mandatory on large organizations to enforce Collective Ownership.

More info

  • What Exactly Is A Name: The Quest [Part I]

The nice thing about standards is that there are so many to choose from.

– Andrew S. Tannenbaum

Code Smell 49 — Caches

Caches are sexy. They are a one-night stand. We need to avoid them in a long-term relationship.

Photo by Aimee Vogelsang on Unsplash

Problems

  • Coupling
  • Testability
  • Cache invalidation.
  • Maintainability
  • Premature Optimization
  • Erratic Behavior
  • Lack of transparency
  • Non-Deterministic behavior

Solutions

  1. If you have a conclusive benchmark and are willing to pay for some coupling: Put an object in the middle.
  2. Unit test all your invalidation scenarios. Experience shows we face them in an incremental way.
  3. Look for a real world cache metaphor and model it.

Sample Code

Wrong

<?

final class Book {

    private $cachedBooks;

    public function getBooksFromDatabaseByTitle(string $title) {

        if (isset($cachedBooks[$title])) {
            return $cachedBooks[$title];
        } else {
            return $this->doGetBooksFromDatabaseByTitle($title);
        }
    }

    private function doGetBooksFromDatabaseByTitle(string $title) {
        globalDatabase()->selectFrom('Books', 'WHERE TITLE = ' . $title);
    }
}

Right

<?

final class Book {
    //Just Book related Stuff
}

interface BookRetriever {
    public function bookByTitle(string $title);
}

final class DatabaseLibrarian implements BookRetriever {
    public function bookByTitle(string $title) {
        //Go to the database (not global hopefully)
    }
}

final class HotSpotLibrarian implements BookRetriever {
    //We always look for real life metaphors
    private $inbox;
    private $realRetriever;

    public function bookByTitle(string $title) {
        if ($this->inbox->includesTitle($title)) {
            //We are lucky. Someone has just returned the book copy.
            return $this->inbox->retrieveAndRemove($title);
        } else {
            return $this->realRetriever->bookByTitle($title);
        }
    }
}

Detection

This is a design smell.

It will be difficult to enforce by policy.

Read More  Is Anything Ever ‘Forgotten’ Online?

Tags

  • Premature Optimization

Conclusion

Caches should be functional and intelligent.

In this way we can manage invalidation.

General purpose caches are suitable only for low level objects like operating systems, files and streams.

We shouldn’t cache domain objects.

This page is hosted on a cached website.

More Info

  • What is Cache and Why do New Devs Struggle With It?
  • A Hitchhiker’s Guide to Caching Patterns

There are only two hard things in Computer Science: cache invalidation and naming things.

– Phil Karlton

Code Smell 50 — Object Keys

Primary keys, IDs, references. The first attribute we add to our objects. They don’t exist in the real world.

Photo by Maurice Williams on Unsplash

Problems

  • Coupling
  • Accidental Implementation
  • Bijection Principle Violation.

Solutions

  1. Reference object to objects.
  2. Build a MAPPER.
  3. Only use keys if you need to provide an external (accidental) reference. Databases, APIs, Serializations.
  4. Use dark keys or GUIDs when possible.
  5. If you are afraid of getting a big relation graph use proxies or lazy loading.
  6. Don’t use DTOs.

Sample Code

Wrong

class Teacher {
    static getByID(id) {
        //go to the coupled database
    }

    constructor(id, fullName) {
        this.id = id;
        this.fullName = fullName;
    }
}

class School {
    static getByID(id) {
        //go to the coupled database
    }

    constructor(id, address) {
        this.id = id;
        this.address = address;
    }
}

class Student {
    constructor(firstName, lastName, id, teacherId, schoolId) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
        this.teacherId = teacherId;
        this.schoolId = schoolId;
    }

    school() {
        return School.getById(this.schoolId);
    }

    teacher() {
        return Teacher.getById(this.teacherId);
    }
}

Right

class Teacher {
    constructor(fullName) {
        this.fullName = fullName;
    }
}

class School {
    constructor(address) {
        this.address = address;
    }
}

class Student {
    constructor(firstName, lastName, teacher, school) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.teacher = teacher;
        this.school = school;
    }
}

//If need to expose a School to an external API or a Database. Another object (not school) will keep the mapping externalId<->school and so on

 

Detection

This is a design policy.

We can enforce business objects to warn us if we define an attribute or function including the sequence id.

Tags

  • Accidental

Conclusion

Ids are not necessary for OOP. You reference objects (essential) and never ids (accidental).

In case you need to provide a reference out of your system’s scope (APIs, interfaces, Serializations) use dark and meaningless IDs (GUIDs).

More info

    • What is (wrong with) software?
    • The One and Only Software Design Principle

The nice thing about standards is that there are so many to choose from.

This article is republished from hackernoon.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Ackley Wyndam

You May Also Like
Data center
View Post
  • Data
  • Public Cloud

Data Sovereignty in Spain. It’s Not Just About the Law, It’s About Efficiency

  • June 3, 2026
View Post
  • Data
  • Platforms
  • Technology

Scaling cloud and AI: Microsoft Azure’s commitment to Europe’s digital future

  • May 11, 2026
View Post
  • Data

Streamline read scalability with Cloud SQL autoscaling read pools

  • March 23, 2026
View Post
  • Data
  • Platforms
  • Public Cloud

PayPal’s historically large data migration is the foundation for its gen AI innovation

  • March 4, 2026
Smartphone hero image
View Post
  • Gears
  • Tech

Zed Approves | Smartphones for Every Budget Range

  • January 29, 2026
View Post
  • Data
  • Technology

3 obstacles to agentic AI adoption and how to overcome them

  • December 22, 2025
Early Black Friday Gears
View Post
  • Tech

Friday Deals – And It’s Not Even Black Friday Yet

  • November 13, 2025
aster-cloud-sms-pexels-tim-samuel-6697306
View Post
  • Programming
  • Software

Send SMS texts with Amazon’s SNS simple notification service

  • July 1, 2025

Stay Connected!
LATEST
  • 1
    Expectations vs. Reality: The AI We Thought We’d Have in 10 Years
    • June 19, 2026
  • digital-nomad-freelancer-worker-2151205464 2
    One paperwork problem – Get your Digital Nomad Visa employment documents fast from UK, EU or Singapore
    • June 16, 2026
  • 3
    Samsung Art Store Brings Art Basel to Homes Worldwide With New Curated Collection
    • June 15, 2026
  • 4
    You Do Not Need to Invest in the IPO of SpaceX, Anthropic, and OpenAI
    • June 10, 2026
  • 5
    The consequences of relying on AI for accurate news
    • June 10, 2026
  • 6
    Connecting AI agents with unstructured data using Google Cloud Storage MCP Servers
    • June 10, 2026
  • 7
    WWDC26: Apple unveils next generation of Apple Intelligence, Siri AI, powerful parental controls, and an expansive set of software improvements
    • June 8, 2026
  • 8
    IBM and Google Cloud Announce Strategic Partnership to Scale AI with Human Expertise and AI‑Powered Delivery
    • June 4, 2026
  • Data center 9
    Data Sovereignty in Spain. It’s Not Just About the Law, It’s About Efficiency
    • June 3, 2026
  • 10
    Ink vs Pixels. What you miss versus what you are actually missing.
    • June 1, 2026
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Banks race to patch new cyber vulnerabilities, and other cybersecurity news
    • May 25, 2026
  • pope-leo-xiv-cq5dam-1500.844 2
    Pope Leo XIV to Publish First Encyclical on Artificial Intelligence and Human Dignity on 25 May
    • May 22, 2026
  • 3
    Portfolio to Clients, and is Strengthened by Ongoing Project Glasswing Work
    • May 20, 2026
  • reMarkable Paper Pure 4
    Everything The reMarkable Paper Pure Actually Does
    • May 14, 2026
  • 5
    Scaling cloud and AI: Microsoft Azure’s commitment to Europe’s digital future
    • May 11, 2026
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.