views:

335

answers:

5

In my experience code clarity and readability are the primary concerns in traditional coding standards. PEP-8 would have been rejected by some of my previous customers.

The idiomatic concepts such as, using dictionaries and lambda functions to emulate the behavior of a switch statement looks like something that would have clearly been forbidden by "Don't use tricky code" mandate in prior coding standards.

Typical coding standards have "whenever it improves the readability of the code break up statements into multiple lines." Again lambda functions are right out.

Beyond that, code reviews are assumed and I've had customers that not only reviewed the code, but REQUIRED software people to be part of the review. This was done to insure the code was readable without requiring an uber-geek specializing in a particular language to look at the code.

How are these types of concerns addressed with Python? End of original question.

Edit: I reread PEP-8. I must retract the "PEP-8 would be rejected" statement. (This may just save my life from the Python lynch mob gathering out side.) The "This is not complete" and "Break the rules when it improves clarity" statements in that PEP do not require such harshness. Lamda statements are not addressed in that PEP. (That falls into the not complete category)

@Javier: Do you have some recomended reading about the advantages of lamda?

@RichieHindle: You may and it's a valueable suggestion, thank you.

+11  A: 

You should write code that's idiomatic in the language you're using, and anyone reviewing the code should be familiar enough with the language to recognise and understand idiomatic code.

Using a dictionary for dispatching is idiomatic Python IMHO. Go with what's Pythonic rather than trying to fit coding standards that were designed for other languages onto Python.

(Of course all this applies to any language.)

RichieHindle
Saying something is idomatic but terse and difficult read and explain is not something my customers are willing believe. The entire point of a high level language is to provide for human comprehensible code. More compact terse code means, it's harder to identify errors and typos, and when they do occur they have a larger impact.
NoMoreZealots
@Pete: you need to provide example of hard-to-read idiomatic code.
SilentGhost
Readability and understandability are not the same thing. Your verbose code may be easier to read by virtue of being more spread out, but it's probably harder to understand, for the same reason, and also for not looking like Python code. Maintainability means that Joe Python programmer off the street can understand and modify your code. If your Python code doesn't do things the Python way (this is especially prevalent in Python in particular, which is VERY focused on doing things "the Python way"), Joe Python programmer will have trouble understanding it, no matter how verbose it is.
Nick Lewis
+3  A: 

Software is hard. No-ubergeek rule is good but it doesn't mean that any random person with a title programmer could review or should be able to understand any code he's looking at with ease.

Lambdas that span multiple lines are shouldn't exist in your code regardless who you're writing for. They're no different from proper functions in this sense, so why would you need them? There is nothing tricky about language idioms and they tend to be quite clear as well.

SilentGhost
+1  A: 

If you don't like using a dict of functions to implement switch type functionality, then just don't use it and use an if-else structure instead.

if var == "a":
  pass
elif var == "b":
  pass
elif var == "c":
  pass
else:
  #Default case
  pass

There. That was easy, wasn't it.

David Locke
If if-elif-else is inelegant but a dictionary is "tricky", what do you recommend using in cases where you need `switch`-like functionality?
mipadi
Use elif, please!
nikow
@mipadi, I couldn't have said it better myself.
David Locke
+3  A: 

But it isn't a good replacement for a light weight switch. In some cases it's a hammer instead of a screwdriver.

Python cannot make jump table optimizations mostly due to its nature as a dynamic language. Therefore if Python had a switch statement, it would be no different from a hash table.

You complained about the verboseness in your example and here I present to you a cleaner way.

fruits = dict(
    red="apple",
    blue="blueberry",
    pink="watermelon")

name = fruits[color]
Unknown
+1  A: 

In answer to your question:

how do you handle the "default" condition in Python?

You do this:

fruits = {
    "red": "apple",
    "blue": "blueberry",
    "pink": "watermelon",
}

print fruits.get(colour, "unknown")

Or you use the defaultdict class, which behaves like a normal dictionary but gives a default value when a key isn't found.

RichieHindle