Re: Idioms considered harmful
by Abigail-II (Bishop) on Oct 15, 2002 at 11:50 UTC
|
I disagree. Idioms are (usually) not obscure, but more important,
it isn't an idiom if only a few will know what it does.
Idioms are constructs that are typically used for a certain task.
Good examples of idioms are the constructs to iterate over an array:
/* C idiom */
for (i = 0; i < arr_length; i ++) {
printf ("%d\n", arr [i]);
}
# Perl idiom
foreach (@arr) {
print "$_\d";
}
C probably has more idioms than Perl, if only because having more
ways of doing something is typical Perl. Perl also has many buildins
where you would use a C idiom in a C program.
Having one way of doing things (whether that's because of the way
the language is designed, conventions or company policies) does have
its merits. Company coding standards aren't there for the sole purpose
of annoying people (although they can be annoying ;-)), Python has
its share of followers, and even in the Perl community you hear too
often "you shall do it this way".
That of course doesn't mean I don't like the many ways of doing things
in Perl.
Abigail
| [reply] [d/l] |
Re: Idioms considered harmful
by jmcnamara (Monsignor) on Oct 15, 2002 at 11:49 UTC
|
To learn a language you have to learn the idioms as well.
In The C Programming Language there is a discussion on how to write a strcpy() function. After several iterations the code is reduced to the following:
while (*s++ = *t++)
;
The comment that follows this is enlightening:
Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you will see it frequently in C programs.
Idioms in natural language and programming languages are just well known concise ways of saying something. Just because something is concise doesn't make it an idiom. For something to become idiomatic it must be useful enough to pass into common usage. As such it is unlikely that an idiom could be considered harmful.
I'd agree however, that Golf is best avoided in production code.
--
John.
| [reply] [d/l] |
|
|
Ironically that book is full of lots of examples of how to not write C. Try that trick strings that might overlap and you can wipe out half your data. Even if you think that it is safe, is having to worry worthwhile?
| [reply] |
|
|
The standard library strcpy() doesn't handle the case where strings overlap either. Or at least it's behaviour is undefined.
--
John.
| [reply] |
Re: Idioms considered harmful
by BrowserUk (Patriarch) on Oct 15, 2002 at 11:07 UTC
|
"...considered harmful" considered harmful.
So, your guys wouldn't understand local, $/, or list assignment greediness, but would understand anonymous arrays, arraypointer dereferencing, chained maps?
Would they also understand when not to use a ST?
5 out of 6 times (example) when the ST has been advocated here as a solution recently, I've benchmarked it versus the simple sort version, the simple version has been quicker. This isn't a case of micro-optomisations either as the sole purpose of the ST is to speed up the the process. The ST is great, but like any idiom, it needs to be understood and applied with knowledge.
Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
| [reply] |
Re: Idioms considered harmful
by tommyw (Hermit) on Oct 15, 2002 at 10:38 UTC
|
While I can see your point, it does raise the question of where you want to draw the line; I've been questioned over the last week on the use of:
- (?:
- qw
- qr
- eval { }
- map { } grep { }
Some of these I could replace with longer code with the same effect (the qw) or remove completely with no ill effects (the non-capturing parentheses were just being a habit). But I was somewhat horrified at being instructed to put in a comment explaining that perl uses eval for exception handling.
So, we really need to distinguish between expected and unexpected idioms. And that's going to be a hard line to draw...
--
Tommy
Too stupid to live.
Too stubborn to die.
| [reply] [d/l] [select] |
|
|
map { MAPCLAUSE } grep { GREPLCAUSE }
Can be rewritten as:
map { GREPCLAUSE ? MAPCLAUSE : () }
For instance, if you want a list of the squares of the first five odd numbers, either of these will work.
@odds_squared = map { $_**2 } grep { $_%2 } 1..10;
@odds_squared = map { $_%2 ? $_**2 : () } 1..10;
Result: 1 9 25 49 81
-Blake
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: Idioms considered harmful
by joe++ (Friar) on Oct 15, 2002 at 11:12 UTC
|
Well, yes and no.
In the thread Cheap idioms it has been mentioned
by Aristotle and Ignatz amongst others, that the acceptability of such an idiom depends on the level of (self-)documentation. This can be either a simple comment (just a few words, otherwise more verbose code would be better IMHO), or, by wrapping the idiom in a subroutine with a self documenting name.
On the condition that warnings and errors are properly reported, I don't see any obvious trouble for maintainers.
To clarify, let's say I see the line of code I don't understand, which is commented with "slurp in file" and I recognise a filename being used there. Now either the code "just works", or it breaks with a proper error mesage ("no such file at line 123").
In the first case I might get interested in why this works and learn something (mind you!), or just take the working for granted.
If there is a failure, I have a clear signal where to go looking: "No such file" still means that I have to trace the file and path names, rather than debugging the "slurp" line.
The bottom line: in my opinion there is a blurred line between knowing what goes on and just using "an idiom" - honestly, which Perl user is fully aware of the C-level system calls underlying Perl's open() builtin? And do we need to know?
--
Cheers, Joe | [reply] |
|
|
Not only that, but using read is faster!
Well, suppose it is, or something is. If the self-documenting-name function was used, then it could be changed in one place.
| [reply] |
|
|
| [reply] |
Re: Idioms considered harmful
by Tanalis (Curate) on Oct 15, 2002 at 10:58 UTC
|
I think I agree almost entirely with this; however I can't help but feel that Perl in its current form is set up to allow idioms for almost any "simple" task.
The mere existance of variable "shortcuts" such as $/ and the like enables a whole host of one-liners, especially when coupled with $_, @ARGV and <> - almost certainly at the expense of code readability.
I think care needs to be taken whereever idioms are used - especially with code that others are going to have to maintain - the same care that should be taken when writing any code to ensure that it's readily understandable.
Future maintenance is the key - if it's not clear what it does, it's not going to be easy to maintain, in my opinion.
--Foxcub
| [reply] |
Re: Idioms considered harmful
by chromatic (Archbishop) on Oct 15, 2002 at 17:01 UTC
|
What do you mean by the word idiom? It seems that you're assigning it a meaning more akin to obfuscation. In common use, it is instead an expression, common or syntacticallly common to a language or speaker group, often difficult to understand divorced from the specific context.
I think it would be fair to say that programming *without* Perl idioms leads to far less maintainable code.
| [reply] |
Re: Idioms considered harmful
by admiraln (Acolyte) on Oct 15, 2002 at 17:09 UTC
|
Idioms are the life blood of a language and without them you completely neuter the language.
Last year I wrote and presented local Student ACM chapter on
Generic/Idiomatic Programming. The core issue was of future mantainablity. I wrote it because I needed a way to show others that Generic Programming is bad.
A Generic program is one that is syntactily simple enough that with only very slight changes could run in any language.
The Generic programs were long and cumbersome and as a result obsured the task at hand. They would unravel idioms to equivalent code that would be syntactily "easy" to maintain.
I have seen 3 to 1 differences in line counts between Generic programs and good Idomatic programs. No one will say its easier to maintain programs that are that much longer.
With the right level of idomatic programing the code does its job and lets the real solution show through.
From from my experience Generic programming is favored by those who work in mulitple languages on a regular(even daily) basis and are trying to avoid idioms that do not port well. This is done to avoid coding themselves in to a cornor by anticipating a particular idiom.
Disclaimer | [reply] |
Re: Idioms considered harmful
by DapperDan (Pilgrim) on Oct 15, 2002 at 15:41 UTC
|
You say (correctly, IMO) that the idiom requires knowledge of:
- local;
- the input record seperator, $/;
- ARGV magic;
- list assignment greediness.
FWIW, I think that's a useful list. It could even be seen as a succinct analysis of how the idiom works. It made me appreciate how much Perl I have learned over the past year.
I knew all of the items except for no. 3, the ARGV magic, which Aristotle pointed out to me, and which I then went off and learned about in perlop (section: "I/O Operator"). I have started using the idiom in my own production code (which is not subject to a code review, except by myself).
I do appreciate your point of view, nonetheless I think it goes too far.
Your point about coworkers really is very subjective. It doesn't say anything about how valid the idiom is *as an idiom*. But that's the whole point, I think, idioms are subjective. And as others have mentioned already, it will always be near impossible to draw a line or boundary for something subjective like this.
Yes the corporate/coding_standard reaction to this will often be to just ban it, but that doesn't make it right. Tomorrow they could be banning <> or $_ as well. | [reply] |
|
|
As a sidenote, I always use the diamond operator magic in commandline tools. It has significant advantages: considering the scripts are usually short, getting all I/O dealt with in a single while(<>) is really convenient, and it deals with any number of files without me having to write an extra loop over @ARGV. I also have these scripts write any actual output to STDOUT and errors or other messages strictly to STDERR, so that the user can redirect or pipe the result (or just let it print to screen) at their convenience. And because these handles are given, I don't need to deal with opening output files either. Also since the diamond operator peeks at @ARGV at the time it is invoked, I can munge any commandline parameters with Getopt::* beforehand, and it works out beautifully.
Bottom line, I can have my cake and eat it too: a lot less code, and consequently work, but maximum flexibility. Perl rocks that way.
Makeshifts last the longest.
| [reply] |
Re: Idioms considered harmful
by ignatz (Vicar) on Oct 15, 2002 at 14:44 UTC
|
Your criteria seem difficult to define. What is "understandable" in Perl? What is "beautiful"? Perl's very nature seems to make setting these standards very difficult. Is there an equivalent in Perl to the Hoch Deutsch or King's English that news casters use to allow for the maximum clarity?
For me what is important is what is explained. I love strumbling upon some advanced technique that I've never seen before (but please, in production code could you throw me a bone and add a comment?).
()-()
\"/
`
| [reply] |
Re: Idioms considered harmful
by Aragorn (Curate) on Oct 15, 2002 at 17:48 UTC
|
One of the things you constantly do as a programmer is learning new things. In Perl, there's a lot to learn. Going from Perl Baby Talk to Idiomatic Perl is a long process in which one learns to express themselves well.
Other people reading your code and finding it difficult to read should not run screaming to you and complain about unreadable code. Instead, I think that they should ask you if the technique you used is a common idiom in Perl. If yes, they should master the idiom themselves and possibly teach others too. Mind you, bad code is bad code, but there are many short, fast and elegant Perl solutions which are not immediately obvious, but on closer inspection, are very valuable things to know.
Python, which is praised as an easy-to-read language, has a number of idioms. I'd be hard-pressed to believe that someone's first encounter with, for example, list comprehensions is going to be more successful than the Schwartzian Transform (Ok, maybe that's a little strong, but I'm sure you get my point :-).
I think that being a good programmer requires you to keep an open mind about things you don't know. Crying "I can't read this!" when a little more investigation shows that it's a very elegant solution is not my idea of keeping an open mind.
--
All that is gold does not glitter... | [reply] |
Re: Idioms considered harmful
by gjb (Vicar) on Oct 15, 2002 at 22:33 UTC
|
At my company, we have knowledge engineers and software engineers. The knowledge engineer do their job in Perl or Lisp and produce code that gets ported to C/C++ by the software engineers.
There are several reasons for this setup, but the upshot is that the knowledge engineers can do their work quickly and efficiently, while the software engineers take care of speed issues, stability and maintenance, each concentrating on what he's best at.
However, the price to pay for me is that I have to refrain from using Perl to the extreme, in the sense that I can't use idioms that are too hard to translate to other programming languages, e.g. C/C++
This might be the point to draw the line for those who ever want to get their code ported (as well as those who'll have their code ported, willingly or otherwise).
map is easy to translate, grep a little harder, but a file slurp?
Just my US$0.02...
-gjb-
| [reply] |
|
|
A little of topic, but...
In C/C++ you do a file slurp with a open/mmap system call, and assign the resulting pointer to a collection object of your choice(char*,vector<whatever> or string). In fact, if your c++ programmers are any good, they should be able to handle most of the perl code you can throw at them, at least the idioms, without much problem. Doing it in C on the other hand....
Closeures are doable in C++, but its ugly and I believe it can't do everything(Could be wrong on that). Regexps can be a pain if they don't use a proper regexp library. Be careful with reblessing references into other classes, or dynamically changing @ISA(I cant think of any idioms that actually do that....).
just my 0.2 kroner
goldclaw
| [reply] [d/l] [select] |
Re: Idioms considered harmful
by LEFant (Scribe) on Oct 16, 2002 at 01:38 UTC
|
What is really harmful to most of our sensibilities those situations where the authority structure we are working in allows participants of insufficient skill in a language or technology to reduce technique to the lowest common denominator. Unfortunately, uneven skills what they have to work with. It takes the greatest management skill to balance this this tension.
Bob, with a thick hide... | [reply] |
Re: Idioms considered harmful
by greenFox (Vicar) on Oct 16, 2002 at 06:24 UTC
|
dictionary.com gives 5 meanings for idiom:
- A speech form or an expression of a given language that is peculiar to itself grammatically or cannot be understood from the individual meanings of its elements, as in keep tabs on.
- The specific grammatical, syntactic, and structural character of a given language.
- Regional speech or dialect.
- A specialized vocabulary used by a group of people; jargon: legal idiom.
- A style or manner of expression peculiar to a given people: “Also important is the uneasiness I've always felt at cutting myself off from my idiom, the American habits of speech and jest and reaction, all of them entirely different from the local variety” (S.J. Perelman).
- A style of artistic expression characteristic of a particular individual, school, period, or medium: the idiom of the French impressionists; the punk rock idiom.
sense 1. seems like it could apply to code that is difficult to understand but in fact it cannot apply: the meaning and action of any piece of perl code is always able to be discerned from the individual elements (if one understands those elements thoroughly enough)- take a look at Re: camel code (SPOILERS!) for an example :) If it can be run by perl (the program) then it can be understood. I am not saying it is OK to write production code that is unclear or obfuscated, just that the word "idiomatic" is the wrong word to describe such code.
sense 2. As Abigail-II pointed out this is what is usually meant by idiom when discussing programming languages. I used to occaisionly resort to wrapping perl 1 line filters in shell code because I didn't have the perl in-place edit idiom in my tool kit. I have seen quite a bit of perl code which I have been able to say (with some accuracy) if the coder was a shell or C or "other" programmer. If you are not using Perl idioms then what language are you programming in?
senses 3-5 don't really apply to programming.
I'm not sure what the connection between idiomatic perl and cargo cult programming is supposed to be. The idiomatic perl solution would generally be a briefer and more concise version of the non-idiomatic version, which is opposite to the cargo cult method of "ritually including code which serves no real purpose". I ritually include cgi.pm in my cgi scripts (without attempting to understand cgi.pm), that is not cargo cult programming because cgi.pm serves a purpose and there are good reasons why it is better than other solutions.
Does obfuscation belong in production code? Definately not. Where is the line between obfuscation and a language idiom? I'm not entirely sure :) If in any doubt comment or re-code clearer.
-- Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth | [reply] |
Re: Idioms considered harmful
by zuqif (Hermit) on Oct 15, 2002 at 14:59 UTC
|
Excuse me! Whats that I see?
A recursive title!? (:
the 'qif; | [reply] |
|
|
| [reply] |
Re: Idioms considered harmful
by rinceWind (Monsignor) on Oct 16, 2002 at 10:45 UTC
|
Thanks to all for the many replies to this post. Indeed I was playing Devil's Advocate, making the post provocative to attract comments and discussion - and because I genuinely wanted to know what people consider an idiom. I wanted to see whether others considered recognised idioms good practice/obfu/cargo cult.
Plus, I did not feel that the issue of comprehensibility by the hoi polloi had been covered. I was interested in this because I find myself regularly having to debate the Java versus Perl argument, see Perl advocacy, CGI/ModPerl vs ASP/JSP.
For career reasons, I'm learning Java by the way, and I miss the intellectual freedom of TIMTOWDI. | [reply] |