<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in code-formatting people: I need your help</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="http://tplus1.disqus.com/code_formatting_people_i_need_your_help/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 16 Dec 2008 10:34:09 -0000</lastBuildDate><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4428804</link><description>&lt;p&gt;Here's how I would format these:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;cat1, cat2, cat3, cat4, cat5 = [self.categories[x] for x in range(1, 6)]&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;since it fits in 78 columns (duh!).  If it didn't, I'd try&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;cat1, cat2, cat3, cat4, cat5 = [self.categories[x]&lt;br&gt;                                for x in range(1, 6)]&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;cat1, cat2, cat3, cat4, cat5 = [&lt;br&gt;        self.categories[x] for x in range(1, 6)]&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;On to the second example.  I find those repeated "1 if condition else None"&lt;br&gt;expressions annoying; surely a helper can make them readable&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;def output_bool(self, x):&lt;br&gt;    return 1 if x else None&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I'd name that helper $outputformat_bool for whatever output format you're&lt;br&gt;producing.&lt;/p&gt;

&lt;p&gt;Next, I'd be inclined to put every tuple element on a separate line, if at&lt;br&gt;least one of them is so large that it doesn't fit on a single line.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;def my_send_methods(self, SendMethod, disabled=False):&lt;br&gt;    return [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;,&lt;br&gt;             x.display_name,&lt;br&gt;             dict(selected=output_bool(x == self.preferred_send_method),&lt;br&gt;                  disabled=output_bool(disabled)),&lt;br&gt;            ) for x in SendMethod.constants.values()]&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;For the third example, I see your argument, but I think the intimidation factor&lt;br&gt;of a single expression spanning 9 lines of code is worse than having people&lt;br&gt;explicitly make sure the expressions are independent:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;employees = ([(0, 'None')] +&lt;br&gt;             [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;, x.display_name,&lt;br&gt;               dict(selected=output_bool(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt; == employee_id)))&lt;br&gt;              for x in v2org.employees])&lt;br&gt;locations = [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;, x.display_name,&lt;br&gt;              dict(selected=output_bool(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt; == location_id))&lt;br&gt;             for x in v2org.locations]&lt;br&gt;statuses = [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;, x.display_name,&lt;br&gt;             dict(selected=output_bool(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt; == status_id))&lt;br&gt;            for x in model.ShiftStatus.select()]&lt;br&gt;return dict(htmlclass="advancedscheduling", v2org=v2org, name=name,&lt;br&gt;            employees=employees, locations=locations, statuses=statuses)&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Although this particular example has enough regularities to want to extract&lt;br&gt;them into a helper&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;def item_list(items, selected_id):&lt;br&gt;    return [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;, x.display_name,&lt;br&gt;             dict(selected=output_bool(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt; == selected_id))&lt;br&gt;            for x in items]&lt;p&gt;&lt;/p&gt;

&lt;p&gt;employees = [(0, 'None')] + item_list(v2org.employees, employee_id)&lt;br&gt;locations = item_list(v2org.locations, location_id)&lt;br&gt;statuses = item_list(model.ShiftStatus.select(), status_id)&lt;br&gt;return dict(htmlclass="advancedscheduling", v2org=v2org, name=name,&lt;br&gt;            employees=employees, locations=locations, statuses=statuses)&lt;br&gt;&lt;/p&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;HTH,&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Marius Gedminas</dc:creator><pubDate>Tue, 16 Dec 2008 10:34:09 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4429161</link><description>&lt;p&gt;Marius, thanks for the feedback!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Tue, 16 Dec 2008 09:55:15 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4416370</link><description>&lt;p&gt;&lt;/p&gt;&lt;pre&gt; &lt;code&gt;&lt;br&gt;The only real gripe I have with the non-standard bool values. If you must have a bool that can’t be used to do arithmetics create some kind of custom Matt bool:&lt;p&gt;&lt;/p&gt;

&lt;p&gt;class mbool:&lt;br&gt;    def __init__(self,expr):&lt;br&gt;        self.val=bool(expr)&lt;br&gt;    def __nonzero__(self):&lt;br&gt;        return self.val&lt;br&gt;    def __bool__(self):&lt;br&gt;        return self.val&lt;/p&gt;

&lt;p&gt;then you can also extract the repeating part:&lt;/p&gt;

&lt;p&gt;def selected_lst(org_entity, select_id):&lt;br&gt;    return [(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt;, x.display_name, {'selected':mbool(&lt;a href="http://x.id" rel="nofollow"&gt;x.id&lt;/a&gt; == status_id)})&lt;br&gt;            for x in org_entity]&lt;/p&gt;

&lt;p&gt;Which allows you to say:&lt;br&gt;return dict(&lt;br&gt;    htmlclass="advancedscheduling",&lt;br&gt;    v2org=v2org,&lt;br&gt;    name=name,&lt;br&gt;    employees=[(0, 'None')] + selected_lst(v2org.employees,employee_id),&lt;br&gt;    locations=selected_lst(v2org.locations,location_id),&lt;br&gt;    statuses=selected_lst(model_ShiftStatus.select(),status_id),&lt;br&gt;)&lt;/p&gt;

&lt;/code&gt;&lt;p&gt;&lt;code&gt;&lt;/code&gt; &lt;/p&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">nes</dc:creator><pubDate>Mon, 15 Dec 2008 16:55:05 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4387742</link><description>&lt;p&gt;Thanks for the writeup.  I think maybe you can do preformatted text with the code tag, and then use lots of nbsp's to indent.&lt;br&gt;&lt;code&gt;&lt;br&gt;def f(a, b, c):&lt;br&gt;   return a + b + c&lt;br&gt;&lt;/code&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Sat, 13 Dec 2008 12:31:04 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4387722</link><description>&lt;p&gt;I'm not against OOP, I just learned how to program in an environment without any OOP people around me, so I doesn't come naturally.  And I've worked with some bad programmers that were also OOP fans, so I've read a lot of really ugly OOP code.&lt;/p&gt;

&lt;p&gt;For example, I dislike having to hop up four or five or more parent classes to find the code that actually does what I want.  So maybe in reaction to that, I'm too far on the opposite extreme, where I avoid any anything but low-level data types and literals.&lt;/p&gt;

&lt;p&gt;I write these posts in order to get feedback and hopefully improve.  Thanks for your comments.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Sat, 13 Dec 2008 12:29:26 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4385735</link><description>&lt;p&gt;Yuck, mangled my whitespace. I can't find any documentation on how to do preformatted text in Disqus comments, so here's my un-mangled version: &lt;a href="http://simonwillison.net/static/2008/code-formatting-example.txt" rel="nofollow"&gt;http://simonwillison.net/stati...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">simon</dc:creator><pubDate>Sat, 13 Dec 2008 06:21:41 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4385276</link><description>&lt;p&gt; You are not a fan of OOP are you? A functional language like Erlang seems more apropiate for you, Python's parameter passing is not side effect free so your issue #2 doesn't apply much.&lt;/p&gt;

&lt;p&gt; Instead of returning tuples and dictionaries you should be returning class instances where the breaking down into separate lines is more natural. I for one can't understand why  selected and disabled aren't properties or methods of employee.&lt;/p&gt;

&lt;p&gt; This also makes the code more readable for collaborators, experience tells me highly functional programs tend to be terse on documentation. If you are programming in Python is probably because  you appreciate readability, so not only do I expect you to use intermediary variables, I even expect you to give them descriptive names so everybody knows what that value you just calculated is.&lt;/p&gt;

&lt;p&gt; I understand you have your style and respect it and given that most of your code is written to work like this its probably a bad idea to switch now, so I'll simply encourage you to follow the advises given by Gary and David about proper indentation.&lt;/p&gt;

&lt;p&gt; Nice post, cheers.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">rgzdev</dc:creator><pubDate>Sat, 13 Dec 2008 05:03:23 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4374041</link><description>&lt;p&gt;David,&lt;/p&gt;

&lt;p&gt;I flip-flop a lot on using backslashes vs opening a list on one line&lt;br&gt;and then putting the meat of the assignments on the next line.&lt;/p&gt;

&lt;p&gt;Thanks for the tip on conditional expressions.  I'll go back and see&lt;br&gt;if that approach works.  I prefer using None rather than False because&lt;br&gt;I'm never going to misinterpret None as an integer.&lt;/p&gt;

&lt;p&gt;Thanks for the feedback.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Fri, 12 Dec 2008 15:37:22 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4371940</link><description>&lt;p&gt;Breaking assignments over multiple lines: try to avoid backslashes. They're fragile and ugly. One space after the backslash will break it. Instead, I'd do:&lt;/p&gt;

&lt;p&gt;cat1, cat2, cat3, cat4, cat5 = [&lt;br&gt;    self.categories[x] for x in range(1, 6)]&lt;/p&gt;

&lt;p&gt;IOW, use the properties of Python's brackets (and braces and parentheses).&lt;/p&gt;

&lt;p&gt;As for conditional expressions, I hope you realize that these two dictionaries are almost identical:&lt;/p&gt;

&lt;p&gt;        {'selected':1 if self.preferred_send_method == x else None,&lt;br&gt;         'disabled':1 if disabled else None,}&lt;/p&gt;

&lt;p&gt;        {'selected': self.preferred_send_method == x,&lt;br&gt;         'disabled': disabled,}&lt;/p&gt;

&lt;p&gt;Python's True == 1, and False == 0, so just use the condition directly as in the second version above. You used None as a false value; unless there's a good reason not to, you should use False instead.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David Goodger</dc:creator><pubDate>Fri, 12 Dec 2008 15:04:32 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4371885</link><description>&lt;p&gt;That's a really good idea.  Thanks!&lt;/p&gt;

&lt;p&gt;For a while, I was hung up on the notion of putting code into the model that is really a view-y kind of thing.  Somewhere back I wrote a post called MVC blasphemy to that effect, where I added methods to individual model objects so that they would have an HTML-ish form.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Fri, 12 Dec 2008 15:01:41 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4371710</link><description>&lt;p&gt;All of that looks pretty unpythonic to me. Long lines and long statements are not as clear as breaking things into separate lines and using temporary variables. Your style reminds me of Java where, by the time you are halfway through a construct, you forget what the construct was about.&lt;/p&gt;

&lt;p&gt;As far as actual code, in the first case I would just make the whole assignment in single line, since it is less than 80 characters.&lt;/p&gt;

&lt;p&gt;The next two... yeah, I would break things down to something like you showed in . I just find it much easier to read. I don't think it is a problem to use lots of vertical space when you have to use lots of vertical space. Break it into a function if it seems bothersome.&lt;/p&gt;

&lt;p&gt;I also don't think 2. is really true. Think about this trivial example:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;In [1]: a={'a':1, 'b':2}&lt;p&gt;&lt;/p&gt;

&lt;p&gt;In [2]: def foo(x, y): print x, y&lt;br&gt;   ...: &lt;/p&gt;

&lt;p&gt;In [3]: foo(a.pop('a'), a)&lt;br&gt;1 {'b': 2}&lt;br&gt;&lt;/p&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The second parameter is clearly dependent on how you derive the first parameter.&lt;/p&gt;

&lt;p&gt;List comprehensions and generators are great, but you shouldn't overdo them. If the expression becomes long, it is a hint that it is probably too complex to easily read and understand. The obvious exception to this rule would be a case where this piece of code was a performance bottleneck and list comprehension was the fix to make it fast enough.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Heikki Toivonen</dc:creator><pubDate>Fri, 12 Dec 2008 14:52:27 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4371524</link><description>&lt;p&gt;Regarding the "viewer discretion advised" block: I started trying to clean it up, but I don't think I can really make it better. The example is so small that the design of the containing system can't be escaped.  What I would probably do, as a first round of repair, is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;return dict(htmlclass="advancedscheduling",&lt;br&gt;            v2org=v2org,&lt;br&gt;            name=name,&lt;br&gt;            employees=v2org.employees.to_json(),&lt;br&gt;            locations=v2org.locations.to_json(),&lt;br&gt;            statuses=[status.to_json()&lt;br&gt;                      for status in model.ShiftStatus.select()))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'm assuming here that v2org is a model object. I don't know whether the constraints imposed by your ORM would allow you to say "v2org.employees.to_json()", since "employees" might be some kind of result set from the database. But the basic idea I would advocate is that the model objects should know how to express themselves in whatever formats you need, and the controller should just ask them for those formats when it needs them. That way the controller does one job (mediates between user actions and model changes) and the model does one job (exposes the data).&lt;/p&gt;

&lt;p&gt;In general, my opinion is: if your code is complex enough that you need to think hard about formatting it, you definitely are doing too much in the method, and may be doing too much in the class.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">garybernhardt</dc:creator><pubDate>Fri, 12 Dec 2008 14:41:09 -0000</pubDate></item><item><title>Re: code-formatting people: I need your help</title><link>http://blog.tplus1.com/index.php/2008/12/12/code-formatting-people-i-need-your-help/#comment-4371841</link><description>&lt;p&gt;Thanks for the feedback!&lt;/p&gt;

&lt;p&gt;I'm talking about how the key-value pairs in your dictionary a are independent, not parameters going into a function.  If you can figure out how to set one key in a dictionary based on the value of another key in that dictionary, DURING THE CONSTRUCTION, then I would really like to see it, because it would mean I'm completely wrong.&lt;/p&gt;

&lt;p&gt;Matt&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Fri, 12 Dec 2008 13:59:23 -0000</pubDate></item></channel></rss>
