Object calisthenics

Preview:

Citation preview

Boas práticas para melhor codificaçãoObject calisthenics

Vanderlei Alves da Silva

Analista de sistemas

https://github.com/vanderleisilva

https://www.linkedin.com/in/vanderlei-alves-da-silva-73380120

vanderleisilva.github.io

Prefeitura de Santos

Codificação

Computadores são estupidos;

Seguem instruções exatas;

Sem julgamento moral;

ObjetivoMaintainability;

Readability;

Testability;

Comprehensibility;

Tonifique seu código!

Clean code

STUPID to SOLIDSingleton

Tight Coupling

Untestability

Premature Optimization

Indescriptive Naming

Duplication

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

OrigemLivro: The ThoughtWorks Anthology (Jeff Bay)

https://www.thoughtworks.com/

O que é?

AplicaçãoIndependente de tecnologia;

Boas práticas;

Dependente do paradigma orientado a objetos

9 regras1. Only One Level Of Indentation Per Method2. Don't Use The ELSE Keyword3. Wrap All Primitives And Strings4. One Dot Per Line5. Don't Abbreviate6. Keep All Entities Small7. No Classes With More Than Two Instance Variables8. First Class Collections9. No Getters/Setters/Properties

Only One Level Of Indentation Per Method

Only One Level Of Indentation Per Method

Don't Use The ELSE Keyword

Defensive Programming

Early returns

Wrap All Primitives And StringsEncapsulate all the primitives within objects.

If the variable of your primitive type has a behavior, you MUST encapsulate it. And this is especially true for Domain Driven Design. DDD describes Value Objects like Money, or Hour for instance.

Domain-driven Design Primitive Obsession

One Dot Per LineOnly talk to your immediate friends.

a.getB().getC().doSomething()

Law Of Demeter

High Cohesion

Don't AbbreviatePor que abreviar?

Nome muito grande;

Repetido várias vezes;

Golden Rule is: if you're not able to find a name for a class, then ask yourself if this class makes sense, or if you can decouple things a bit more.

Nomes não devem exceder 2 palavras Readability

Keep All Entities SmallNo class over 50 lines and no package over 10 files.

The idea behind this rule is that long files are harder to read, harder to understand, and harder to maintain.

No Classes With More Than Two Instance Variables

Two is an arbitrary choice that forces you to decouple your classes a lot.

First Class CollectionsExtensão da regra Wrap All Primitives And Strings;

Qualquer classe que contenha um array não deve conter outras propriedades;

No Getters/Setters/Properties

It is okay to use accessors to get the state of an object, as long as you don't use the result to make decisions outside the object.

Para terminar ...Tudo questão de organização;

Nosso pior inimigo é nós mesmos a 3 meses atrás

Indo alémSOLID principles;

Design Patterns;

Software AntiPatterns;

Clean Code

Perguntas!?

Até a proxima!!!

Bibliografiahttp://williamdurand.fr/2013/06/03/object-calisthenics/

http://martinfowler.com/books/refactoring.html

http://swreflections.blogspot.com.br/2012/03/defensive-programming-being-just-enough.html

https://sourcemaking.com/refactoring/smells/primitive-obsession

http://www.codeproject.com/Articles/339725/Domain-Driven-Design-Clear-Your-Concepts-Before-Yo

http://www.bennadel.com/blog/2375-object-calisthenics-in-javascript---my-first-attempt.htm

http://symfony.com/doc/current/contributing/code/standards.html

http://c2.com/cgi/wiki?LawOfDemeter

http://williamdurand.fr/2013/07/30/from-stupid-to-solid-code/

Recommended