quixadhal
Wizard


Group: Members
Posts: 1,256
Joined: Oct 17, 2007
|
#16 Posted Jul 16, 2009, 4:29 am
|
David Haley said:
Code (text): 1
2
3
4
5
6
7
8
9 |
def f(x, y):
if x > 2:
x = x + 1
x = x * y
return x
|
The example is contrived, but the point is made perfectly. Where do you put the line x = x * y? Do you put it inside the if block or not? In other words, do you assign x*y to x only when x > 2, or always?
I don't consider that a downside. Why do we bother to indent anyways? Because it makes the code clear and easy for others to follow. Except nobody seems to be able to agree on the stupidly nitpicking details of it.
Code (text): 1
2
3 | if(x){foo();}else{bar();} |
Code (text): 1
2
3
4
5
6
7 | if(x) {
foo();
} else {
bar();
} |
Code (text): 1
2
3
4
5
6
7
8
9
10 | if(x)
{
foo();
}
else
{
bar();
} |
All perfectly valid C code, but totally different in appearance. In such tiny examples, it doesn't matter, but go look at a decent sized codebase where you have a dozen levels of indentation, half of which use one style, half of which use another, and tell me again, how having a language-enforced level of consistency is such a horrible idea.
In the contrived example David presented, we can't know which parts he meant to include in the if statement, but the author presumably knew. The same can be said for this C code....
Code (text): 1
2
3
4
5
6
7 | if(x)
foo();
bar();
if(y)
foobar(); |
Sure, it SAYS to always call bar(), but is that what the author meant to do, or did he just forget to put braces around the foo();bar(); calls? Does having the begin/end symbols help you there? In they python example, the compiler will probably fail to compile that, but if you indented everything below the def line one level, it would be valid... and just as confusing as the C example here.
Scanning a chunk of code that's hundreds or thousands of lines long, it's much easier to spot a change in the indentation level than it is a missing brace or END statement. If the language requires you to indent, you'll very quickly learn to indent. If not... well, I've dealt with a lot of really ugly (but functional) perl code that makes me wish it had been python.
Of course, this isn't an argument that can be won... but I feel compelled to point out that lack of enforced indentation can lead to exactly the same problems that forced indentation can. You can mitigate this somewhat if your language always requires begin/end symbols, but then people would complain about how cumbersome it is to write quick one-liners.
|
......................... 
|
|
Scandum
Wizard


Group: Members
Posts: 1,105
Joined: Aug 8, 2006
|
#17 Posted Jul 16, 2009, 8:52 am
|
quixadhal said:Of course, this isn't an argument that can be won... but I feel compelled to point out that lack of enforced indentation can lead to exactly the same problems that forced indentation can. You can mitigate this somewhat if your language always requires begin/end symbols, but then people would complain about how cumbersome it is to write quick one-liners. 
I've picked up the habit to always use braces which probably saved me a lot of trouble over the years.
Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.
|
|
|
David Haley
Wizard


Group: Members
Posts: 5,727
Joined: Jun 30, 2007
|
#18 Posted Jul 16, 2009, 9:34 am
|
Yes -- you can solve this problem in C/C++/Java etc. by simply always using braces. It's a good habit to pick up.
Lua doesn't give you a choice, you must use explicit block terminators, e.g.:
Code (text): 1
2
3
4
5
6
7 |
if x > y then
return 2
end
|
Quote:Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.
No, as I said, it is impossible in general to automatically indent Python code that isn't already indented. The example I gave is enough to show that. However it is true that the editors can make pretty good guesses most of the time, again as I said. But this is quite different from automatically indenting Python code in general.
quixadhal said:In they python example, the compiler will probably fail to compile that, but if you indented everything below the def line one level, it would be valid...
I don't think it would be, as you must start a new indentation level after the if statement, or put the if-block on the same line.
quixadhal said:In such tiny examples, it doesn't matter, but go look at a decent sized codebase where you have a dozen levels of indentation, half of which use one style, half of which use another, and tell me again, how having a language-enforced level of consistency is such a horrible idea.
Python doesn't enforce the style of indentation, just that indentation separates blocks. Nothing prevents one developer from using four spaces, another from using 3 spaces, another from using tabs, another from putting things on the same line as the if statement, and so on and so forth. The rule is that one block must have the same indentation through the block, but different blocks in the same function can mix indentation levels...
|
|
|
Scandum
Wizard


Group: Members
Posts: 1,105
Joined: Aug 8, 2006
|
#19 Posted Jul 16, 2009, 11:31 am
|
David Haley said:Quote:Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.
No, as I said, it is impossible in general to automatically indent Python code that isn't already indented. The example I gave is enough to show that. However it is true that the editors can make pretty good guesses most of the time, again as I said. But this is quite different from automatically indenting Python code in general.
You're mistaken, for example:
Code (text): 1
2
3
4
5 |
if 1 + 1 == 2: print "foo"; print "bar"; x = 42
|
Could be auto indented as:
Code (text): 1
2
3
4
5
6
7
8 |
if 1 + 1 == 2:
print "foo"
print "bar"
x = 42
|
There's really no 'auto indentation' in the way you speak off (except for editors), because code is always indented by the parser according to the rules of the language. I understand your confusion, perhaps try to think of it as 'standardized indentation', though it doesn't matter a whole lot how you think of it. This because the concept is implicit when tokenized data is written back to file as untokenized data.
|
Last edited Jul 16, 2009, 11:32 am by Scandum
|
|
|
|
Kelvin
Magician


Group: Members
Posts: 61
Joined: May 18, 2006
|
#21 Posted Jul 16, 2009, 11:37 am
|
I saw this out on the intarweb at one point and thought it was amusing (and true):
Quote:
Useless programmers complain about indentation in Python. Those same programmers complain about semicolons in C.
I will say that the Forestry department here teaches Python to Forestry students in the GIS field. Mind you, these students have never wrote a line of code beforehand, and end up doing some pretty neat stuff. If some of the people in our Forestry department are learning and putting Python to great use, I'm positive that your semi-intelligent (at least) builders can figure it out :)
It's all a matter of preference. I really like the enforced indentation, but it's a matter of preference and no one philosophy will ever be "right". Let's not get into a holy war here.
|
......................... Evennia: Python + Twisted + Django + MUD = Win
Embrace the snake
|
|
Scandum
Wizard


Group: Members
Posts: 1,105
Joined: Aug 8, 2006
|
#22 Posted Jul 16, 2009, 11:39 am
|
David Haley said:By the way, the parser is most certainly not the thing that is indenting the code. The parser is the thing using indentation to understand where blocks start and stop.
The sub discussion is about a parser reading a script from file.. read closely.. and then the parser writing the script back to file.. read carefully.. most scripting languages do not provide this functionality.. thank you for your time.
|
Last edited Jul 16, 2009, 11:40 am by Scandum
|
|
|
|
Kelvin
Magician


Group: Members
Posts: 61
Joined: May 18, 2006
|
#24 Posted Jul 16, 2009, 2:52 pm
|
Of course if you want to use braces in Python, just do this:
Code (text): 1
2
3
4
5 |
from __future__ import braces
|
|
......................... Evennia: Python + Twisted + Django + MUD = Win
Embrace the snake
|
|
|
|