DEV Community

Cover image for Code Smell 48 - Code Without Standards
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

3

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.

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

public class MY_Account {
// This class name has a different case and 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
// wrapped lines are not consistent
}
}

Right

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);
}
}
view raw standards.java hosted with ❤ by GitHub

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

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.

Examples

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.

Relations

More info

Naming Conventions


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

Andrew S. Tannenbaum

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

Top comments (1)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

a while ago I started adding tests to my projects that check indentation and it has saved me from many accidental space-indented commits already 😁

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay