of something that might be wrong. They are not rigid rules.

Code Smell 01 — Anemic Models

Your objects are a bunch of public attributes without behavior.

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

  • No Encapsulation.
  • No mapping to real world entities.
  • Duplicate Code
  • Coupling

Solutions

    1. Find Responsibilities.
    1. Protect your attributes.
    1. Hide implementations.
    1. Delegate

Examples

  • DTOs

Sample Code

	<?

	Class Window{
	  public height;
	  public width;

	  function getHeight(){
	    return $this->height;
	  }

	  function setHeight($height){
	    $this->height = $height;
	  }

	  function getWidth(){
	    return $this->width;
	  }

	  function setWidth($width){
	    $this->width = $width;
	  }

	}

Wrong

<?

	final Class Window{ 

	  function area(){
	    //...
	  }

	  function open(){
	    //..
	  }

	  function isOpen(){
	    //..
	  }

	}

Right

#code-smells #clean-code #refactoring #refactor-legacy-code #stinky-code #stinky-code-parts #pixel-face #hackernoon-top-story

How to Find the Stinky Parts of Your Code (Part I)
1.45 GEEK