|
Isn't if-else if-else if-else itself horrendous? A decent switch-case (C#) or a select-case (VB) should be cool and elegant!
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
Vasudevan Deepak K wrote: Isn't if-else if-else if-else itself horrendous? A decent switch-case (C#) or a select-case (VB) should be cool and elegant!
Hopefully, I can show the neophytes how to get rid of switches and elses altogether.
Personally, I prefer replacing switch statements with polymorphism, and replacing explicit else logic with implicit else logic. It makes the code so much easier to read.
|
|
|
|
|
I'll see what I can dredge up
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
I could give you some code, but you are not going to to be able to refactor it (think (hand)unrolled loop)
|
|
|
|
|
leppie wrote: I could give you some code, but you are not going to to be able to refactor it (think (hand)unrolled loop)
Try me. This should be interesting...
|
|
|
|
|
Now that I was thinking of it, it wasnt really nested!
Due to me formatting behavior of always having braces around if / while/ do/ foreach, VS's formatting makes the code look nested
I will keep an eye out for some. I am sure I have a few uglies
|
|
|
|
|
I've got a couple snippets from what I wrote about a year and a half ago, as a C newbie. Sorry about the formatting.
The first one is a line clipping function for displaying lines on a calculator screen, as part of a rudimentary zoomable vector diagram program.
void Geom_LineOn_inWindow(Geom_Line *lines, unsigned short int n)
{
Geom_Line W_N = {{W_Min_x, W_Min_y}, {W_Max_x, W_Min_y}};
Geom_Line W_E = {{W_Max_x, W_Min_y}, {W_Max_x, W_Max_y}};
Geom_Line W_S = {{W_Min_x, W_Max_y}, {W_Max_x, W_Max_y}};
Geom_Line W_W = {{W_Min_x, W_Min_y}, {W_Min_x, W_Max_y}};
unsigned char p1_in;
unsigned char p2_in;
unsigned short int k = 0;
for (; k < n; ++k)
{
Geom_Line l = lines[k];
p1_in = ( l.p1.x >= W_Min_x && l.p1.x <= W_Max_x && l.p1.y >= W_Min_y && l.p1.y <= W_Max_y );
p2_in = ( l.p2.x >= W_Min_x && l.p2.x <= W_Max_x && l.p2.y >= W_Min_y && l.p2.y <= W_Max_y );
if ( p1_in && p2_in )
{
DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, l.p2.x, l.p2.y, A_REPLACE);
}
else if ( (!p1_in) && (!p2_in) )
{
if (Geom_intersect(l, W_N));
else if (Geom_intersect(l, W_E));
else if (Geom_intersect(l, W_S));
else if (Geom_intersect(l, W_W));
else
goto skipLine;
signed short int dx = l.p2.x - l.p1.x;
signed short int dy = l.p2.y - l.p1.y;
float m;
Geom_Point int_pt, int_pt_other;
short int i = 0;
int_pt = l.p1;
while (i < 2)
{
if (dx == 0)
{
if (int_pt.y < W_Min_y)
int_pt.y = W_Min_y;
else
int_pt.y = W_Max_y;
}
else if (dy == 0)
{
if (int_pt.x < W_Min_x)
int_pt.x = W_Min_x;
else
int_pt.x = W_Max_x;
}
else
{
m = dy / dx;
if (int_pt.x < W_Min_x)
{
int_pt.x = W_Min_x;
int_pt.y = m * (int_pt.x - l.p1.x) + l.p1.y;
}
if (int_pt.x > W_Max_x)
{
int_pt.x = W_Max_x;
int_pt.y = m * (int_pt.x - l.p1.x) + l.p1.y;
}
if (int_pt.y > W_Max_y)
{
int_pt.y = W_Max_y;
int_pt.x = (int_pt.y - l.p1.y + m * l.p1.x) / m;
}
if (int_pt.y < W_Min_y)
{
int_pt.y = W_Min_y;
int_pt.x = (int_pt.y - l.p1.y + m * l.p1.x) / m;
}
}
if (!i)
{
int_pt_other = int_pt;
int_pt = l.p2;
}
++i;
}
DrawFastLine(LCD_MEM, int_pt.x, int_pt.y, int_pt_other.x, int_pt_other.y, A_REPLACE);
skipLine:
}
else
{
signed short int dx = l.p2.x - l.p1.x;
signed short int dy = l.p2.y - l.p1.y;
float m;
Geom_Point int_pt;
if (p1_in)
int_pt = l.p2;
else
int_pt = l.p1;
if (dx == 0)
{
if (int_pt.y < W_Min_y)
int_pt.y = W_Min_y;
else
int_pt.y = W_Max_y;
}
else if (dy == 0)
{
if (int_pt.x < W_Min_x)
int_pt.x = W_Min_x;
else
int_pt.x = W_Max_x;
}
else
{
m = dy / dx;
if (int_pt.x < W_Min_x)
{
int_pt.x = W_Min_x;
int_pt.y = m * (int_pt.x - l.p1.x) + l.p1.y;
}
if (int_pt.x > W_Max_x)
{
int_pt.x = W_Max_x;
int_pt.y = m * (int_pt.x - l.p1.x) + l.p1.y;
}
if (int_pt.y > W_Max_y)
{
int_pt.y = W_Max_y;
int_pt.x = (int_pt.y - l.p1.y + m * l.p1.x) / m;
}
if (int_pt.y < W_Min_y)
{
int_pt.y = W_Min_y;
int_pt.x = (int_pt.y - l.p1.y + m * l.p1.x) / m;
}
}
if (p1_in)
DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, int_pt.x, int_pt.y, A_REPLACE);
else
DrawFastLine(LCD_MEM, l.p2.x, l.p2.y, int_pt.x, int_pt.y, A_REPLACE);
}
}
}
And this snippet is from a function that draws clipped "circles" or arcs:
if (dxmin <= Geom_circle_table[s][0])
if (dxmax >= Geom_circle_table[s][0])
if (dymin <= Geom_circle_table[s][1])
if (dymax >= Geom_circle_table[s][1])
if (pen_down)
{
x = circles->c.x + circles->r*Geom_circle_table[s][0];
y = circles->c.y + circles->r*Geom_circle_table[s][1];
DrawFastLine(LCD_MEM, X, Y, x, y, A_REPLACE);
X = x;
Y = y;
}
else
{
pen_down = 1;
X = circles->c.x + circles->r*Geom_circle_table[s][0];
Y = circles->c.y + circles->r*Geom_circle_table[s][1];
}
else pen_down = 0;
else pen_down = 0;
else pen_down = 0;
else pen_down = 0;
}
if ((circles->s2 <= circles->s1) && first)
{
s = 0;
first = 0;
stop = (circles->s2+1);
goto Arc_draw_2;
}
if (circles->s1 == 0 && circles->s2 == 25)
if (pen_down)
if (dxmin <= Geom_circle_table[circles->s1][0])
if (dxmax >= Geom_circle_table[circles->s1][0])
if (dymin <= Geom_circle_table[circles->s1][1])
if (dymax >= Geom_circle_table[circles->s1][1])
{
x = circles->c.x + circles->r*Geom_circle_table[circles->s1][0];
y = circles->c.y + circles->r*Geom_circle_table[circles->s1][1];
DrawFastLine(LCD_MEM, X, Y, x, y, A_REPLACE);
}
|
|
|
|
|
Ok, this is definitely one I can work with, but do you have any other horror samples in the .NET languages? I still need to be able to compile those samples...
|
|
|
|
|
Just nest heaps of if/else if/else statements without using brackets where possible, and making terrible use of white space
Mark.
|
|
|
|
|
Well, if i still worked in my last place, i could provide you with TONS of sample material. However, not to inundate you, i would give you the gem of the entire system. A single 750 line PageLoad function that was 12 nested If / Case statements deep at places.
Unfortunately, i don't have it
Still, it should keep you occupied for a month or five.
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
For VB.NET, I just use ElseIf s. I haven't found myself in a situation where I had to use a lot of nested If statements. If I do, I end up moving that code to a new method anyway.
So the creationist says: Everything must have a designer. God designed everything.
I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?
|
|
|
|
|
Heres a good solution:
do{
if(not some state)
break;
if(not some other state)
break;
... etc...
}while(FALSE)
Morality is indistinguishable from social proscription
|
|
|
|
|
I too use the 'do', also Back in my VB6 days, I would often (amongst other things) start an app up using a Select.
Select Case False<br />
Case DoThis()<br />
Case DoThat()<br />
Case Else<br />
End Select
Each method is called in turn, the first method that fails (returns false) aborts the startup.
Much cleaner than endless if-else's, and although it's probably not correct to compare Select and switch, switch is no match.
modified on Thursday, April 3, 2008 3:58 AM
|
|
|
|
|
My colleague wrote code for calculate total price and then save it he use session for sending total price to another Sub.This session use in Cal() and Save() only.
Private Sub Cal()
dim dblTotalPrice as Double
dblTotalPrice= cint(me.txtQty.text) * cdbl(me.txtPricePerUnit.Text)
session("TotalPrice") = dblTotalPrice
End Sub
Private Sub Save()
dim dblTotalPrice as Double
dblTotalPrice = session("TotalPrice")
End Sub
modified on Thursday, March 13, 2008 5:42 AM
|
|
|
|
|
Looks like useless code, delete it. See if he notices, when he does, tell him not write code that has no purpose.
|
|
|
|
|
Krirk wrote: session("TotalPrice")
But are you sure he is not using it in a different context within the application like a different period or time.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
Useless looking code. What was your colleague thinking?
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
Paul Conrad wrote: What was your colleague thinking?
"Almost weekend..."
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
dnh wrote: "Almost weekend..."
Yes, or...
"Hmmmm, 4:45 Friday, I'll delete the dead code Monday" ( and gets sidetracked with a Monday morning meeting )
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
Paul Conrad wrote: thinking
Can he? He should have been in an inebriated state right?
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
Quite possible...
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
hey would u please send me the source code of peer to peer internet caching.as iam an engg student and iam doing a project on that re.so please send me the source code of it ok.send to my id
rameez_313 @yahoo.com
|
|
|
|
|
I think you have incorrect place to post.
|
|
|
|
|
First of all, hello. I'm a newbie. Since this section is called Coding Horrors, I thought it would be funny to share this C++ header file I wrote a few days ago:
http://www.geocities.com/fruitcake59/Prototype.txt[^]
The following tokens are actually macros: name , type , state , stype , parent , ptype , base , nodeclare , notserial . The two first ones (name and type ) MUST be defined before including Prototype.h. The other ones are optional.
If you see something like this...
<q style="color: black"><q style="color: blue">#ifdef</q> SOME_TOKEN<br />
SOME_TOKEN<br />
<q style="color: blue">#undef</q> SOME_TOKEN<br />
<q style="color: blue">#endif</q></q>
... then SOME_TOKEN is also an optional macro that can be defined before including Prototype.h.
Here's an example of how to use this header file:
<q style="color: black"><q style="color: green">
<q style="color: blue">#pragma once</q><br />
<br />
<q style="color: blue">#pragma region</q> Class Names<br />
<br />
<q style="color: blue">#define</q> name CInstruction<br />
<q style="color: blue">#define</q> type INSTRUCTION<br />
<q style="color: blue">#define</q> state CInstrState<br />
<q style="color: blue">#define</q> stype INSTRSTATE<br />
<q style="color: blue">#define</q> parent mpParent<br />
<q style="color: blue">#define</q> ptype CModule<br />
<q style="color: blue">#define</q> nodeclare<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> Friendships<br />
<br />
<q style="color: blue">#define</q> CONST_FRIENDSHIPS \<br />
<q style="color: blue">friend class</q> CModule; \<br />
<q style="color: blue">friend struct</q> MODULE;<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> Data Structures<br />
<br />
<q style="color: blue">#define</q> VAR_ATTRIBUTES \<br />
CStringList slParams;<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> Constructor<br />
<br />
<q style="color: blue">#define</q> CONSTRUCTOR \<br />
CInstruction(CModule *mpParent); \<br />
CInstruction(CModule *mpParent, CString &sName);<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> Object Operations<br />
<br />
<q style="color: green">
<q style="color: blue">#define</q> CONST_GETTERS \<br />
GET_COUNT() \<br />
GET_CHILD(GetParameter, GetParamSt, CString, )<br />
<br />
<q style="color: blue">#define</q> CONST_DOERS \<br />
<q style="color: blue">void</q> EditParameter(INT_PTR i, LPCTSTR s);<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> State Operations<br />
<br />
<q style="color: green">
<q style="color: blue">#define</q> VAR_GETTERS \<br />
GET_CHILD_COUNT(slParams) \<br />
GET_CHILD_STATE(GetParamSt, CString &, slParams)<br />
<br />
<q style="color: green">
<q style="color: blue">#define</q> VAR_DOERS \<br />
ADD_CHILD_STATE(AddParamSt, LPCTSTR, slParams) \<br />
EDIT_CHILD_STATE(EditParamSt, LPCTSTR, slParams) \<br />
DELETE_CHILD_STATE(DeleteParamSt, slParams)<br />
<br />
<q style="color: green">
<q style="color: blue">#define</q> COPY_STATE \<br />
COPY_LIST(slParams)<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#pragma region</q> Private Tools<br />
<br />
<q style="color: blue">#define</q> CONST_TOOLS \<br />
<q style="color: blue">void</q> AfterParamUpdate();<br />
<br />
<q style="color: blue">#pragma endregion</q><br />
<br />
<q style="color: blue">#include</q> <q style="color: red">"Prototype.h"</q></q>
|
|
|
|
|
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|