2007-04-17 17:25:17 Misc PHP/FScript comparison
Now, granted, there is much that is done on the API-side to make life more custom fit for each language, but given equal attention to doing this particular task, it's interesting to take this snapshot comparison of exactly equal functionality:
( php )
  1  foreach( $this->works->each( $wholeOrder, $whichway ) as $work )
2 $this->page->setMultipleFieldsForTemplate( 'work', array(
3 'title' => $work['title'],
4 'author' => $work['author'],
5 'years' => $work['startyear'] .
6 ( $work['startyear']==$work['endyear'] ? '' : '-'.$work['endyear'] ),
7 'titleStyle' =>
8 ( $work['id']==$currentID ? 'bookCurrent' :
9 ( ( $work['important']=='Y' and $work['done']==1 ) ? 'bookImp' :
10 ( $work['done']==1 ? 'bookNotImpDone' : 'bookNotImp' ))),
11 'bookStyle' =>
12 ( $work['id']==$currentID ? 'bookCurrent' :
13 ( $work['done']==1 ? 'bookDone' : 'bookUndone' )),
14 ));


The F-Script code has no "?:" style conditional, but has (testCondition) ifTrue:[…] ifFalse:[…] conditional. Wordier, but does lend itself to some improved readability. Nesting them requires more concentration, but that's even more true with the "?:"-styled code above.
( fscript )
  1  workBlock := [ :work | {
2 'title', work valueForKey:'title',
3 'author', work valueForKey:'author',
4 'years',
5 (work valueForKey:'startyear')++
6 ((work valueForKey:'startyear') = (work valueForKey:'endyear')
7 ifTrue:[''] ifFalse:[ '-'++((work valueForKey:'endyear') printString) ]),
8 'titleStyle',
9 ((work valueForKey:'id') = currentId
10 ifTrue: [ 'bookCurrent' ]
11 ifFalse:[ ((work valueForKey:'important')='Y') & ((work valueForKey:'done')=1)
12 ifTrue: [ 'bookImp' ]
13 ifFalse:[ (work valueForKey:'done') = 1
14 ifTrue: [ 'bookNotImpDone' ]
15 ifFalse:[ 'bookNotImp' ] ] ] ),
16 'bookStyle',
17 ((work valueForKey:'id') = currentId
18 ifTrue: [ 'bookCurrent' ]
19 ifFalse:[ (work valueForKey:'done') = 1
20 ifTrue: [ 'bookDone' ]
21 ifFalse:[ 'bookUndone' ] ])
22 } ].
23 page setMultipleFields:allWorks usingBlock:workBlock forField:'work'.

One thing that could make me happier with the fscript code is if I could write at:'…' instead of valueForKey:'…'. I think the result would match one's intuition for a dictionary lookup. I could always add it as a category to NSDictionary, I guess.
Leave a comment