The usual way in which we plan today for tomorrow is in yesterday’s vocabulary. We do so, because we try to get away with the concepts we are familiar with and that have acquired their meanings in our past experience. Of course, the words and the concepts don’t quite fit because our future differs from our past, but then we stretch them a little bit.
On the cruelty of really teaching computer science
-
Zaczynamy od przeczytania/przejrzenia 🛁 Clean Code concepts adapted for Ruby.
Na wykładzie wyjaśnimy dokładniej niektóre opisanych tam zaawansowanych rzeczy, na przykład co oznacza skrótowiec SOLID, co rozumiemy przez refaktoryzację kodu oraz czy kod który smells to przeciwieństwo kodu który jest clean. -
Literatura – na początek wystarczą nam dwie książki Sandi Metz i jej prezentacja z YouTube:
-
Go Ahead, Make a Mess; zob. też Joe Armstrong, one of the inventors of Erlang, The Mess We’re In.
-
Ruby is very friendly. The language permits nearly anyone to create scripts to automate repetitive tasks, and an opinionated framework like Ruby on Rails puts web applications within every programmer’s reach.
The syntax of the Ruby language is so gentle that anyone blessed with the ability to string thoughts into logical order can produce working applications. Programmers who know nothing about object-oriented design can be very successful in Ruby.
Metrics, Duplication, Churn, Style – koniec XX wieku.
-
Cloc (counts).
-
Flog (ABC complexity).
-
Flay (structural similarities).
-
Turbulence (churn&complexity).
-
RuboCop – static code analyzer (a.k.a. linter) and code formatter (style).
Zob. też Breaking Up the Behemoth – Design Stamina Hypothesis, Procedural vs Object-Oriented Code, Churn and Complexity.
Do wyszukiwania pain in Ruby code użyjemy narzędzia Flog – the higher the score, the more pain the code is in; zob. też Bryan Helmkamp, Deciphering Ruby Code Metrics
def verse(n)
"#{n == 0 ? 'No more' : n} bottle#{'s' if n != 1}" +
" of beer on the wall, " +
"#{n == 0 ? 'no more' : n} bottle#{'s' if n != 1} of beer.\n" +
"#{n > 0 ? "Take #{n > 1 ? 'one' : 'it'} down and pass it around"
: "Go to the store and buy some more"}, " +
"#{n-1 < 0 ? 99 : n-1 == 0 ? 'no more' : n-1} bottle#{'s' if n-1 != 1}"+
" of beer on the wall.\n"
end
flog -ad verse.rb # --all --details
36.2: flog total
36.2: flog/method average
36.2: main#verse verse.rb:1-8
15.2: branch
7.0: +
6.5: -
5.3: ==
5.2: !=
4.1: lit_fixnum
3.3: >
1.4: <
See Chapter 10. Text Formatting for examples of colored, line-through formatting.
No rules KATA.
Rules.
-
Baruco 2013: Rules, by Sandi Metz. No more than
-
100 lines per class
-
5 lines per method
-
3 parameters per method
-
-
SOLID – coined by Michael Feathers and popularized by Robert Martin:
-
Single Responsibility,
-
Open-Closed,
-
Liskov Substitution,
-
Interface Segregation,
-
Dependency Inversion.
-
-
DRY (Don’t Repeat Yourself) – Andy Hunt and Dave Thomas’s
-
LoD (the Law of Demeter) – the Demeter project at Northeastern University
-
Our rules?
-
Replace Conditional with Polymorphism
-
Wyszukujemy i naprawiamy problemy w kodzie.
Code smells, refactorings – początek XXI wieku, Kent Beck.
Uruchomić RubyCritics – a Ruby code quality reporter – na większym projekcie napisanym w języku Ruby.
Programu reek można użyć do wypisania „zapachów” w projekcie napisanym w języku Ruby:
reek -f json KATALOG | jq .[].documentation_link | sort | uniq -c | sort -n
Jeśli katalog zawiera nie tylko pliki z kodem w języku Ruby, to modyfikujemy nieco to polecenie, na przykład tak:
find lib -name '*.rb' | xargs reek -f json | jq .[].documentation_link | sort | uniq -c | sort -n
Tutaj jest oficjalna lista code smells i refaktoryzacji dla języka Ruby i Java. Znaleźć trzy najczęściej występujące oficjalne zapachy w jakimś większym projekcie Ruby.
Na ile linijek kodu, średnio, wypada jeden zapach.
Zakładamy konto na portalu Exercism. Następnie wybieramy tracks z językiem obiektowym i przerabiamy ćwiczenia (10+).