Salam All,
This is a c++ programming post/question. So, if you're not a programming geek, please ignore gracefully :-)
I'm looking for advice on choosing a simple & elegant programming style for a certain type of functions. I will first explain the function, and then list several solutions I've seen (and don't totally like so far), and then I'll keep it open for people to post back their suggested answers.
The Function (i want to write)
The function simply has several statements to execute in order. Each statement may return an error code. The statements must be called in order, and if any fails, none of the rest must execute. The function also has initialization & clean up code that must run (whether the statements fail or not).
How to write this function in an elegant readable style that would allow future additions & modifications easily?
Following are several solutions I've seen. However, there's something I do not like about each. My question is... do you know of different method that would avoid any of my dislikings?
Style #1, if-else-if, example
int TheElegantFunction () {
/*** init-code ***/
int rc = 0; //return value. 0 means no error
mutex.get();
/*** the statements s1, s2, & s3 ***/
if (0 != (rc == s1())) {
} else if (0 != (rc == s2())) {
} else if (0 != (rc == s3())) {
} else {
//we were successful
}
/*** clean up ***/
mutex.release();
return rc;
}
advantages:
1- no code repetition
2- one-exit-point for the function
3- compact code (each statement takes 1 line)
disadvantages:
1- not very readable (sequential code does not belong to the if-conditional-expression)
2- what to do with non-error-returning statements, or addign-statements
3- placing those statements in else {} will force creating several levels of nested else { if... }
Style #2, if-error, example
int TheElegantFunction () {
/*** init-code ***/
int rc = 0; //return value. 0 means no error
mutex.get();
/*** the statements s1, s2, & s3 ***/
rc = s1();
if (rc != 0) {
/*** clean up ***/
mutex.release()
return rc;
}
rc = s2();
if (rc != 0) {
/*** clean up ***/
mutex.release()
return rc;
}
rc = s3();
if (rc != 0) {
/*** clean up ***/
mutex.release()
return rc;
}
/*** clean up ***/
mutex.release();
return rc;
}
advantages:
1- readable
2- easy
disadvantages:
1- you have to repeat the clean up code for every single statement
2- multiple exit points for the function - can be confusing to follow the logic
Style #3, goto-solution, example
int TheElegantFunction () {
/*** init-code ***/
int rc = 0; //return value. 0 means no error
mutex.get();
/*** the statements s1, s2, & s3 ***/
rc = s1();
if (rc != 0) goto end;
rc = s2();
if (rc != 0) goto end;
rc = s3();
if (rc != 0) goto end;
end:
/*** clean up ***/
mutex.release();
return rc;
}
advantages:
1- readable
2- one function exit point
3- compact
4- no repetition
disadvantages:
1- the frowned-upoen, spagetti-programming, goto statement!
2- the goto can mask the logic and make the function not very readable - especially if there are loops
Style #4, my-dream-language, example
int TheElegantFunction () {
/*** init-code ***/
int rc = 0; //return value. 0 means no error
mutex.get();
conditional_block (rc == 0 /* exit block when expression doesn't hold */) {
/*** the statements s1, s2, & s3 ***/
rc = s1();
rc = s2();
rc = s3();
}
/*** clean up ***/
mutex.release();
return rc;
}
advantages:
1- (I think) readable & elegant & clear logic-flow
2- no code-repetition
3- one entrance, one exit for the function
disadvantages:
1- imaginary (maybe a nice C# v3.0 feature? hint hint ;-))
so what do you guys think? do you know of a nice style of write such common-functions?
A quest for the Arab Startup Formula - what does it take to create the most successful startups in the Arab Region? I work at EcommerceSea startup in Riyadh, trying to create the Home for Modern Saudi Fashion at Vanilla.sa - a platform for designers, manufacturers, and products that have a global appeal with an Arabic character.
Sep 3, 2005
Aug 30, 2005
What do you listen to while working?
What do you listen to when you work or study?
I usually look for no-lyrics, melow but not boring, complex but not distracting type of music. My favorites:
1- Mystical Legacies by Ali Jihad Racy. This album contains solo pieces by Racy on different Arabic and Middle Eastern instruments. The nay tracks are especially beautiful. Racy is considered the foremost Arabic musician in the US (yes, not Simon Shaheen)
2- Clarinet Trio & Quintet by Brahms. Towards the end of his life, Brahms decided to retire and never compose again, but then he did. And the last few pieces he produced were amazing. While I find early Brahms a bit too "obvious", his late compositions are truly at a different level. Just like with Mozart's Requiem and Beethoven's Ninth (although his earlier pieces are also amazing), for some reason, "maturity" does show up in music. And it's not just music maturity, but it's life-experience reflected on the composition.
So that is me. What do -you- listen to while working or studying?
I usually look for no-lyrics, melow but not boring, complex but not distracting type of music. My favorites:
1- Mystical Legacies by Ali Jihad Racy. This album contains solo pieces by Racy on different Arabic and Middle Eastern instruments. The nay tracks are especially beautiful. Racy is considered the foremost Arabic musician in the US (yes, not Simon Shaheen)
2- Clarinet Trio & Quintet by Brahms. Towards the end of his life, Brahms decided to retire and never compose again, but then he did. And the last few pieces he produced were amazing. While I find early Brahms a bit too "obvious", his late compositions are truly at a different level. Just like with Mozart's Requiem and Beethoven's Ninth (although his earlier pieces are also amazing), for some reason, "maturity" does show up in music. And it's not just music maturity, but it's life-experience reflected on the composition.
So that is me. What do -you- listen to while working or studying?
Aug 27, 2005
Oh sweet California
I go to this small dinky arab shop next to the musala in san jose (California, where I've moved very recently), and
what do I find!? ha, what do I find in this small dinky arab shop!?
In this small dinky arab shop, I find a piece of my lost spirit:
- SABBAR!! (prickly pears), oh, I've been looking for this stuff for so
long! In seattle I used to hunt in China Town every weekend to get it
like once a year.
- Bala7!! (yes, not Tamer, but hard juicy yellow Bala7). I did not have
bala7 since I was in Jordan in 2000. Ah.. the years I've yearned for one.
- real Teen!! and I'm not talking about the plastic green stuff you get
at Safeway. This is nice sweet teen (although I'm still looking for
that Syrian purple teen - big, juicy & self-peeling)
- Khiyar and Kosa!! the real thing, small & tasty and free of those huge
seeds!
- white Seeds (bizir ya3ni, bizir!)
ah.. I know this sounds weird, but mediterranean fruits are hard to find in the US. California is so different. It's so similar to back home. People even have Lemon & Orange trees in their backyards.
(note: I didn't have my camera, so forgive me for using an older picture of Sabbar in Seattle)
what do I find!? ha, what do I find in this small dinky arab shop!?
In this small dinky arab shop, I find a piece of my lost spirit:
- SABBAR!! (prickly pears), oh, I've been looking for this stuff for so
long! In seattle I used to hunt in China Town every weekend to get it
like once a year.
- Bala7!! (yes, not Tamer, but hard juicy yellow Bala7). I did not have
bala7 since I was in Jordan in 2000. Ah.. the years I've yearned for one.
- real Teen!! and I'm not talking about the plastic green stuff you get
at Safeway. This is nice sweet teen (although I'm still looking for
that Syrian purple teen - big, juicy & self-peeling)
- Khiyar and Kosa!! the real thing, small & tasty and free of those huge
seeds!
- white Seeds (bizir ya3ni, bizir!)
ah.. I know this sounds weird, but mediterranean fruits are hard to find in the US. California is so different. It's so similar to back home. People even have Lemon & Orange trees in their backyards.
(note: I didn't have my camera, so forgive me for using an older picture of Sabbar in Seattle)
Aug 25, 2005
Hijab Swim Suits!
Aug 23, 2005
The problem of modern Arabic music
Many accuse modern Arabic songs with extreme degradation.
You hear: Oh, Om Kalthoum's songs are the best.
You hear: All this Amr-Diab, Nansi stuff is nonsense.
But is it?
What makes a good song?
You can spend years researching the question. You can come up with a complicated rating system that gives points depending on the originality of the melody, the meaningfulness of the words, the "dignity" of the singer, etc. But then at the end, you would just create a very complicated system to answer the question "how much do you like this song?"; a question you can answer instantly after listening to it. And, if some other person creates another rating system, most likely his results will differ from yous. Thus, this whole rating system idea does not get us anywhere.
So again to the question, what makes a good song?
It seems that what matters the most, is whether people like the songs or not. Do the people in the Arab world like the modern songs? Just by looking at the concerts, bus stations, radio channels - a resounding yes, people love the music of today.
And this brings us full-circle: why do people accuse modern Arabic songs with extreme degradation, if they actually like them?
I don't know. Do you?
You hear: Oh, Om Kalthoum's songs are the best.
You hear: All this Amr-Diab, Nansi stuff is nonsense.
But is it?
What makes a good song?
You can spend years researching the question. You can come up with a complicated rating system that gives points depending on the originality of the melody, the meaningfulness of the words, the "dignity" of the singer, etc. But then at the end, you would just create a very complicated system to answer the question "how much do you like this song?"; a question you can answer instantly after listening to it. And, if some other person creates another rating system, most likely his results will differ from yous. Thus, this whole rating system idea does not get us anywhere.
So again to the question, what makes a good song?
It seems that what matters the most, is whether people like the songs or not. Do the people in the Arab world like the modern songs? Just by looking at the concerts, bus stations, radio channels - a resounding yes, people love the music of today.
And this brings us full-circle: why do people accuse modern Arabic songs with extreme degradation, if they actually like them?
I don't know. Do you?
Subscribe to:
Posts (Atom)

