Why the first instruction we learn to program should be the last to use.

Image for post
Image for post

Nobody uses GOTO instruction anymore and few programming languages still support it.

We have matured and confirmed spaghetti code is unmaintainable and error prone. Structured Programming solved that problem years ago.

We got rid of the sentence thanks to Edsger Dijkstra’s incredible paper: Go To Statement Considered Harmful.

Next evolution step will be removing most IF statements

IFs/Cases and Switches are GOTOs disguised as structured flow.

Our tool will be Object Oriented Programming principles.


Incomplete objects cause lots of issues.

Image for post
Image for post
Photo by Brett Jordan in Pexels

Problems

  • Mutability
  • Incomplete objects
  • Concurrency inconsistencies between creation and essence setting.
  • Setters

Solutions

  • Pass the object’s essence on creation

Examples

  • Some persistence frameworks in static typed languages require an empty constructor.

Exceptions

  • Stateless objects. Always better solution than static class methods.

Sample Code

Wrong

Right

Detection

Any linter can warn this (possible) situation.

Tags

  • Essence
  • Incomplete
  • Mutable

Conclusion

Always create complete objects. Make their essence immutable to endure through time. Every object needs its essence to be a valid one since inception.

Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI (Very Large Scale Integration)…


The code smells bad. Let’s see how to change the aromas.

Image for post
Image for post

In this serie we will see several symptoms and situations that make us doubt the quality of our developments.

We will present possible solutions.

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

Code Smells


Your objects are a bunch of public attributes without behavior.

Image for post
Image for post
Photo by Stacey Vandergriff on Unsplash

Protocol is empty (with setters/getters).

If we ask a domain expert to describe an entity he/she would hardly tell it is ‘a bunch of attributes’.

Problems

Solutions

1) Find Responsibilities.

2) Protect your attributes.

3) Hide implementations.

4) Delegate

Examples

  • DTOs

Sample Code

Wrong

Right

Detection

Detection can be automated with sophisticated linters ignoring setters and getters and counting real behavior methods.

Also Known as

  • Data Class

Tags

  • Anemic
  • OOP as Data
  • Encapsulation
  • Setters/Getters
  • Mutability

More info

This…


What I would say to myself if I could travel to my past.

Image for post
Image for post

Today I wrote a tweet asking for recommendations for junior developers

I got a lot of very interesting answers. I will group them down here.

Tweets are textual, I chose the more representative, corrected some typos and abbreviations and added some references.

I Agree with

I’m not a Jr Dev, yet. But if I had the experience, my advice to a Jr. Dev and to my future self is; be a student for the rest of your career. Listen to every dev no matter the age and experience. Stay humble, that will make people relied on you and reliable means stable job.


Classes are handy. We can call them and invoke them any time. Is this good?

Image for post
Image for post
Photo by Marco Bianchetti on Unsplash

Problems

  • Coupling
  • Extensibility
  • Hard to mock

Solutions

  1. Use interfaces or traits (if available).
  2. Use Dependency Injection.
  3. Favor Loose Coupling.

Sample Code

Wrong

Right

Detection

We can use almost any linter to find references to classes. We should not abuse since many uses might be false positives.

Tags

  • Coupling

Conclusion

Dependencies to Interfaces make a system less coupled and thus more extensible and testable.

Interfaces change less often than concrete implementations.

Some objects implement many interfaces, declaring which part depends on which interface makes the coupling more granular and the object more cohesive.

Relations

More info

When your code depends on an interface, that dependency is usually very minor and unobtrusive…


Classes are handy. We can call them and invoke them any time. Is this good?

Image for post
Image for post
Photo by Alfons Morales on Unsplash

Problems

  • Coupling
  • Classes are global unless we use Namespaces.
  • Name polluting
  • Static Methods
  • Static Constants
  • Singletons

Solutions

  1. Use namespaces, module qualifiers or similar
  2. Avoid namespace polluting, keep the Global names as short as possible.
  3. Class single Responsibility is to create instances.

Sample Code

Wrong

Right

Detection

We can use almost any linter or create dependency rules searching for bad class references.

Tags

  • Globals

Conclusion

We should restrict our classes to small domains and expose just facades to the outside. This greatly reduces coupling.

Relations

More info

Write shy code — modules that don’t reveal anything unnecessary to other modules and that don’t rely on other modules’ implementations.

Dave Thomas

This…


El primer ejemplo es el primer codesmell de nuestra carrera

Image for post
Image for post
Photo by Octavian Dan on Unsplash

Todos los tutoriales que he leído durante los últimos 30 años comienzan con el infame ejemplo de “Hola mundo”. Esta podría ser una de las razones por las que escribimos software de mala calidad.


sort, doSort, basicSort, doBasicSort, primitiveSort, superBasicPrimitiveSort, who does the real work?

Image for post
Image for post
Photo by Roger Bradshaw on Unsplash

Problems

  • Readability
  • Bad Naming
  • Low Cohesion
  • Single Responsibility Principle

Solutions

  1. Use good object wrappers
  2. Use dynamic decorators

Sample Code

Wrong

Right

Detection

We can instruct our static linters to find wrapping methods if they follow conventions like doXXX(), basicXX() etc.

Tags

  • Declarativeness

Conclusion

We came across this kind of methods some time in our developer life, We smelled something was not OK with them. Now is the time to change them!

More info

The primary disadvantage of Wrap Method is that it can lead to poor names. …

Maximiliano Contieri

I’m senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store