(One of the summaries of a talk at the 2014 djangocon.eu.)
Ustun Ozgur tells about Rich Hickey’s talk about simplicity, simpleness and complexity. What does “complexity” tell us as django developers?
He quotes Richard Gabriel who talks about “habitability” as a characteristic of source code that enables developers that see a piece of code to understand it and to work on it, even if they haven’t seen the code before. This is so for django.
Complexity should be fought head-on.
Look at domain driven design. Keep your architecture close to the actual domain. That helps.
Django has a couple of layers of defense against complexity.
URLs. Django routes various parts of the url to various applications. The division into seperate applications helps a lot to fight complexity.
Models and managers. Django uses several well-known design patters that help structure your code in the right way.
But… watch out for “fat” classes that do too much. You could look at mixins and compositions. Or you can move some of the fat class’s methods to separate functions.
Sometimes no code is the best code. You could put state machine state in a variable (a dict or so) and use that. Data is easier to manipulate than code.
Templates. It is the weakest point of django in terms of abstractions. Extends/includes are primitive. They lead to “scope hell”. He prefers template tags.
Building UIs is hard. The main problem is that there’s too much state. A full javascript frontend is often quite long-living. Keeping the UI’s state in sync with the server is hard. Generating the page on the server all the time is the easiest solution: the state is always up to date.
Some solution that somehow keeps the state in sync would be best. According to him it is facebook’s react.js is the solution. Why?
Everything is a component. He recommends us to read http://facebook.github.io/react/blog/2013/11/05/thinking-in-react.html .
Composition: components can have children.
Owners pass data to children through properties.
Components can use properties during rendering ({ this.props.greeting }
).
Properties are fully managed/controlled by react.js. React.js does its utmost best to make sure you do everything in a neat and ordered way: everything to make sure the state is right. This property system is one of the main ways to ensure this.
Some components have state and state changes are explicit.
If you don’t take explicit control of your state, your state takes control of you.
Keep shared “state” at the top. This means that if two components need to have access to the same state, they should be inside a parent component (“the owner”) that holds that state.
Summary:
Complexity can be managed in django because it provides solid foundations.
React.js handles the complexity on the UI side.
Rotterdam central station
My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):