This document discusses using declarative programming and algebraic data types (ADTs) to concentrate on important aspects of code and check as much as possible early. It provides examples of using ADTs in Python to define types for multilingual content and ensure consistency. ADTs allow defining types as variants like an enumeration that can be matched on to concentrate on "what" rather than "how". Consistency is ensured by checking that all variants are handled. This improves coherence of components.
1 of 35
Download to read offline
More Related Content
Declarative Programming & Algebraic Data Types from Django's perspective
6. A few things to worry about
defhandle_article_form(request):
ifrequest.method=='POST':
form=ArticleForm(request.POST)
ifform.is_valid():
save_article(form.cleaned_data)
returnHttpResponseRedirect('/success/')
else:
form=ArticleForm()
returnrender(request,'article_form.html',{'form':form})
束What損 is obscured by 束How損
redundant details
Single Responsibility principle is violated
7. If we could get rid of How's once and for all,
would we miss them?
13. Different ways to do the same thing
@view_defaults(route_name='authentication',renderer='auth_form.html')
classAuthenticationHandler(object):
@view_config(request_method='POST',validate_form=EmailAuthForm)
defauth_with_email(self):
#...
@view_config(request_method='POST',validate_form=SMSAuthForm)
defauth_with_sms(self):
#...
@view_config(request_method='POST',validate_form=LoginAuthForm)
defauth_with_login(self):
#...
@view_config(request_method='POST')
defon_invalid_form(self):
#...
33. Beyond today's topic
Things worth mentioning
github.com/lihaoyi/macropy
github.com/benanhalt/PyAlgebraicDataTypes (cons ADT)
束Say What You Mean損 talk by Ryan Kelly