Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Excellent talk. Would recommend to developers outside the python community as well.

Some takeaways; 1. Adhere to a great style guide 2. Can you predict what the code does? Can you trace a path through the function calls, if statements and for loops? 3. Can you explain to someone what the code -means-? Why is it the way it is?

    p = (170, .1, .6);
    if p[1] > .5:
      print("bright!")
    elif p[2] > .5:
      print("light")
That's ok, right? Pretty nice. I can see that it'll print "light".

Good enough? No, not really. How are "bright" and "light" related to (170, .1, .6) and .5?

How about this?

    color = Color(hue=170, saturation=.1, luminosity=.6)
    if color.saturation > .5:
      print("bright!")
    elif color.luminosity > .5:
      print("light!")
Far better! I know now that saturation is related to brightness, and that luminosity is related to lightness. It takes about the same time to write, yet is far more legible.


The obvious question a newcomer would have to that particular code is "Can't a color be bright and light at the same time? Why?".

Those things are better explained in a comment, I'd think.

Comments are needed, but should not explain what the code does (as in "add 1 to n") and instead exaplin why it does it (as in "fn expects indices to start from 1").


>The obvious question a newcomer would have to that particular code is "Can't a color be bright and light at the same time? Why?".

It can and the code in question will print both "bright" and "light" if passed a color that matches that.

Where did you see that it's an either-or affair in the example?

Besides, the key point, is that the code talks about luminocity and brightness levels, and the second example makes that explicit. No comments needed if you know what HSV is. And if you don't, comments are usually not the place to learn about it.


    if color.saturation > .5:
      print("bright!")
    elif color.luminosity > .5:
      print("light!")
it's an `elif`... it will never print both "bright" and "light"

This kind of mistake really detracts from the point that was argued here about code being "intelligible"


In the presentation it was two if's, not an if and an elif. Someone up-thread misrepresented it.

I didn't read it carefully here, as I had already seen the presentation and it wasn't until your disagreement I realized the code was different.


Yup... my comment about "detracting from the point" was about such an important semantic change to slip through unnoticed. (not about the points delivered in the talk itself or anything)

There's nothing wrong with if/elses, but I strongly prefer pattern matching, where available. And even more, I think it's important to keep computation and presentation separate... that is, to not entagle IO with the rest of the code.

(Unfortunately Haskell is the only language I know of that gives you a tool to tackle that)

Obviously this was just a small example, but I've seen unintended IO byte back the developer in real world (Python) code.

And another small comment: since I haven't bothered to comment on Hettinger's talk before now.

I liked it (but played it back at 1.5x)... but since it's not first talk by Raymond Hettinger I saw, I knew that his delivery is good. Some of the stuff (should?) be obvious to every (Python) developer (but repetita juvant). I didn't like the jab at Java for the mistake of iterating over indexes: there's no excuse to writing that code not even there (look at the Iterator and Iterable interfaces)


That's not his code.


In Python, "elif" means "else if". At most one of "bright!" or "light!" will print.

  $ python
  >>> if True:
  ...   print "First"
  ... elif True:
  ...   print "Second"
  ... 
  First
  >>>


I know what elif means in Python (been doing it since 1998 -- remember Zope?).

That's not the code in his example. Here's that code:

https://youtu.be/wf-BqAjZb8M?t=40m41s


Apologies - I think we've been talking at cross purposes. I was referring to the code up-thread in https://news.ycombinator.com/item?id=10025412 , which (unlike the presentation) uses "elif".


>Apologies - I think we've been talking at cross purposes.

Yeah, didn't pay close attention to it, thought it was the same as the one I saw in the presentation.


Yeah sorry, that was my mistake in copying from the video. It was supposed to be an if statement. I'll leave it now because of the discussion on the issue


I guess Color is a namedtuple. I'm often tempted to [ab]use binding.

    def qualify(color):
        hue, saturation, luminosity = color
        if hue ... or saturation:
            ...
        ...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: