Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Give me a horror :)

Give me a horror :)

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharptutorialcomtools
18 Posts 12 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Philip Laureano

    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...

    Do you know...LinFu?

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #9

    Now that I was thinking of it, it wasnt really nested! :-O Due to me formatting behavior of always having braces around if / while/ do/ foreach, VS's formatting makes the code look nested :doh: I will keep an eye out for some. I am sure I have a few uglies ;P

    xacc.ide - now with IronScheme support
    IronScheme - 1.0 alpha 2 out now

    1 Reply Last reply
    0
    • P Philip Laureano

      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? :)

      Do you know...LinFu?

      B Offline
      B Offline
      Bevan C Bird
      wrote on last edited by
      #10

      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 )
      		    {
      		    	  // Both Points are in window...
      		        DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, l.p2.x, l.p2.y, A_REPLACE);
      		    }
      		    else if ( (!p1_in) && (!p2_in) )
      		    {
      		
      		        // Both Points are outside the window...
      		    		// Find all Intersections of the line we'll draw with the four window boundaries:
      		    		
      		    		//DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, l.p2.x, l.p2.y, A_REPLACE);
      		   		  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; // intersection point, starts out as the outside point, then is brought in to the boundary line.
      						short int i = 0;
      						
      				    int_pt = l.p1;
      				    while (i < 2)
      				    {
      						    if (dx == 0) // vertical
      						    {
      										if (int_pt.y < W_Min_y)
      										    int_pt.y = W_Min_y;
      										else
      										    int_pt.y = W_Max_y;
      						    }
      						    else if (dy == 0) // Horizontal
      						    {
      										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)
      							    	{
      
      P 1 Reply Last reply
      0
      • B Bevan C Bird

        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 )
        		    {
        		    	  // Both Points are in window...
        		        DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, l.p2.x, l.p2.y, A_REPLACE);
        		    }
        		    else if ( (!p1_in) && (!p2_in) )
        		    {
        		
        		        // Both Points are outside the window...
        		    		// Find all Intersections of the line we'll draw with the four window boundaries:
        		    		
        		    		//DrawFastLine(LCD_MEM, l.p1.x, l.p1.y, l.p2.x, l.p2.y, A_REPLACE);
        		   		  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; // intersection point, starts out as the outside point, then is brought in to the boundary line.
        						short int i = 0;
        						
        				    int_pt = l.p1;
        				    while (i < 2)
        				    {
        						    if (dx == 0) // vertical
        						    {
        										if (int_pt.y < W_Min_y)
        										    int_pt.y = W_Min_y;
        										else
        										    int_pt.y = W_Max_y;
        						    }
        						    else if (dy == 0) // Horizontal
        						    {
        										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)
        							    	{
        
        P Offline
        P Offline
        Philip Laureano
        wrote on last edited by
        #11

        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...

        Do you know...LinFu?

        1 Reply Last reply
        0
        • P Philip Laureano

          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? :)

          Do you know...LinFu?

          M Offline
          M Offline
          MarkB777
          wrote on last edited by
          #12

          Just nest heaps of if/else if/else statements without using brackets where possible, and making terrible use of white space :) Mark.

          1 Reply Last reply
          0
          • P Philip Laureano

            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...

            Do you know...LinFu?

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #13

            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"

            1 Reply Last reply
            0
            • P Philip Laureano

              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? :)

              Do you know...LinFu?

              T Offline
              T Offline
              Tristan Rhodes
              wrote on last edited by
              #14

              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.

              1 Reply Last reply
              0
              • P Philip Laureano

                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? :)

                Do you know...LinFu?

                R Offline
                R Offline
                Ri Qen Sin
                wrote on last edited by
                #15

                For VB.NET, I just use ElseIfs. 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?

                1 Reply Last reply
                0
                • J jhwurmbach

                  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? :-D

                  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"

                  D Offline
                  D Offline
                  Derek Bartram
                  wrote on last edited by
                  #16

                  OI, some of use like GOTO :) (although use sparingly granted)

                  1 Reply Last reply
                  0
                  • P Philip Laureano

                    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? :)

                    Do you know...LinFu?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #17

                    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

                    C 1 Reply Last reply
                    0
                    • L Lost User

                      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

                      C Offline
                      C Offline
                      CTF66
                      wrote on last edited by
                      #18

                      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 Case DoThis() Case DoThat() Case Else //Launch Main Form 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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups