|
peterchen wrote: inferiority complex
not being fluent in VB.NET does that to people; I'm just a C# guy.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: not being fluent in VB.NET does that to people;
I'm not fluent, and that doesn't make me feel inferior.
Conclusion? I'm not people.
Wow, that was easy
In case you missed it, I meant the language (not you) expresses its inferiority complex with "me is nothing"
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
OK, so you'd not want to assign a value to an object that was null, but it brings up an interesting discussion on the use of "if (this != null)".
Take a look at this discussion on MSN[^] about this != null in C#.
cheers,
Chris Maunder
CodeProject.com : C++ MVP
|
|
|
|
|
|
if (this == null || .....
{
// assigned values to self
}
You can get into the true part there, you just have to do a bit of IL hacking.
Since instance methods are like static methods with a "this" param as the first arg in il.
you can invoke such method and pass null to "this"
Im not saying you should do this, but its possible
I happened to get that problem when I was doing the subclass proxies for NAspect (our AOP framework)
It works as long as you don't access any member variables :-P
|
|
|
|
|
Hi Guys,
I'm going to write an article for beginners on how to refactor deeply nested "if" statements, and I was wondering if anyone here had any VB.NET or C# code that they could share that had a few "if" statements at least five levels deep that I could use as a refactoring example...do I have any takers?
|
|
|
|
|
Philip Laureano wrote: article for beginners on how to refactor deeply nested "if" statements
You won't endorse the use of multiple return in one function, no?
And surly not even name the dreaded word GOTO ?
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
jhwurmbach wrote: You won't endorse the use of multiple return in one function, no?
And surly not even name the dreaded word GOTO?
I'd never endorse using GOTOs, but I'd definitely endorse using multiple returns from a single function if it's easier to read. If you look at some of the code that I've done with LinFu, you'll notice that most of my "if" statements don't go over two levels deep, and you'll never find a single switch statement inside LinFu. My goal is to help people write better code, and hopefully the article I'll write will be able to do that.
You never know if the code you teach someone how to write will be the code you have to maintain someday...
|
|
|
|
|
Philip Laureano wrote: I'd definitely endorse using multiple returns from a single function
Myself, I DO multiple return, but I definitely try to limit them.
I mostly use them in the first lines of a function, while validating the input.
But after that, I most often use nested if or even a bool marker.
There are quite a few company coding style guides disallowing (or strongly discouraging) multiple returns.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
OI, some of use like GOTO (although use sparingly granted)
|
|
|
|
|
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.
|
|
|
|
|