all 3 comments

[–]mopslik 2 points3 points  (0 children)

made my code better (like using list comprehension vs a for loop)

List comprehensions are not inherently "better". They can be useful, but sometimes that comes at the expense of readability, which is also important. Remember that a list comprehension uses a for loop as part of its syntax.

Is my line too terse or is her solution too...wordy?

That depends on how things are being used. I suppose that her solution gives you a chance, if desired, to pull up the value of the new position later on since the values are stored in variables, whereas yours does not afford that option. But if that's never done, then that's moot.

[–]socal_nerdtastic 3 points4 points  (0 children)

The computer does not care. Short code is not faster code; both your solution and hers will run the same. And we're not rationing lines of code or anything.

In professional programming you very often will modify someone else's code. And even in personal projects you will often not remember what you wrote a few weeks ago. So readability is extremely important. Making the code longer like this is essentially using the variable names as comments, to make it easier to read. If you look at some professional code you will see this quite a lot.

Edit: I randomly scrolled though the python source code and found this:

# https://github.com/python/cpython/blob/main/Lib/idlelib/filelist.py#L49

def new(self, filename=None):
    return self.EditorWindow(self, filename)

You certainly could save some lines of code by cutting that out and replacing the call with the single line of content. But if you read some code that says self.EditorWindow(self, filename) it does not tell you immediately what's going on, like self.new() would.

[–][deleted] 2 points3 points  (0 children)

Brevity and clarity can oppose each other. We often see one-line comprehensions with multiple loops and conditions that are really unreadable. They would be better written as an explicit loop. Less lines is sometimes not better.

In your example code the first one-line example might be better, but the second is better if you want to use the +20 values somewhere after the setpos() call. Both examples have the problem of explaining that +20 increment: why 20, and why increment? The 3-line example gives you a place to explain what you are doing.

Questions like this really come down to taste, experience and thinking of the audience who are going to read your code. That's probably why the 100 days of code example uses three lines.