|
If you use constructs like this below (or with more if else) there are much combinations to test.
It would give a bad software metric if you use a test tool. We have to use test tools for our
tests of space applications.
But the way the guy wrote his code is more bad. A construct like below written in his if free
style would be unreadable for someone else and I bet the performance would be very bad too.
if ( expression1) {
if ( expression2) {
if ( expression3) {
...
} else {
...
}
} else {
if ( expression4) {
...
} else {
...
}
}
}
else {
if ( expression5) {
if ( expression6) {
...
} else {
...
}
} else {
if ( expression7) {
...
} else {
...
}
}
}
|
|
|
|
|
I've never encountered a business case that needed that many branches for a single set of conditions. I have encountered "cute" programmers who thought that unnecessary complexity made sense and did things that didn't make sense to me. (Throwing unrelated logic together in one big if-else if- cluster.) I'm also wondering if branching might help both the readability and maintainability of the logic and if that supercedes efficiency if it meets the business logic?
IE
if ( expression1) Condition1();
else NotCondition1();
...
void Condition1()
{
if ( expression2) Condition2();
else NotCondition2();
}
void NotCondition1()
{
if ( expression5) Condition3();
else NotCondition3();
}
void Condition2()
{
if ( expression3) {
...
} else {
...
}
}
etc. I'm not all that convinced that branching to routines won't be performant as well as more readable.
I just recently read about a bubble sort routine. OK, old news + inexperienced developer = time to move on. One of the respondents liked how easy it was to read and how efficient it was. Man, I can't let that misinformation go. I explained how it rated as N^2 efficiency. (IE double the number of fields will quadruple the time to compute.) 200K records sorted in over 2 minutes. (The posted program sorted 11 hardcoded values in an int[] array. Of course that would be fast.) I calculated a million numbers would sort in about 56 minutes. I ran 50 million numbers through a binary sort process in just over 14 seconds. The time consistently increased by a second for just under 3 million numbers being added.
A LOT of if tests being done in recursive routine calls. Slightly slower than List<int>'s built-in Sort routine, List blows up faster with memory problems as the numbers increase. (Can't do 20 million int[] and 20 million List<int>)
|
|
|
|
|
CP wrote: The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices
Congratulations, your contribution was spot-on!
|
|
|
|
|
If you did that to my codebase I'd probably avoid working with you as much as possible. You can always throw hardware at code performance. You can't throw hardware at code readability.
|
|
|
|
|
Indeed. Thank you. As I was reading that post I wondered why the editors would choose to put that out on the daily email.
|
|
|
|
|
That's a bit harsh isn't it? Used in the right place, a trick like this could shave days off a long simulation run on a research cluster.
Finding the right place to use something like this is always the hard part.
|
|
|
|
|
Oh my gosh! A program with no if-else statements! Please don't make me learn assembly! Oh, wait - I already did...
|
|
|
|
|
Yeah, there's some optimization for ya. Instead of typing a nice little two letter word like if , now you have to remember a ton of 3 letter "if" statements that all being with "J".
|
|
|
|
|
What? do i need to write 4 lines to avoid an If?, I have better things to do...
|
|
|
|
|
SarK0Y wrote: Here, i'd like to discuss how to reduce conditional branches in the code. 1st of
the all, i would like to share some tricks.
I would suggest that you learn what automated profiling is. Then buy or find a tool that does that. Then learn how to use it effectively.
|
|
|
|
|
I'm definitely a beginner but I have a question:
Doesn't the program end up doing an if/then regardless of whether or not you're removing the actual syntax? There still needs to be a comparison done somewhere, or at least I thought.
|
|
|
|
|
The old IBM 360 JCL didn't have any if statements.
In PERL, you'd normally see something like
open ("fred") || die
which translates to
if (not open("fred")) then die
If you look at the assembler, in both cases, it is exactly the same.
|
|
|
|
|
We had such discussion in the eighties when programmers wanted to be artists, writing unreadable code just to be irreplaceable.
If you would be one of my programmers I would fire you immediately for this waste of money and time and delivering error-prone code. I thought those times were gone!
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
heh, i wasn't here for awhile actually, ye're wrong on that: readable codes are good, only if you have no the needs of deep optimizations. deep optimization means vast asming.
P.S.
for fast sorting of floats, my if-reduced code http://sourceforge.net/projects/fsort--no-if/?source=navbar
|
|
|
|
|
SarK0Y wrote: readable codes are good, only if you have no the needs of deep optimizations. deep optimization means vast asming.
Code can be both: Optimized and readable. It's just depends on the plannings. Cryptic code isn't any faster than good, optimized readable code. It's just harder to understand and correct. That's all.
|
|
|
|
|
Ingo, did you write asm codes ever???
|
|
|
|
|
SarK0Y wrote: Ingo, did you write asm codes ever???
Okay. I talked about higher languages. If you write asm codes, I won't mind if you code another way.
|
|
|
|
|
Without a doubt, that's the worst idea I've seen in a long time. Instead of using a nice, easy to read if , you've turned it into a very-expensive-to-support-and-debug piece of crap that you'd have to throw unit test after unit test at with a crap-ton of edges cases. Congratulations!
Now go back and wipe that smiley face off your post before I smack it off.
|
|
|
|
|
A true gem...
If CurrentContext = "D" Then GoTo c45
GetUserDataRecord aForm
c45:
There were no other ref's to c45, so replaced by...
If CurrentContext <> "D" Then GetUserRecord aForm
Further down in same procedure:
GoSub ResolveTabOrder
Exit Function
ResolveTabOrder:
If uControlNbr < 2 Then Return
GoTo ResolveTabOrder
End Function
modified 7-Feb-13 7:54am.
|
|
|
|
|
Now this is what happens if you Google "coding" in the "Fill in the blanks" forum
|
|
|
|
|
Just came across a [view name].[column name] combination in some SQL code I'm working with that is that many characters long. Actually, if it included the square brackets, it'd be 81 characters.
Here it is (characters changed to protect the guilty):
vw_AaaaAaaaaaaa_BbbbBbbbBBB_vw_CcccCcccCcccc.AaaaAaaaaaaaDdddddDddDddddddDddd
|
|
|
|
|
Quite long. Suppose this is an unique name...
|
|
|
|
|
I'm sure I already read this a few weeks ago... Did you get stuck in a loop? Codebase day...?
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
|
You know, you may be right. I did have a feeling of deja vu when I posted it.
|
|
|
|
|
Maybe this is why it seems familiar. That must be the answer.
|
|
|
|