Component design question

5 views
Skip to first unread message

J. Pic

unread,
Jun 20, 2019, 11:28:34 AM6/20/19
to django...@googlegroups.com
Hi all,

We're running some experiments replacing templates by Components
(which support channels based data-binding amongst other niceties) in
Python, i would like to run an opinion survey about two syntaxes
before moving on. Syntax 0 is what seems more "pythonic" but requires
a less strict call signature by cooking *args and **kwargs which adds
a little overhead, but in in my opinion more readable:



if not request.user.is_authenticated:
content.append(
Li(
A(_('Log in'), href=reverse('crudlfap:login')),
cls='no-padding',
)
)
else:
content.append(Li(A(
_('Log out'),
**{
'class': 'waves-effect',
'data-noprefetch': 'true',
'href': reverse('crudlfap:logout'),
}
), cls='no-padding'))

The other syntax works with a strict constructor signature, adding no
overhead and less complex but in my opinion less readable and
enjoyable:

content = [Ul([
Li([
A(
router_link_content,
{'class': 'collapsible-header waves-effect waves-teal'},
),
Div(
sublinks,
{'class': 'collapsible-body'}
)
], {
'class': self.active
})
], {
'class': 'collapsible collapsible-accordion'
})]

If you were in the position where you had to decide what syntax to
keep before moving on, how would you decide ?

Thanks in advance for your feedback

More context:
https://yourlabs.io/oss/crudlfap/commit/85cccdfef936d4303c1abde39238b7dbd688eee1
https://yourlabs.io/oss/ryzom/tree/ssr

--

J. Pic

unread,
Jun 20, 2019, 11:31:44 AM6/20/19
to django...@googlegroups.com
Better for perspective to port the first example in second syntax to compare:


if not request.user.is_authenticated:
content.append(
Li(
A([_('Log in')], dict(href=reverse('crudlfap:login'))),
dict(cls='no-padding'),
)
)
else:
content.append(Li(A(
[_('Log out')],
{
'class': 'waves-effect',
'data-noprefetch': 'true',
'href': reverse('crudlfap:logout'),
}
), {'class': 'no-padding'}))

I think I could get used to it, but I still feel like the first syntax
is nicer to use.

Looking forward to read your opinions on this !

Have a great day

J. Pic

unread,
Jun 20, 2019, 11:36:21 AM6/20/19
to django...@googlegroups.com
Damnit, my port was wrong above, here goes, with list for content and
dict for attributes as arguments instead of using *args, **kwargs:


if not request.user.is_authenticated:
content.append(
Li(
[A([_('Log in')], dict(href=reverse('crudlfap:login')))],
dict(cls='no-padding'),
)
)
else:
content.append(Li([A(
[_('Log out')],
{
'class': 'waves-effect',
'data-noprefetch': 'true',
'href': reverse('crudlfap:logout'),
}
)], {'class': 'no-padding'}))

The good side is that this mistake does make it look like it's more
error-prone than relying on some magic with *args, **kwargs. By magic
i mean: constructor take all *args, each arg that is text or a
component goes in self.content, if an arg is an Event then it goes in
self.events, although kwargs['events'] might also be set to a list ...

Would you favor readability in user code over readability in
constructor of core class Component ?

Thanks in advance

--

Reply all
Reply to author
Forward
0 new messages