09.09
Today, only fRew and I ended up meeting for September Dallas.p6m meeting. Turns out several people forgot that today was in fact the Tuesday after Labor Day and not Monday. We mostly just sat and talked about miscellaneous software things. We discussed GPS, my Algorithms course, Parrot, PGE, and styling. Styling is the last thing that we discussed and one that seemed semi-heated.
I discussed my reasoning for my styling quirks, which fRew insisted he would replace in a heartbeat. I’ve mostly been honing my preference for certain things by finding bugs using styles that seem to lead towards common mistakes. For example, I always place if statement parenthesis immediately after the ‘f’ because you cannot have one without the other (well, you can have the boolean expression alone but it rarely makes sense unless it’s a return value). I tried to apply the same reasoning to why I put my braces at the end of the if line with a space between the ‘)’ and the ‘{‘. I do this because the block started by ‘{‘ can exist without if clause. I always keep the ‘{‘ on the same line so attempts to comment out the if clause will fail to compile as I’ve found and fixed too many bugs as a result of true laziness.
Now that may seem kinda wierd, but it’s 1) a mental seperation technique and 2) an attempt to reduce the number of standalone nested blocks (that can do odd things like cause variable scope issues).
Then we started discussing using ampersand, ‘&’, to begin functions. My reasoning is because I more often than not prefer to be as explicit as possible, and the ‘&’ let’s me do that. I failed to recall an example that led me to preferring the use of ampersand, but I eventually found it. Basically, functions should look like function calls and not keywords, macros, or other environment lexicals. &foo($arg1, $arg2); looks a bit hairy and dated (generally a perl4 way of doing things), but it’s clear from the first character what is about to happen. My brain needs only to parse the first character to read the code with the right mindset. I am calling a user defined function (not a built-in), named ‘foo’, and passing 2 arguments. That is clear, readable, and will likely work for the forseeable future; if not, it’s still easy to find and correct.
On the other hand, foo; or foo(); (under strict) is not necessarily clear. The first example is basically a bare-word and could be any number of things. It could be a symbol, a package, a string, or a function call. The arguments passed would be @_, which requires more investigation. The second one looks like a subroutine call but I have to parse 5 characters and then grep around for a sub named foo within the current namespace (was it loaded elsewhere and exported to my namespace?). While both of these are more compact and concise, they also both require more work to figure out exactly what is happening.
Also, foo($arg1, $arg2); is more clear but not until I’ve read the minimum of 4 characters to I start to think it might be a function call. This does not parse and skim nearly as quickly, at least to me.
All of this skimmable code talk (note: I don’t agree with Schwern, end-of-scope comments usually clutter code more than they help) may sound frivolous to those readers who deal with thousands of lines of code. It’s not something you can truely appreciate until you maintain code that weighs in with at least 9 digits (executable code only). I personally manage 250,000 lines and I am responsible for a product that is about 2 million lines (all 30 of our branches are about 2 million lines each).
In the end, I stick by my preference for ampersand function calls unless someone else can point out a better reason to ditch them.
Consider the “Why in the world…?” factor of maintenance.
Experienced Perl 5 programmers know the effect that the leading ampersand has on prototyped function calls and will go a little bit crazy trying to figure out why you want that behavior.
I agree with chromatic. People who know Perl will look at your code and either think that you don’t know modern Perl programming or that you’re explicitly trying to bypass prototypes. Neither of which is true. There comes a point where personal style causes more problems than it fixes when it’s so much against what the rest of the world is doing.
In a similar vein, I used to have a massive .perltidyrc file that made things exactly (or pretty close) like I wanted them. Over time as I’ve worked with other people’s code more that file has been reduced to just a handful of lines. I’m happy with my code and now I’m happy when I work on other people’s code too.
“On the other hand, foo; or foo(); (under strict) is not necessarily clear.”
You mean *without* strict. With strict, foo == &foo. And strict or no &foo() = foo().
I still say
if (1) {
say join ‘,’, @list;
foo(@args);
}
is better.
Actually I would replace ‘,’ with q{,} as I think it is easier to parse with the eyes than ‘,’, But that’s something I’d be willing to yield.
Oops, my bad &foo probably doesn’t == foo as &foo does things that you probably don’t want, whereas foo = foo()
Okay, so the main argument against is that it’s not considered modern Perl and that it’s use may confuse people (is he trying to bypass prototypes or just a moron?).
I can see the argument against, but I’m not exactly convinced that doing without ampersand is better than increased scanability.
Is there a specific bug or problem that I may inadvertantly trigger by using ampersand? Is there an issue that may crop up with rotting code or maintanence issues that may not happen without its use?
The point of the article was to show that, while I understand it’s not considered common practice anymore, the reason for it seems stronger than without.
Hi, I have a couple jobs that I would like to advertise on your site or via an email list to inform your readers about Perl programming jobs. Please get back to me as soon as you get a chance.
Look forward to hearing from you.
Chris
Is this real? If so, find me in #perl6 on irc.freenode.net.