3 ms·
Sibling comments have pointed out that you just don't need to line things up that way. Here is how your code looks without the alignment: def mouseMoved(se
by Stratoscope 5y ago
Sibling comments have pointed out that you just don't need to line things up that way. Here is how your code looks without the alignment:
def mouseMoved(self, info):
point = info["point"]
text = u"%.0f %.0f" % (point.x, point.y)
self.set(text)
Is that any less readable?
Urgent request! We need two new variables for some debugging: distance and relative_velocity. Going back to the vertically aligned format, we will add them like this:
def mouseMoved(self, info):
point = info["point"]
text = u"%.0f %.0f" % (point.x, point.y)
distance = self.calculate_distance(info)
relative_velocity = self.calculate_velocity(distance, info)
log_move(point, text, distance, relative_velocity)
self.set(text)
We had to add whitespace to the previous assignments to match the new ones. Now, with all that horizontal whitespace, my eyes have trouble tracking what is assigned to what. And the version control history will have unnecessary formatting diffs.
If you forgo the column alignment, those problems go away:
def mouseMoved(self, info):
point = info["point"]
text = u"%.0f %.0f" % (point.x, point.y)
distance = self.calculate_distance(info)
relative_velocity = self.calculate_velocity(distance, info)
log_move(point, text, distance, relative_velocity)
self.set(text)
- alpaca128 5y ago> Now, with all that horizontal whitespace, my eyes have trouble tracking what is assigned to what If it's not aligned I have trouble identifying exactly what I need because it's so chaotic. When everything's aligned it's a bit like a table and my brain immediately knows where the right-hand side is no matter which line I want to read. Now I don't do that everywhere, often it just doesn't bring any benefit. But generally I often find it helpful and it's one of the quickest ways I can make some code blocks more readable, as it takes just a few keypresses.
- josephg 5y agoIt looks chaotic because everything is packed together. If you let that code breathe a bit it looks like this: def mouseMoved(self, info): point = info["point"] text = u"%.0f %.0f" % (point.x, point.y) distance = self.calculate_distance(info) relative_velocity = self.calculate_velocity(distance, info) log_move(point, text, distance, relative_velocity) # Update overlay label self.set(text) I find that a much more readable style. Instead of stuff on the left - assigned to stuff on the right, its now a story made up of steps: First we're figuring getting some variables for text labels, and distances. Then logging that out. Then after that we're updating the label on the overlay. Each step in the story has a little breath (the newline). And if needed, a comment. Here's a random work-in-progress example from some real code I'm working on this week. Its not beautiful or overly fancy. Just steady, confident and readable (assuming you know the context): https://github.com/josephg/diamond-types/blob/2e807298a70382eb0c7d1216343577a595954f97/src/list/doc.rs#L411-L437 https://github.com/josephg/diamond-types/blob/2e807298a70382...
- alpaca128 5y agoWhen the equal signs aren't aligned I first have to search where the symbol is on a given line to identify the left and right side. When the assignments are aligned it's obvious at a glance. I guess it's different between people, I personally find it noticeably slower and more difficult to read like that. Blank lines inbetween don't help because it's about horizontal alignment.
- josephg 5y agoI hear what you’re saying, but the horizontal alignment implies each line is doing a similar thing. That’s not the case here. Eg, the first line creates the ‘info’ variable and later lines use that to calculate derived variables. So order matters. Both approaches emphasise different narratives about the code. The aligned block style emphasises the similarity between the assignments. My space-between-lines style emphasises the difference between each block of code, as if each block is a paragraph it’s own separate intent. If the assignments were actually similar, I would find the other approach more compelling. Some sibling comments have given better examples where this makes sense. But for this code in particular, the assignments are more different than similar (in both goal and subject). I’d use spacing to make that difference obvious.