paint-brush
How to Find The Stinky Parts of Your Code (Part VII)by@mcsee
228 reads

How to Find The Stinky Parts of Your Code (Part VII)

by Maximiliano ContieriDecember 30th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Part I can be found here, Part II, Part III, Part IV and part V and the last one (for now) This is part V. of a series of symptoms and situations that make us doubt the quality of our development. Some of these smells are just hints of something that might be wrong. They are not rigid rules. We see several symptoms that make people doubt quality of their development. We need to write software for humans, not software for compilers, not compilers. We need software for the 2020, not a long descriptive name.

People Mentioned

Mention Thumbnail

Company Mentioned

Mention Thumbnail
featured image - How to Find The Stinky Parts of Your Code (Part VII)
Maximiliano Contieri HackerNoon profile picture

Yet more code smells? Plenty of!

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

Let's look at some possible solutions.

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 and the last one (for now).

Let's continue...

Code Smell 31 — Accidental Methods on Business Objects

Adding persistence, serialization, displaying, importing, exporting code to an object bloats its protocol and brings coupling.

Photo by Robert Bye on Unsplash

Problems

  • Readability
  • Coupling
  • Maintainability

Solutions

  1. Keep your objects clean.
  2. Decouple business objects.
  3. Separate accidental concerns:
  4. Move Persistence, Formatting, Serialization to special objects.
  5. Keep essential protocol using bijection.

Examples

  1. Persistence
  2. Identifiers
  3. Serialization
  4. Formatting

Sample Code

Wrong

Right

Detection

It is difficult (but not impossible) to create linting rules based on naming and hinting for suspect names.

Exceptions

  • Some frameworks force us to inject dirty code in our objects. (For example identifiers).

We should try to use better languages/frameworks.

Tags

  • Declarative

Conclusion

We are very used to see business objects polluted. This is normal. We need to reflect on the consequences and coupling from these designs.

Simple things should be simple, complex things should be possible.

Alan Kay

Code Smell 32 — Singletons

The most used and (in)famous design pattern in the world is causing us great harm.

Photo by Maria Teneva on Unsplash

Problems

  • Coupling
  • Testability
  • Accidental implementation problems.
  • Multi threading issues.
  • Static methods polluting.
  • Object creation contract violation.
  • Bijection mismatch.
  • Memory issues.
  • Premature Optimization.

Solutions

  • Avoid it.
  • Use contextual unique objects.
  • Benchmark object creation.

Examples

  • Database Access
  • Globals
  • Loggers
  • Helpers

Sample Code

Wrong

God is the archetypical singleton example.

Right

Detection

This is a design pattern. We should avoid it by policy.

We can add linter rules for patterns like ‘getInstance()’ so new developers cannot infect code with this anti-pattern.

Tags

  • Globals

Conclusion

This is an historical mistake already acknowledged by the community. Nevertheless, lazy developers bring it again and again. We need to reach a consensus on its drawbacks.

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 33 — Abbreviations

Abbreviating is very important so that we look smart and save memory and mind space.

Photo by Jessica Knowlden on Unsplash

Problems

  • Coupling
  • Bad Naming
  • Declarativeness
  • Ambiguity
  • Readability
  • Premature Optimization

Solutions

Use meaningful and declarative names.

Examples

  • Variable naming
  • Function naming
  • Package naming
  • Class Naming

Sample Code

Wrong

Right

Detection

We can’t automate choosing what is a short name and a declarative.

Some “modern” and shinny languages enforce this bad practice. So we should wisely choose a good language instead.

Tags

  • Declarative

Conclusion

Computer science was born from the mother of science (mathematics). In math, the assignment of single letter variables (i, j, x, y) is a good practice.

The concept of reference arose from the variable.

Many people wondered why mathematicians can work with such short variables, and computer scientists cannot.

For mathematicians, once entered into a formula, variables lose all semantics and become indistinguishable.

Our brain wastes a lot of energy figuring out what is the meaning of an abbreviation.

It is 2020, We need to write software for humans, not for compilers.

A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment.

Robert Martin

Code Smell 34 — Too Many Attributes

A class defines objects with lots of attributes.

Photo by Andy Li on Unsplash

Problems

  • Low Cohesion
  • Coupling
  • Maintainability
  • Readability

Solutions

  1. Find methods related to attributes.
  2. Cluster these methods.
  3. Break the object related to those clusters.
  4. Find real objects related to this new objects and replace existing references.

Examples

  • DTOs
  • Denormalized table rows

Sample Code

Wrong

Right

Detection

Most linters warn when you declare too many attributes. Setting a good warning threshold should be easy.

Tags

  • primitive

Conclusion

Bloated objects know too much and are very difficult to change due to cohesion.

Developers change these objects a lot, so they bring merge conflicts and are a common problems source.

So much complexity in software comes from trying to make one thing do two things.

Ryan Singer

Code Smell 35 — State as Properties

When an object changes its state the best solution is to change the attribute, isn’t it?

Photo by Tom Crew on Unsplash

Problems

  • Mutability
  • Attributes polluting
  • Setters

Solutions

  • Model states as mathematical set inclusion.
  • State is accidental, take it away from the object.

Examples

  • State diagrams

Sample Code

Wrong

Right

Detection

If we want to be extreme, we should consider every setter to be a potential state change. Linters can warn us. But we might end up getting too many false positives.

Exceptions

  • Over Design
  • Performance issues (if a serious benchmark supports it).

Tags

  • Mutation

Conclusion

This technique is very elegant but can lead to over design. For example changing a visual component's color should be a counterexample to this smell.

We should be aware and very caution like with any other smell.

They are hints and not rigid rules.

First make the change easy (warning: this might be hard), then make the easy change.

Kent Beck

Those were the first 35. Nevertheless, I keep getting more suggestions on twitter, so they won't be the last!