Post #231,651
10/31/05 9:30:44 AM
|
Context
It's another of Peter's Terrible Perl Programs That Inexplicably Works!
[link|http://guildenstern.dyndns.org/tmp/autocomm.pl.html|http://guildenstern..../autocomm.pl.html]
Look in the "update_rcc_commissioned_flags" function.
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,660
10/31/05 10:18:59 AM
|
Re: Context
Normally, I would just say "No." as I hate reading my own Perl, let alone someone else's. But had I looked at it (*shudder*) I might have commented that my $output_dir_exists = opendir( OUTPUT, "autocomm_out" );\nmkdir "autocomm_out" unless $output_dir_exists; might work just as well like: mkdir "autocomm_out" unless -d "autocomm_out";
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,662
10/31/05 10:26:40 AM
|
Moochos grassy arse.
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,668
10/31/05 11:30:32 AM
|
And I would point out that...
you really want to check whether mkdir succeeds. Perhaps you don't have permissions to the directory it is in? Perhaps there is a file there of that name?
This is somewhat mitigated by the fact that he shortly afterwards tries to create a file in that directory and checks $! there. So he will notice the error. But good habits are good habits.
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,711
10/31/05 2:21:34 PM
|
How to do that...
mkdir($foo) or die "Unable to make directory!" unless -d $foo; ? Does that need parentheses anywhere?
Note that I was just demonstrating using -d, and not actually putting any thought into the answer whatsoever. ;-)
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,719
10/31/05 3:07:35 PM
|
That works but...
There is a reason why Perl does not let you nest if's directly. It is confusing. Perl allows enough cryptic things, but even Larry drew the line at that one. Create a block for that. They're cheap. \nunless (-d $foo) {\n mkdir($foo) or die "Cannot mkdir '$foo': $!";\n}\n Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,735
10/31/05 4:03:49 PM
|
Ah, good. Thanks.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,674
10/31/05 11:50:58 AM
|
Minor points
For reasons that Code Complete describes, an 8 space indent interferes with comprehension. Reduce that down to 2-4. Perl Best Practices quotes research indicating that 4 is best. I haven't reviewed that research, so I don't know how much to believe it. Personally I use 2, but that is habit. I use a different brace style, but that is habit. I know of no research indicating that one style actually is better than another. Generally I prefer sending debugging messages to STDERR. So you might rewrite your debug routine like this: \nsub debug {\n my $msg = shift;\n $debug or print STDERR "DEBUG: $msg\\n";\n}\n Scott already addressed how to optionally create a directory in Perl. You've got a big-ass RE which is repeated twice. I'd stick that into a variable using the qr// operator. Advantages include being sure that fixes to one copy make it to the other, and being able to name the operation. Like this: \nmy $match_something = qr/(nasty)pattern(here)*/;\n# time passes\nif ($a_string =~ $match_something) {\n ...\n}\n You might also want to use the /x flag on the RE to allow you to break it up and comment it. An organizational note. I'm a fan of organizing commandline programs like this: \n#! /usr/bin/perl -w\nuse strict;\n# program here\n\nexit();\n\n# functions here\n because that puts the part I'm most likely to look at up front. I'm also a fan of using a module like Getopt::Long to handle option processing so that things like debug mode can be easily turned into command-line switches rather than having to be edited in the source. If you're going to have anyone else see and use this program, I'm also a fan of putting pod documentation in the program, and using something like Pod::Usage to print usage messages if someone doesn't use it correctly. For more detailed suggestions, including suggestions of more sophisticated command-line processing and documentation modules, I highly recommend at least reading Perl Best Practices. I don't agree with all of its suggestions, but they all provide at least food for thought. Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,699
10/31/05 1:02:42 PM
|
Re: Minor points
The regexes actually different if you look really closely.
Debug to STDERR: good point, will do.
Getopt::Long: I hear you. Will investigate.
Brace Style: I whacked "Format Source" in Eclipse and then Exported it as HTML. I prefer 4 spaces, too. I'll have to twiddle Eclipse.
For a program I wrote this morning, whilst getting interrupted, i'm actually quite pleased with it.
Mad props to Kiki, though; without it, regexes of that scale are just nightmarish to compose and test.
Thanks for the comments; I'm always keen to hear from Sensei Ben.
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,712
10/31/05 2:25:54 PM
|
Slightly OT: on brace style:
I use: if (foo)\n{\n ...\n} mainly because I have permanent double vision. The extra white space helps. Unfortunately it always seems to drive other people to distraction. :-P
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,714
10/31/05 2:28:40 PM
|
That's what I've been doing, but I'm a noob.
It appeals to my sense of symmetry more than having the opening and closing braces in different columns.
FWIW.
Cheers, Scott.
|
Post #231,721
10/31/05 3:10:18 PM
|
No way, this is the shizzit
I MUCH MUCH prefer this style. It makes everything very clear.
--\n-------------------------------------------------------------------\n* Jack Troughton jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca] [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
|
Post #231,727
10/31/05 3:29:44 PM
|
Yes, that is the correct form
for the reasons you cite - I have wandering eye muscle issues that produces double vision when tired and if I don't align braces like that I can't generally follow the code.
So I will reformat other people's code when reading it for comprehension, review, critique, etc. I also always required students to format that way and did serious point dingage when they use other styles (hey, I'm teacher, that makes me the client and thus the king).
"Whenever you find you are on the side of the majority, it is time to pause and reflect" --Mark Twain
"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." --Albert Einstein
"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses." --George W. Bush
|
Post #231,729
10/31/05 3:37:28 PM
|
I've heard arguments for and against that
But I've never seen or heard of research showing anything other than "people do better with the brace style they're used to using". I hear your point about whitespace. But there are other ways to guarantee whitespace, and I've known other people with bad vision who prefer other brace styles.
If you have some research to share, please do.
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,730
10/31/05 3:40:07 PM
|
The arguments are simple
Vertical alignment people are correct, dissenters are wrong.
......connection closed.
"Whenever you find you are on the side of the majority, it is time to pause and reflect" --Mark Twain
"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." --Albert Einstein
"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses." --George W. Bush
|
Post #231,731
10/31/05 3:52:54 PM
|
Ah, an open mind.
Always good to see when a request for "research" is made.
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #231,732
10/31/05 3:56:51 PM
|
Yeah, but he's right.
\nthis { <-- brace gets lost in the noise:\n sucks;\n}\n\nthis\n{\n doesn't;\n}\n
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,738
10/31/05 4:07:50 PM
|
Yes, we know you're strongly imprinted
That isn't an actual argument, nor is it research (which is what I'm really interested in seeing).
Regards, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,748
10/31/05 5:05:16 PM
10/31/05 5:08:27 PM
|
I'm interested in research too
so I can stop arguing this with the "wrong" people.
Sadly, I've never found any. Which seems odd as it might be fertile ground for a doctorate.
I have lots of anecdotal experience that suggests that the right style (BSD) is best though. Some of this is from teaching programming and helping students understand their programs. Often, simply fixing brace placement/reindenting gives them the insight they need to fix their programs.
Some of it is from code statement nesting errors I've either found and fixed or helped find with someone (pair debugging) who uses the wrong style (to pick a name to differentiate it from the "right" style).
I think a worthwhile experiment would be to take a bunch of source code and either double or remove braces arbitrarily, present them to developers and time how long it takes them to spot the error.
"Whenever you find you are on the side of the majority, it is time to pause and reflect" --Mark Twain
"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." --Albert Einstein
"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses." --George W. Bush
|
Post #231,752
10/31/05 5:22:53 PM
|
so K&R is wrong? Heretic BURN HIM!
"the reason people don't buy conspiracy theories is that they think conspiracy means everyone is on the same program. Thats not how it works. Everybody has a different program. They just all want the same guy dead. Socrates was a gadfly, but I bet he took time out to screw somebodies wife" Gus Vitelli
Any opinions expressed by me are mine alone, posted from my home computer, on my own time as a free american and do not reflect the opinions of any person or company that I have had professional relations with in the past 49 years. meep questions, help? [link|mailto:pappas@catholic.org|email pappas at catholic.org]
|
Post #231,804
10/31/05 10:45:07 PM
|
:-\ufffd :-\ufffd :-\ufffd :-\ufffd
jb4 shrub●bish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT
|
Post #231,756
10/31/05 5:28:23 PM
|
There's a lot of stuff at scholar.google.com
I'm not sure how well it would satisfy partisans though. I haven't come across a study of the benefits of brace placement rules, but there are things like [link|http://research.sun.com/people/mlvdv/COM.sun.mlvdv.doc.ist_02.paper_pdf.pdf|this] 17 page .pdf by Van De Vanter at Sun: pp.1-2: Unlike the textual representation of linguistic structure, which includes keywords, identifiers, operators, and punctuation, documentary structure consists of those textual aspects explicitly defined to be not part of the language: white space (new lines, spaces, tabs), comments, and choice of names.
Viewed differently, documentary structure is what programmers add to source code for the sole purpose of aiding the human reader. This is of enormous importance because of the central role of reading during software development [14]. Programmers clearly understand this: they arrange code carefully, complain about inadequate comments, and argue passionately about the exact placement of braces in code (purely a matter of white space in most languages).
It is almost tautological that documentary structure is outside the formal language. It is a much more subtle fact that documentary structure is mostly orthogonal to language structure. An important consequence is that compiler-oriented tools do not represent documentary structure adequately. Compilers discard this information freely because it is not needed: humans seldom read compiler output. For other language-based tools, however, losing documentary structure violates the tool builder\ufffds equivalent of the physician\ufffds oath to \ufffdfirst do no harm.\ufffd Cheers, Scott.
|
Post #231,786
10/31/05 9:48:21 PM
|
I think it depends on your visual habits.
I'm into white-space and correct indents, so I use the K&R brace-style. It also means I like spaces around my = signs, and after commas and around expressions in if, while, switch, etc.
And I know I'm extremely partial to Vim's syntax highlighting... :-)
Wade.
"Insert crowbar. Apply force."
|
Post #231,856
11/1/05 9:29:21 AM
|
Sidenote
most of the arguments on this are based on the fact that most code isn't adaptable to the bracing style the individual prefers.
BUt i was using (god, can't remember the name now....) jalopy with Java and multiple programmers. I prefer White style, one prefers K&R, etc. Get the code, run it through jalopy, update the changes, push it back to the "standard style" and be done.
My only other comment is that certain language features (anonymous classes in Java for one) seem to favor certain bracket styles (K&R).
|
Post #231,859
11/1/05 9:37:17 AM
|
Automate that Jalopy
CVS allows you to run processes (like, say, Jalopy) when checking code in. Set this process up, create a team-agreed-upon standard style of formatting code, and then whenever you check code in, it will automatically be reformatted to project standards, no matter how it was coded.
If you'd like to know the steps to do this, I'll go look up the reference I used to get it done for our project a few months back. Let me know.
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #231,861
11/1/05 9:44:25 AM
|
That's okay...
- We're on ClearCase (employer required) -- ClearCase does allow for scripts to run on check-in also
- That project was a while ago...onto other things. <shrug>
|
Post #231,866
11/1/05 9:59:47 AM
|
np
Every company is different, and we've all got our own unique requirements.
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #231,877
11/1/05 11:14:00 AM
|
Yes, that's a PBP recommend :-)
The Perl equivalent is perltidy.
Formatting each to his own style is great for shared source code. But somehow it seems that people wind up seeing the default format more often than you'd expect. And so will argue about it. *shrug*
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,733
10/31/05 3:59:22 PM
|
I'm seeing comments that "Code Complete" addresses that
and argues that your approach (open brace stays with the statement) is best.
Cheers, Scott. (Who hasn't read CC but probably should one of these days...)
|
Post #231,737
10/31/05 4:06:37 PM
|
Where?
By my recollection Code Complete is pretty neutral on the topic. Perl Best Practices, however, takes a stand, prefering my style.
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,740
10/31/05 4:13:13 PM
|
CC2, pp 740-743
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #231,757
10/31/05 5:28:44 PM
|
I should finish reading it then
I got bogged down in comments...
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,770
10/31/05 6:45:18 PM
|
Ar
I read it like a book -- not a reference manual -- some number of months ago. As the novel progressed, I became more and more disappointed and disillusioned by what my company (at the time) was not doing correctly.
A change of company in the interim has not changed the outlook for what's being done correctly, either.
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #231,821
11/1/05 12:03:59 AM
|
I read the first one that way, the first time
I've read the first one multiple times. I was going through the second one with a fine-toothed comb, and lost energy to continue around 11 months ago.
It wasn't the book that was at fault. :-)
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,743
10/31/05 4:16:39 PM
|
E.g.
[link|http://www.sitepoint.com/blogs/2005/08/08/coding-style/|Here], by Noam A., ~ 2/3 of the way down. Unfortunately, the example is broken at that web site so it's not terribly clear what Noam prefers.
The CC discussion is apparently on page 734 of the 2nd edition, according to the index shown at Amazon.
A discussion on various indentation and brace styles is at [link|http://en.wikipedia.org/wiki/Indent_style#K.26R_style|WikiPedia].
My preference, with no substance behind it, is something approaching GNU Style at the moment.
FWIW. HTH.
Cheers, Scott.
|
Post #231,745
10/31/05 4:33:47 PM
|
What I see on that page:
By way of example:
K&R [image|http://www.mr-anderson.net/images/knr-csr.png||||]
BSD [image|http://www.mr-anderson.net/images/bsd-csr.png||||]
Whitesmith [image|http://www.mr-anderson.net/images/whitesmith-csr.png||||]
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,746
10/31/05 4:58:18 PM
10/31/05 7:25:29 PM
|
I see.
[edit:] (I didn't initially see the images at home either, but was able to on loading the URLs directly, and then on refreshing the page the images all came up. Weird...)
I remember when you first talked about your condition. It certainly is interesting! It must make your love of reading quite a challenge these days. :-(
I see why you'd prefer the BSD-style. Do you use a "code folding" mode in emacs also? I like the idea of bending stuff out of the way when I don't need it, but I wonder if it's worth the trouble.
Cheers, Scott. (Who uses the IDE editor in Borland C++ Builder 5 and UltraEdit and the built-in editor in FileCommander but hasn't done any code folding...)
|
Post #231,747
10/31/05 4:59:37 PM
|
Dunno what to tell you...
Direct links if that helps:
http://www.mr-anderson.net/images/knr-csr.png http://www.mr-anderson.net/images/bsd-csr.png http://www.mr-anderson.net/images/whitesmith-csr.png
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,750
10/31/05 5:10:10 PM
|
www.mr-anderson.net could not be found...
Dunno. Adblock isn't blocking anything on Z. I'll try at home after the monsters are gone.
Cheers, Scott.
|
Post #231,751
10/31/05 5:11:10 PM
|
Can you dig it?
As in: ophelia:~/Temporary peter$ dig www.mr-anderson.net\n\n; <<>> DiG 9.2.2 <<>> www.mr-anderson.net\n;; global options: printcmd\n;; Got answer:\n;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29858\n;; flags: qr; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0\n\n;; QUESTION SECTION:\n;www.mr-anderson.net. IN A\n\n;; ANSWER SECTION:\nwww.mr-anderson.net. 38400 IN A 66.80.246.91\n\n;; Query time: 12 msec\n;; SERVER: 10.200.1.50#53(10.200.1.50)\n;; WHEN: Mon Oct 31 22:10:47 2005\n;; MSG SIZE rcvd: 53
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,749
10/31/05 5:06:27 PM
|
They work here. Check your browser/adblock.
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,785
10/31/05 9:36:57 PM
|
For some reason reading text isn't so bad.
Most of the time the left eye just gives up and I see only ghosts. For whatever reason, code brings it to the fore.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,793
10/31/05 10:15:49 PM
|
Would you be better off wearing a patch?
|
Post #231,808
10/31/05 10:56:45 PM
|
Tried that for a bit.
And while it appeals to my inner pirate, after a while I get a raging headache.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,734
10/31/05 4:02:21 PM
|
It's not just "bad vision"
I have *double* vision. Not side-to-side -- vertical double vision. My left eye picks up stuff from the line below and places it in the center of my vision, and all the time, not just when I am tired. Combine that with the shifted text and I have severe issues with other brace styles. I'm not saying that this style works if you have bad eyes. I'm saying that in my particular situation it's the best I've found, and using it significantly enhances the readability of code for me.
And as long as we're talking anecdotally, I much preferred the compact brace style before I had double vision.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,736
10/31/05 4:04:13 PM
|
I'm *so* playing you at pool, for money.
Peter [link|http://www.no2id.net/|Don't Let The Terrorists Win] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #231,739
10/31/05 4:09:40 PM
|
Bastard.
And when I finally come to visit you and Jo I'll make sure to straighten all of the pictures on your walls.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #231,744
10/31/05 4:17:42 PM
|
:-D
|
Post #231,742
10/31/05 4:14:13 PM
|
ICLRPD: I'm *so* playing you at pool, for money. (new thread)
Created as new thread #231741 titled [link|/forums/render/content/show?contentid=231741|ICLRPD: I'm *so* playing you at pool, for money.]
-- Steve [link|http://www.ubuntulinux.org|Ubuntu]
|
Post #231,758
10/31/05 5:30:02 PM
|
You're English. Isn't that advantage enough?
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #231,766
10/31/05 5:53:56 PM
|
It's a pretty print problem...
... one that should be fixable by your local edit environment. But if truth be told, the proper format goes something like: IF (someCondition) THEN\n BEGIN\n someStatement\n END
|
Post #231,767
10/31/05 6:31:57 PM
|
Code Complete reserves special criticism for that one
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|