Today I found myself writing
do A if possible, else do B if possible, else error
English that is.
But it caused me to think about Perl's Postfix if
statement if condition;
versus
if( condition ) {code} [[elsif(cond) {code}]* else {code}]
i.e.
if( condition ) {code}
if( condition ) {code} else {code}
if( condition ) {code} elsif(cond) {code} else {code}
etc.
Strange to many programming languages, but sometimes makes conde more readable.
I wonder if the posifix if woth else, that I wrote in English, might be worth thinking about.
Syntactically it is a ternary operator:
operand1 IF operand2 ELSE operand3
I vaguely recall disjoint operator and function syntax - Algol 68? or was it Algol 60? - ...
Disclaimer
The content of this blog is my personal opinion only. Although I am an employee - currently of Nvidia, in the past of other companies such as Iagination Technologies, MIPS, Intellectual Ventures, Intel, AMD, Motorola, and Gould - I reveal this only so that the reader may account for any possible bias I may have towards my employer's products. The statements I make here in no way represent my employer's position, nor am I authorized to speak on behalf of my employer. In fact, this posting may not even represent my personal opinion, since occasionally I play devil's advocate.
See http://docs.google.com/View?id=dcxddbtr_23cg5thdfj for photo credits.
See http://docs.google.com/View?id=dcxddbtr_23cg5thdfj for photo credits.
Showing posts with label programming languages. Show all posts
Showing posts with label programming languages. Show all posts
Friday, June 29, 2012
Tuesday, May 01, 2012
Comments at beginning of line?
code; // we have comments at the end of a line
code /* and comments anywhere on a line, like in the middle */ code;
/* where "anywhere" can be at the beginning: */ code;
but it occurs to me that I often want comments at the beginning of a line, e.g.
foo(
/* arg1= */ 111,
/* arg2= */ 222,
)
and although /*...*/ can accomplish this, they are visually heavyweight.
I wonder if, in a hypothetical new language, we could have comments from the beginning of a line?
Now, ":" would be the most natural comment delimiter for this - if it were not already used for so much else:
foo(
arg1: 111,
arg2: 222,
)
Note that I have distinguished comments in a special color. One might say that such a special comment color is what we really want... I agree, and have discussed that elsewhere. But it is certainly traditional to have program text read monocolor:
foo(
arg1: 111,
arg2: 222,
)
Now, you might also argue that keyword arguments subsume the example above. Sure... but then I can create other examples that are not keyword arguments.
I can imagine comment from beginning of line delimiters other than colon, but none are quite so elegant:
foo(
arg1>>> 111,
arg2:>>> 222,
)
foo(
arg1==> 111,
arg2==> 222,
)
foo(
arg1::: 111,
arg2::: 222,
)
Again, we are limited by the ASCII, or UTF, character set.
code /* and comments anywhere on a line, like in the middle */ code;
/* where "anywhere" can be at the beginning: */ code;
but it occurs to me that I often want comments at the beginning of a line, e.g.
foo(
/* arg1= */ 111,
/* arg2= */ 222,
)
and although /*...*/ can accomplish this, they are visually heavyweight.
I wonder if, in a hypothetical new language, we could have comments from the beginning of a line?
Now, ":" would be the most natural comment delimiter for this - if it were not already used for so much else:
foo(
arg1: 111,
arg2: 222,
)
Note that I have distinguished comments in a special color. One might say that such a special comment color is what we really want... I agree, and have discussed that elsewhere. But it is certainly traditional to have program text read monocolor:
foo(
arg1: 111,
arg2: 222,
)
Now, you might also argue that keyword arguments subsume the example above. Sure... but then I can create other examples that are not keyword arguments.
I can imagine comment from beginning of line delimiters other than colon, but none are quite so elegant:
foo(
arg1>>> 111,
arg2:>>> 222,
)
foo(
arg1==> 111,
arg2==> 222,
)
foo(
arg1::: 111,
arg2::: 222,
)
Again, we are limited by the ASCII, or UTF, character set.
Tuesday, January 24, 2012
Code ordering, want none: perl example
I have several times posted how I find languages without code ordering pleasant, more readable.
E.g.
E.g.
let x = y + z
where z = some log and complicated expressions
Here's an example from writing "shell scripts" - although here I am doing the shell script in Perl:
Start off with:
system("command with some long and complicated command line");
Realize that you want to repeat the command in a pre-announcement of what you are doing,
and in an error message:
Start off with:
print "RUNNING: command with some long and complicated command line\n";
my $exitcode = system("command with some long and complicated command line");
print "error: exitcode=$exitcode: command with some long and complicated command line\n"
if there_is_a_problem($exitcode);
Now avoid repetition:
Standard way:
my $cmd = command with some long and complicated command line";
print "RUNNING: $cmd";
my $exitcode = system("cmd");
print "error: exitcode=$exitcode: $cmd\n"
if there_is_a_problem($exitcode);
In my opinion the non-code ordered way is more readable:
{
my $cmd; # maybe some declaration to say it is not ordered?
print "RUNNING: $cmd";
my $exitcode = system($cmd = "command with some long and complicated command line");
print "error: exitcode=$exitcode: $cmd\n"
if there_is_a_problem($exitcode);
}
I think it is more readable because you see the value set at the point where it matters,
where it is used most intensely (the other uses are mild, just prints).
Note that I have used a scope.
Subscribe to:
Posts (Atom)