|
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
|
|
|
|
|
Dave Sexton wrote: The W stands for Honesty.
Well - That must be why he spent so much time sitting Oval Office
(At least when he wasn't on vacation).
Then, too, I saw his last press conference - and I'm not so sure I believe the standing part, either.*
We really enter into a very philosophical domain:
Can an idiot (or even a mere moron) be deemed honest or dishonest?
* He considers his biggest error in Iraq to be the 'Mission Accomplished' sign.
Clearly, he's either an idiot . . . or a liar.
|
|
|
|
|
Nearing soapbox territory are we and I'm waiting to get blasted by an unhappy republican.
I have a friend who has put forward the notion that he is not an idiot but rather "acting the country fella" to mask a true agenda. I disagree and am more inclined to believe in idiocy.
Balboos wrote: Can an idiot (or even a mere moron) be deemed honest or dishonest?
Hmmm... interesting question. I would imagine that one would first need to establish if said moron is mentally capable of understanding what the terms honest and dishonest mean. If such capacity is there then the branding is fair.
Let's just hope the one coming in isn't as bad as the one going out. Frankly, I don't think you could get much worse. Wasn't the last democrat president Bill Clinton?
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
|
|
|
|
|
As the poll suggests, there is a philosophy behind the simple task of using conditionals.
For one, I try to avoid too many if(a == null) return; statements, since it makes the code less readable.
I am wondering what others opinion is on this.
|
|
|
|
|
Nah, I mean constructions like this:
foreach(Item a in collection)
{
if(a.b == null)
break;
if(a.b is SomeOtherClass) {
if(a.b.c != "Something")
return;
}
}
A complex foreach loop with preferrably more than 20 lines of code inside of it. Aside from the fact that I would like to extract some methods from inside the loop. I think its quite nasty to see that many returns and breaks.
I totally understand the fact that people use if(a == null) return; statements at the start of the method. It's actually quite easy to see what the method expects. I do like theif(a == null) throw new ArgumentNullException("a"); construction better.
|
|
|
|
|
|
If do similar except I invert the condition
if(myObject != null)
{
}
else
{
}
That way I have logic focused at the top and null handling below it.
Either way works. Just prefer this one
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
|
|
|
|
|
If the negative outcome is shorter, I put it at the top.
if(checkIfSomethingIsWrongHere)
{
return null;
}
else
{
}
If the error routine is in the same method and rather long, put it second
if(haveWeSucceeded)
{
}
else
{
}
Cheers,
Vıkram.
I don't suffer from insanity, I enjoy every moment of it.
|
|
|
|
|
If Not(isMisspelled("seamingly") then
quit
else
correctSpelling("seamingly", "seemingly")
end if
JackMcG
|
|
|
|
|
JackMcG wrote: If Not(isMisspelled("seamingly") then
Syntax error!
Gary
|
|
|
|
|
The rule of thumb I usually use is to place the most frequently encountered condition first. A professor in college taught me this - it can actually make the program more efficient since that reduces the number of conditions that need to be checked. Granted, if the statement only has a single IF with an unconditioned ELSE, this doesn't make any difference, but if you are using an ELSEIF or CASE construct, it can make a huge difference.
Then again, I went to college way back when saving a few CPU cycles actually mattered... 
|
|
|
|
|
Pretty much agree with that thinking for CASE constructs of putting the most likely. Although I do break it fairly often too. For instance, I usually assume that someone else will get their hands dirty in my code sometime on the future, so I may use a more 'obvious' ordering to the conditions for the benefit of any poor sucker that comes behind.
However, when it comes to XSLT choose blocks I've often found it necessary to put the weird edge cases first and then proceed to the more commonly expected ones later. In other words filter out the strange stuff that requires special handling before you get to what you're really after. Unfortunately this is more CPU intensive, but then again that's XSLT.
I just love Koalas - they go great with Bacon.
|
|
|
|
|
In cases where you cannot tell which condition will be taken more often I put the smaller clause first in the hopes that a false prediction won't necessarily cause as much cache pollution.
|
|
|
|
|
That is what I do. I wonder if this is what most of 69% "depends on the situation" category do? I see the need to re do this poll..
John
|
|
|
|