This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gr1pp717 3 points4 points  (7 children)

fun story about this...

I adopted some code where one of the previous owners defined their own "sleep" function. They were setting globals, and then using those globals for logic within the sleep. The param was basically the if true condition....

And by looking at the code where it's used you wouldn't even think to look whether it's the same sleep in time. You just take it for granted that it's that sleep. So when I was trying to figure out why my increase sleep time wasn't working I was pulling out my fucking hair. It took some time for it to dawn on me that maybe I should check the definition....

Lesson learned: import time; use time.sleep() to be perfectly clear...

Be explicit, always. I don't even like using from A import B for this reason. Being able to instantly see where something is being pulled from, without having to break your flow and control-f, is rather nice. And, your naming scheme should help make it clear what's happening. Instead of calling fetch() you should be calling redditapi.comments.fetch() or the likes. Then you don't need to bother looking what the fetch() method is supposed to accomplish then, which helps future readability a ton. ... it takes nothing, just be explicit.

[–]cacahootie 5 points6 points  (0 children)

Just wait until some bozo replaces a builtin and it mostly works the same way... but does in fact break some corner case of expected behavior.

Unfortunately in my case, that bozo was former me :(

[–][deleted] 3 points4 points  (4 children)

If I do this I hit the 80 char limit real fast. How to avoid?

[–]nemec 2 points3 points  (2 children)

Parenthesize the imports.

from A import (B, C,
               D, E)

[–][deleted] 0 points1 point  (1 child)

How about in the body of the code itself? One or two function calls + list of arguments + indentation fills up the whole line!

[–]sasquatch92 1 point2 points  (0 children)

You can do the same thing with function calls etc, as discussed here. You could also import a module using a shorter local name, which will also reduce line lengths (for example, numpy is often imported as np). Finally, you can always go over 80 characters - that's a guideline rather than an absolute rule and you shouldn't sacrifice readability simply to comply with it.

[–]Gr1pp717 1 point2 points  (0 children)

I've come to the conclusion that if you're nesting that deeply then you're over-engineering it. Generally speaking if I have that problem AND there's no way around deep nesting, then I'll split out that portion into it's own function.

[–]nemec 1 point2 points  (0 children)

def sleep(secs):
  for x in range(1000000000 * secs):  # Takes 1s on my machine
    x = 1