You are viewing phreakhead

Previous Entry | Next Entry

Powerbass
I've been wanting this in a programming language for a long time: an if...else...finally block. Basically the rule is this: you have a normal if...else block, but you can put a "finally" condition anywhere you want after the first "if". Any "finally" block appearing (there can be more than one) is executed iff one of the conditional branches above it was followed, within the scope of the followed branch. Execution then continues down the chain. This is a useful shortcut when you have very similar code that is executed when a few conditions are met, but not all of them.

Here's a simple example:

if (a) {
	option = a.getOption();
	normalizeOption(option);
	prepareOption(option);
	processOption(option);
	renderOption(option);
}
elseif (b) {
	option = b.getOption();
	prepareOption(option);
	processOption(option);
	renderOption(option);
}
else {
	option = null;
}


could be written as:

if (a) {
	option = a.getOption();
	normalizeOption(option);
}
elseif (b) {
	option = b.getOption();
}
finally {
	prepareOption(option);
	processOption(option);
	renderOption(option);
}
else {
	option = null;
}


This would come in handy so often that I almost want to modify some open-source compiler to recognize these constructs. I've come across better examples but I can't think of them right now. Try coming up with some of your own.

Comments

( 4 comments — Leave a comment )
flyagaric
Feb. 13th, 2009 09:26 pm (UTC)
Dude, I can't tell you how many times I've wanted something like that! It would be soooo useful, and could significantly reduce the complexity of some code. Would be possible to implement using the c preprocessor, although it might take some work to make it happen.

Brilliant!
frozenbears
Feb. 19th, 2009 12:06 pm (UTC)
if it were me I'd probably just check option against null and then throw those redundancies in its scope. but this is really nice syntactic sugar.

it actually makes me wonder how many other things like this one could devise. one of the more malleable languages like LISP/Scheme or Lua would make a good test bed.



phreakhead
Feb. 19th, 2009 06:23 pm (UTC)
Well that was just a really simple example. I just came across the perfect opportunity yesterday when I was programming something in ColdFusion that writes a list of artists with an artist button, or just an artist button when there is only one artist, or nothing if there are no artists, but if there was a button then I had to put a "Media" button after it if there was media. Here's what I had to write:

<cfif ArrayLen(artists) GT 0>
	<cfset artistURL = "artist.cfm?key=344&artistmedia=#URL.artistmedia#&object=#subkey#&subkey=">
	<cfif ArrayLen(artists) GT 1>
		<a href="#artistURL##sbSubPageContent['constituent_1']#"><img src="images/object_artist.gif"></a>
		<div id="artistLinks" style='display:none'>
			<div class='endButton'> </div>
			<div class='midButton'>
				<cfloop index="x" from="1" to="#ArrayLen(artists)#">
					<a href="#artistURL##artists[x]['sb_key']#">#artists[x]['display_name']#</a><br />
				</cfloop>
			</div>
			<div class='endButton'> </div>	
		</div>
	<cfelse>
		<a href="#artistURL##sbSubPageContent['constituent_1']#"><img src="images/object_artist.gif"></a>
	</cfif>	
	<cfif URL.artistmedia NEQ 0>
		<img src="images/spacer.gif" width="2" height="2" alt="" /><br /><a href="media.cfm?key=372&artistmedia=#URL.artistmedia#&subkey=#URL.artistmedia#&artist=#sbSubPageContent["constituent_1"]#&ob=#subkey#"><img src="images/icon_media.gif"></a>
	</cfif>
<cfelse>
	<img src="images/spacer.gif" width="120" height="20" alt="" />
</cfif>


If I had the "finally" construct, I could have done this:

<cfset artistURL = "artist.cfm?key=344&artistmedia=#URL.artistmedia#&object=#subkey#&subkey=">	
<cfif ArrayLen(artists) GT 1>
	<a href="##" onclick="$('artistLinks').show();return false;"><img src="images/object_artists.gif"></a>
	<div id="artistLinks" style='display:none'>
		<div class='endButton'> </div>
		<div class='midButton'>
			<cfloop index="x" from="1" to="#ArrayLen(artists)#">
				<a href="#artistURL##artists[x]['sb_key']#">#artists[x]['display_name']#</a><br />
			</cfloop>
		</div>
		<div class='endButton'> </div>	
	</div>
<cfelseif ArrayLen(artists) GT 0>>
	<a href="#artistURL##sbSubPageContent['constituent_1']#"><img src="images/object_artist.gif"></a>
<cffinally>
	<cfif URL.artistmedia NEQ 0>
		<img src="images/spacer.gif" width="2" height="2" alt="" /><br /><a href="media.cfm?key=372&artistmedia=#URL.artistmedia#&subkey=#URL.artistmedia#&artist=#sbSubPageContent["constituent_1"]#&ob=#subkey#"><img src="images/icon_media.gif"></a>
	</cfif>
<cfelse>
	<img src="images/spacer.gif" width="120" height="20" alt="" />
</cfif>


I guess this is also a simple example but it would make some things way easier in certain situations.

I'm not sure about Lua, but I can't think of a way that it would work in Lisp, since Lisp "if..else" constructs are just a function called "cond":

> (setq a 3)
3
> (cond
   ((evenp a) a)        ;if a is even return a
   ((> a 7) (/ a 2))    ;else if a is bigger than 7 return a/2
   ((< a 5) (- a 1))    ;else if a is smaller than 5 return a-1
   (t 17))              ;else return 17
2
frozenbears
Feb. 19th, 2009 10:18 pm (UTC)
Ah, I get it. That's a greatly simplified construct.

I was figuring that it might be doable using Lisp macros (or something like that), since a lot of what people use them for is to create domain-specific language extensions or get around redundancies that can't otherwise be avoided. Of course, you'd have to use Lisp though.

I totally support your hacking of major language toolkits.
( 4 comments — Leave a comment )

Latest Month

August 2009
S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
Powered by LiveJournal.com
Designed by Tiffany Chow