Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Oficina de Código Limpo

Oficina de Código Limpo

Apresentação sobre boas práticas de programação. Parte de um mini-curso realizado na Faculdade Sete de Setembro.

Bruno Cavalcante

May 12, 2012
Tweet

More Decks by Bruno Cavalcante

Other Decks in Programming

Transcript

  1. "Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand." – Martin Fowler
  2. Nomes Significativos Ruim: int d; //tempo decorrido em dias Bom:

    int elapsedTimeInDays; int daysSinceCreation; Ruim: public List<int[]> getThem(); Bom: public List <int[]> getFlaggedCells();
  3. Nomes Significativos • Use o padrão da linguagem/framework • Evite

    Codificações • Exite Prefixos/Sufixos • Crie nomes pronunciáveis
  4. Nomes Significativos @updates = Update.order_by_relevance.by_users(all_following).limit(20) # Ruby print max(total_liars, total_nonliars),

    min(total_liars, total_nonliars) # Python def update_gaming_networks if NetworkGameSyncAttempt.should_update?(current_user) GamingNetwork::PSN.sync(current_user) if current_user.psn GamingNetwork::Live.sync(current_user) if current_user.live GamingNetwork::Steam.sync(current_user) if current_user.steam end end #Ruby
  5. Funções • Não passe de 20 linhas (no geral) •

    Faça somente uma coisa (em todos os casos) • Não cause efeitos colaterais • Prefira Exceptions a Error Codes
  6. Funções $(document).ready(function() { renderContent(getMovieList()); renderSourcesOptions(); renderThemeSwitcher(); }); function loadData(source) {

    switch (source) { case 'www.omdb.org' : loadDataFromOmdb(); break; case 'www.tmdb.org' : loadDataFromTmdb(); break; } } function getTotalOfMovies() { return $('tbody tr').size(); }
  7. Formatação • Limite de 120 caracteres por linha • Identação

    com espaços e seguindo o padrão da linguagem/framework Ruby: 2 espaços Python: 4 espaços Java: 4 espaços
  8. Formatação #hoppityhop.py import sys def hoppity_hop(number): if (not(number % 3)

    and not(number % 5)): print "Hop" elif (not(number % 3)): print "Hoppity" elif (not(number % 5)): print "Hophop" for i in range(1, int(sys.argv[1])): hoppity_hop(i)