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.

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.
  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.

3 comments:

Anonymous said...

Next can you address if it should be dest,src or src,dest in assembler, as well as that whole endianess thing :-)

Joking aside there are several things being addressed here. One is DRY (don't repeat yourself). The other is diagnostics when things aren't going right. You end up littering your code with prints until you figure it out. What would be ideal in this case is some way of annotating that system should take less than a second to execute and always succeed. The language/runtime should then automatically display parameters in the case of error or if it is taking longer than normal to run.

Andy "Krazy" Glew said...

"Next can you address if it should be dest,src or src,dest in assembler"

I've posted on that. Neither.

It should be

dest := OP(src)

or

dest OP= src

or

OP ->dest, <-ssrc

or

OP in out dest, in src

or

...


I think that one of the best things I did for Intel microcode was make it use the

dest := OP( src1, src2 )

notation.

Fewer bugs.

One of the weaknesses was tyhat eventually it becaame


dest := OP( src1, src2 ).special_annotatyions...


where the special annotations were often essentially little instruction streams executed at different pipestages, such as at the renamer and at retirement.

Andy "Krazy" Glew said...

One of the biggest regrets I have is that I acquiesced, let my programming partner win, in a simulator at another company, where the microinstructions ended up looking like

OPCODE r1, r2, r3, r4, r5, ...

I.e. with no syntax helping understand which was which.

Although, to be fair, he later added input/output indications.

OPCODE <>r1, >r2, reg2

although it is silly to have keywords for all operands

COMPARE-AND-BRANCH-AND-LINK-INDIRECT-GE reg1, reg2 target<-reg3, link->reg4

For some reason I find that < and < do not indicate direction as well as <- and ->. Wish I worked in a character set where there were real arrows.