|
|
Personally, I try to avoid putting statements in my conditionals...
if (expression is true) then...
if (expression is false) then...
are both acceptable though.
|
|
|
|
|
Which leaves you with:
<br />
if()<br />
;<br />
else()<br />
;
Saves some typing, but I would personally tire of it after a bit. You might want to, at least, use simple statements in your conditionals.
Just so that they actually (1) make up their mind about something, and (2) do something after they decide.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
No, according to most languages (Ruby and functional languages being notable exceptions) an expression is something that returns a value - a boolean expression, for example, returns true of false. While a statement is almost anything: a declaration, an if statement, a while statement.
Placing such a beast within an if's conditional expression yields...
if (if (x==3) y = 4; ) y = 5;
Which really doesn't make much sense.
|
|
|
|
|
I was taught and have taught that your code should test for the most likely condition first, thus avoiding unneccessary excution of code. If the False state is the more likely, then test for false and avoid testing for True - use the else clause for that. The case should be never used to test for just true/false. Speaking of Case, test in descending order of expected frequency of occurance.
Been doin' this for too long!
|
|
|
|
|
|
if always tests for true , never false .
Whether I do if ( x == 0 ) or if ( x != 0 ) I'm still testing for true.
|
|
|
|
|
|
PIEBALDconsult wrote: if always tests for true
But the survey doesn't ask how if statement works. it asks how do you use it:
In which order do you test conditionals?
"In the end it's a little boy expressing himself." Yanni
|
|
|
|
|
Quite right Sir.
Regards,
Rob Philpott.
|
|
|
|
|
My point is that (some of) the provided answers make no sense and I can therefore not give a meaningful answer.
|
|
|
|
|
PIEBALDconsult wrote: My point is that (some of) the provided answers make no sense and I can therefore not give a meaningful answer.
Your point is understood and my point is that if someone is clever enough to understand that even not in a conditional statement is compared to true, should easily understand the point of this survey question which is, again, what you use.
Actually this if will probably be translated to some jumps after some cmp instructions which in fact are not compared to neither true nor false but to 1 or 0.
Also one might say that cmp is not an instruction; it's nothing more than a number in binary form or even not that but some high or low voltages waiting for being processed in next CPU cycle. Oh wait they're not waiting there just a flipflop is... The survey question isn't talking about any of these. It's what you type in your programming editor. Based on the question I believe choices are relevant and make sense.
Just my 2 cents.
"In the end it's a little boy expressing himself." Yanni
|
|
|
|
|
PIEBALDconsult wrote: if ( x != 0 ) I'm still testing for true.
You're still testing for false. It'll catch if the false condition is true, but you're testing for false with that.
|
|
|
|
|
x could be a count or something.
|
|
|
|
|
PIEBALDconsult wrote: x could be a count or something.
Um, what? You're still testing for a false condition. The does not equal part is your false test. The value of X is really irrelevant in the scope of your point.
|
|
|
|
|
Me: Say prithy, be there no holes in my flagon?
He: Aye, sir, there be no holes in thy flagon.
|
|
|
|
|
always check for true first is the best way to code (example)
if true then (do something)
if false then( do something else)
has been my way of coding in that order
so really the question is misleading.
|
|
|
|
|
Byte Magazine (semi-lost and lamented) once carried an article by John McCarthy (sic?) on Logic Gem. He showed how the production of if then elses could be done with a logic table and speed coding and reduce errors. Logic Gem is still available. I wouldn't code complex flow of control without it. Generates code in readable languages and c varieties too.
I have no commercial or personal relationship with Logic Gem, Byte or John McCarthy. And, my opinions are my personal ones.
|
|
|
|
|
Depending on the problem at hand, I almost always opt for switch..case over multiple if..else statements. General rule of thumb in my style of coding is that if there's 3 or more conditions to check for then switch...case is the way to go. Makes code much easier to read as well.
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson Because programming is an art, not a science. Marc Clifton I gave up when I couldn't spell "egg". Justine Allen
|
|
|
|
|
Logic Gem even has an option to generate case structures from the logic table. Inclusion of the term Gem in its name is no exaggeration.
|
|
|
|
|
If only all languages had CASE statements... *sigh*
|
|
|
|
|
Depends a lot upon the language's implementation of switch().
If the condition depends upon a string comparison, than you can use the switch with C# but not with C or C++;
Thereupon, I become most perplexed: do I change this particular style with language?
Simplcity vs. consistancy.
Further purplexation: does it matter - for how often would I convert interconvert C++ and C# ?
Now see what you've done to me. I'm afraid to go back to my code and check to see what I've done.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
Balboos wrote: purplexation
That's a brilliant word
Balboos wrote: Further purplexation: does it matter - for how often would I convert interconvert C++ and C# ?
Personally I don't think it matters. Chances are when whatever it is you're working on needs C or C++ then you will probably find all your old nuances from the language fill your code, same as it is when you write in C#.
I see programming as an expression in itself, and much like spoken languages each language has its own idiosyncrasies. Does that mean then, that coding style is a dialect of the language?
Balboos wrote: Now see what you've done to me. I'm afraid to go back to my code and check to see what I've done.
More than happy to bake your noodle.
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson Because programming is an art, not a science. Marc Clifton I gave up when I couldn't spell "egg". Justine Allen
|
|
|
|
|
Dave Sexton wrote: Balboos wrote:
purplexation
That's a brilliant word
It has special importance, today, for the Shrub will soon become no-longer-president.
This mangling of English is to commemeorate the event.
Oh, by the way - it's never to late to Send "W" a bag of pretzels, sit back, and hope.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
Balboos wrote: Oh, by the way - it's never to late to Send "W" a bag of pretzels, sit back, and hope.
The W stands for Honesty.
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson Because programming is an art, not a science. Marc Clifton I gave up when I couldn't spell "egg". Justine Allen
|
|
|
|