http://qs321.pair.com?node_id=266609

In Python, a programming language that I don't like much, you don't use curly braces or keywords to delimit code blocks. Instead, indenting is required. The block ends when indenting ends.

I hate forced indenting. Even though I always indent, I don't want this to be necessary - I want to code in my own style, and be able to change that style when I feel like doing so. This is the number one reason for me to not learn anything about Python.

But apparently some people like forced indentation and some even use Python because of only that. Well, Perl can do this too. Let's start with an example script, that I will call test.pl:

#!/usr/bin/perl -w use strict; $a = 0; while ($a < 5) { print ++$a, "\n"; for $b (1..10) { print "$a $b\n"; } for $b (11, 12) { print "$a $b\n"; } } print "done\n";
(Yes, this does use package globals, yes, strict is kind of useless because of that. I just typed "use strict;" because it's a habit. A good one, IMO.)

Now let's remove the curlies, since when I'm done, they will be inserted automatically.

#!/usr/bin/perl -w use lib "."; use Test; use strict; $a = 0; while ($a < 5) print ++$a, "\n"; for $b (1..10) print "$a $b\n"; for $b (11, 12) print "$a $b\n"; print "done\n";
As you can see, I added use Test;, because my test module will be named Test.pm. I like keeping things simple and obvious. use lib "."; is there to make sure I'm not using the other module called Test :).

I have no idea how Python's indenting rules are, and don't really want to know either. So I decided to create some rules:

foo bar; baz quux xyzzy blah; moo
equals, more or less:
foo { bar; } baz; { quux { xyzzy { blah; } } } moo

Source filtering is cool. Dangerous and scary, but extremely cool. And with Filter::Simple, it's also very easy.

package Test; use strict; use Filter::Simple; FILTER_ONLY code => sub { no warnings; my @lines = split /\n/, $_; my @indents; for (@lines) { my ($indent) = /^(\s*)/; $indent =~ s/\t/12345678/g; $indent = length $indent; if ($indent > $indents[-1]) { s/^/{/; push @indents, $indent; } elsif ($indent < $indents[-1]) { while ($indent < $indents[-1]) { defined pop @indents or last; s/^/}/; } } } $_ = join "\n", @lines; $_ .= "}" x @indents; }; 1;
As this is a quick hack, I added no warnings; to get rid of warnings caused by the initial emptiness of @indents. I also did not do much testing. To be honest: test.pl was the only test I did. I hate forced indenting, so I'd rather not write more testing code for this :)

Anyway, this works. Maybe Python coders like Perl better now. Perhaps not.

Perl can, without a lot of code, support forced indenting. I wonder if Python people can hack up something to support curly delimited blocks in Python, without mandatory indenting.

Is Python flexible enough? (I'm asking this in the wrong place; I should be trolling a Python forum instead.)

Please do not use this module. Mandatory indenting is evil.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

P.S. / Update: I'm not actually being serious :)

Another update:
10:09 < scrottie> hrm.  should write a source filter that reads white-space structured 
                  perl and adds the { and }'s
10:09 <@Juerd> scrottie: Sorry.
10:09 <@Juerd> scrottie: http://perlmonks.org/index.pl?node_id=266609
10:09 <@Juerd> I win :)
10:09 < scrottie> no kidding!?
10:09 <@Juerd> This was 2 days ago, even :)
10:09 < scrottie> you're a crazy fuck, you know that?

Replies are listed 'Best First'.
Re: Mandatory indenting
by theorbtwo (Prior) on Jun 17, 2003 at 20:16 UTC

    A iuseful use of this might be a use CheckIndent; that will not allow you to miss-indent. (IE each inner block must have at least one more preceading space then it's parent block.)


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Mandatory indenting
by Maclir (Curate) on Jun 17, 2003 at 21:25 UTC
    How does this handle arbitrary line breaks? Say I do something like:
    $foo = "bar"; $return_code = xyzzy($parm_value[$index_counter], "This is the page title", $someothervalue); if ($return_code)
    I don't like forced indentation and layout rules, because they often destroy readabiity.

      How does this handle arbitrary line breaks?

      That fails.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Mandatory indenting
by chunlou (Curate) on Jun 17, 2003 at 21:58 UTC
    One general comment though. The design philosophy of Python language itself is "there must be an obvious to do it," whereas Perl's "there is more than one way to do it."

    Apparently, they have opposite design philosophies. Forced indenting is more consistent with Python's design philosophy than Perl's.

    You can rarely appear right to everyone every time, but at least you can be consistent. I supposed that's what the language designers did.
Re: Mandatory indenting
by Maclir (Curate) on Jun 18, 2003 at 12:35 UTC
    To bring this thread back from a Python vs Perl flame-fest, to the original concept "is mandatory code indentation to show structure a good thing, and how can we do this in perl", here is my view:

    I indent my code to make it easy for me to see the structure and purpose of the code. How the language compiler / interpreter / parser determines where a code block starts and ends is its own personal business, provided it interprets code structure as I intended it to be.

    At the moment, I am coding far more in C and VB.Net than perl. (Yeah, sometimes life sucks.) I probably tend to overuse curly braces, but that way I know there is no confusion - either for the compiler or myself. But I will also break up long lines, particularly for functions calls, and line up parameters.

    My bottom line - we doan need no steekin mandatory indentation. It should go the way of column 72 being the line continuation field, and mandatory line numbering in columns 1 to 5 (with column 6 to indicate a comment).

      the original concept "is mandatory code indentation to show structure a good thing, and how can we do this in perl"

      For the record: I think mandatory indenting is a BAD thing, but I liked playing with a source filter to get it in Perl.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      I don't like the idea of a language enforcing mandatory indenting, but a common indenting style is a good thing for a team project. That way you can diff two versions of a file and see real differences between them without a zillion irrelevant differences because two programmers have different indenting styles. That's why I configured CVS to reject any code that hasn't been perltidied. It's a little obnoxious, but the effort more than pays for itself. (And cool hack, juerd.)

        How does your CVS know the code hasnt been tidied and thus to reject? I personally tend to tidy code before I put it under source control, but have no automatic procedure for doing so. Id like to hear more.


        ---
        demerphq

        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
        ++Zed for setting sensible project and team standards.
Re: Mandatory indenting
by yosefm (Friar) on Jun 18, 2003 at 11:20 UTC
    This could go as a module in the Acme:: namespace...

    Anyway, sometimes forced style can be usefull, especially when you work in a team. I once worked in a place where I reformatted my team-leader's code all the time, to make it possible for the rest of the team to read what she was doing. And it wasn't just indenting - it was also selecting variable names like 'jj' not for loop counters but for serious stuff. People who do that should be hanged :-)

Re: Mandatory indenting
by Anonymous Monk on Jun 18, 2003 at 00:39 UTC

    Damn, I don't even have to say anything, I'll just quote you:

    1. In Python, a programming language that I don't like much
    2. I have no idea how Python's indenting rules are, and don't really want to know either.

    So you don't even know the most basic points about something, but you dislike it nonetheless? Sorry kid, this doesn't cut it.

    Is Python flexible enough?

    I have yet to see a situation where required, consistant block indenting causes anything more than the most trivial problems. I've been using it (and about 15 other languages) for years.

    I wonder if Python people can hack up something to support curly delimited blocks in Python, without mandatory indenting.

    Yes, we could. Very easily in fact, but we don't. Do you know why we don't bother? Consistancy. It's what Python has that Perl does not. It's what makes Python easier to learn than Perl. It's what makes Python easier to parse than Perl. It's what makes Python easier to test than Perl. It's what makes Python easier to maintain than Perl. It's what keeps me from having to worry about hiring programmers that write camel code for actual projects.

    Perl has a lot of great features, but it takes providing multiple options too far. A language with less flexibility can be a very good thing.

      Yes, we could. Very easily in fact, but we don't. Do you know why we don't bother? Consistancy. It's what Python has that Perl does not. It's what makes Python easier to learn than Perl. It's what makes Python easier to parse than Perl. It's what makes Python easier to test than Perl. It's what makes Python easier to maintain than Perl. It's what keeps me from having to worry about hiring programmers that write camel code for actual projects.

      Perl has a lot of great features, but it takes providing multiple options too far. A language with less flexibility can be a very good thing.

      So what you are saying is: if we dumb down the language, we can hire people that are not experts while still getting good quality clean code? I don't buy that.I have seen this several times; where a company's code in language X becomes unmaintainable because the code was written by someone who did not know how to program, and then the company hears the sirens' song of being able to get good quality code of non/Jr. programmers because language Y will not let them write bad code, only to find out that it is the programmer and not the language that dictates code quality. So all you end up with when you dumb down a language is a language that cripples experienced programmers while doing nothing to keep bad programmers from writing bad code.

      And when you say keeps you from having to worry about programmers from writing Camel code, I take it that you mean that it keeps experienced programmers from writing code that a novice would not be able to grok, a @bar = map {} sort {} map{} @foo for example? But how would python make, let us say, the VJ header compression code easier to read? In perl, the syntax just makes some things easier to read for an expert while being harder for a novice to read.

      -- It is the programmer, not the language.

      Disclaimer:

      I am in no way saying that python is a dumbed down language -- I have not used it before

        Every language I'm aware of allows you to write bad code. The differences between many of them simply boil down to how easy it is to screw up. Consistant languages with predictable behaviour will reduce development time, maintenance time, and the number of mistakes made. Obviously if you have expert Perl programmers writing the same code as novice Python programmers the Perl programmers will do a better job.

        The real question is what would the results be having a wide range of programmers of all experience levels of each language going head-to-head in a variety of real-world programming tasks. Perhaps it would be an interesting competition?

      Howdy!

      -- for lame anonymous reply.

      I have yet to see a situation where required, consistant block indenting causes anything more than the most trivial problems. I've been using it (and about 15 other languages) for years.

      ...long lines that you want to split and indent further? I can see that causing issues that I would not characterize as "trivial".

      Oh, consistancy is not all its cracked up to be. It can be a straightjacket. It may make learning easier in the early stages, but can impede advanced learning. Too much consistency can leave the language without enough conceptual texture. If everything works the same, how can you tell the difference. I don't recall where I saw the mention, but it was noted that natural languages have the most inconsistency in some of the most heavily used portions. Consider irregular verbs. Which ones are most commonly irregular? "to be" is frequently wildly irregular and is probably the most heavily used verb.

      Perl may be excessively flexible, but you are not required to use all the flexibility. As chromatic noted, discipline is key to the appropriate use of flexibility.

      yours,
      Michael

      So you don't even know the most basic points about something, but you dislike it nonetheless?

      I know one basic point, namely mandatory indenting, and that single basic point is enough for me to dislike it. I'm not saying that Python is a bad language, because I simply don't know if it's any good. Python may be great, but its mandatory indenting keeps me from trying.

      Yes, we could. Very easily in fact, but we don't.

      Does Python have source filtering capabilities, then? Or would this require a hack in the core?

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        Python may be great, but its mandatory indenting keeps me from trying

        You definitly should try it, it is fun to code in python (though I didn't in the last nearly 2years, alas). The only thing I don't like is the inevitable OO-approach to regexps, but they have a real cute extension to perls: named captures (you can give names to the capturing parenthesis).

        While an anonymous sub is cool, a lambda as the function-arg to reduce did always put a smile on my face ;)

        I wouldn't do most of my admin-stuff and a lot of other work-stuff in perl if I followed this road: I hate(d) the sigils, and I was absolutly sure never to use perl.
        Well, I had to code a german public-health forum (external link) using slashcode. And now I'm in love with perl.

        Don't let such a thing fool you, in my experience you miss more in not using languages for such reasons, than the language looses in you not using it :-)
        always play a wee bit with it, beeing even if you're doing it for nothing else than to despise it for well-founded reasons.

        kind regards,
        tomte


        Edit:s/linke/like/

        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.

        I know one basic point, namely mandatory indenting, and that single basic point is enough for me to dislike it.

        It seems to me like you're taking a idea from another paradigm and using it in your paradigm. See how the shoe fits its intended foot, not some other foot.

        I've had similiar discussions with a C programming friend of mine. He so dislikes Perl because you have to type ord to get the ASCII value of a char. He finds it completely silly. The fact that you rarely want to use ord in the typical Perl task doesn't occure to him.

        I think he does wrong in judging a feature of Perl like that. I think he should first try to grasp the whole concept of Perl--what it's for, why people like it, and how the pieces fit the Perl puzzle. If he then choose to dislike it I can respect his opinion.

        Take any piece of art (not fractals). Zoom in a lot. Can you appreciate its beauty now? Probably not.

        I can keep writing analogies all day, but I'll stop now.

        Just my thought,
        ihb

        PS. I realize you don't say that Python is a bad language, but I'm saying that you should evaluate mandatory indenting in a Python mind-set, not as a standalone phenomena.

      I find myself wondering, if Python is wonderful and Perl came out of a camel's behind, why are you reading, let alone posting on, this site?

      Personally, while I always indent my code to match its meaning ( thank you, emacs ), I prefer to have some punctuation to enforce my intent. And for those occassions when I cut and paste some text---no, no, I never do that, I re-factor, it's good to have braces delineating sub-components until I get them re-aligned

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

      I was going to write a retort here, but I won't bother since Larry has already done a much better job. The relevant section is at the bottom of the mail where he quotes Umberto Eco: "similar things should look different, because [otherwise], you end up with too little redundancy for effective communication." Consistency is not nearly what it's made out to be - which is why natural languages tend to be all but consistent. But then, you probably expected replies like this one on a site devoted to Perl - which raises the question of why you'd bother to post a node like yours on a site devoted to Perl.

      And then even if I a feel a desire for consistency at some point, I'll pick Ruby anyway.

      Makeshifts last the longest.