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. General Programming
  3. C#
  4. Complexity

Complexity

Scheduled Pinned Locked Moved C#
algorithms
8 Posts 4 Posters 0 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.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    Ive create a method that will handle keydown event it has a lot of "if" statment and it say i has a high complexity and its scores 33 is there way to decrease its complexity actually the method is not finish here is the code

    	private static void FlyouCommandEvent(object sender, KeyEventArgs e)
    	{
    		var fd = (FlyoutDialog)sender;
    		if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandAbort);
    		}
    		else if (e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
    		}
    		else if ((e.KeyCode == Keys.I || (e.Alt && e.KeyCode == Keys.I)) && fd.CanExecuteCommand(Helpers.FlyoutCommandIgnore))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandIgnore);
    		}
    		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNo))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandNo);
    		}
    		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNone))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandNone);
    		}
    		else if (e.KeyCode == Keys.Enter && fd.CanExecuteCommand(Helpers.FlyoutCommandOK))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandOK);
    		}
    		else if ((e.KeyCode == Keys.R || (e.Alt && e.KeyCode == Keys.R)) && fd.CanExecuteCommand(Helpers.FlyoutCommandRetry))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandRetry);
    		}
    		else if ((e.KeyCode == Keys.Y || (e.Alt && e.KeyCode == Keys.Y)) && fd.CanExecuteCommand(Helpers.FlyoutCommandYes))
    		{
    			fd.ExecuteCommand(Helpers.FlyoutCommandYes);
    		} 
    	}
    

    any suggestion is highly appreciated thank you

    G P A 3 Replies Last reply
    0
    • G Gilbert Consellado

      Ive create a method that will handle keydown event it has a lot of "if" statment and it say i has a high complexity and its scores 33 is there way to decrease its complexity actually the method is not finish here is the code

      	private static void FlyouCommandEvent(object sender, KeyEventArgs e)
      	{
      		var fd = (FlyoutDialog)sender;
      		if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandAbort);
      		}
      		else if (e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
      		}
      		else if ((e.KeyCode == Keys.I || (e.Alt && e.KeyCode == Keys.I)) && fd.CanExecuteCommand(Helpers.FlyoutCommandIgnore))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandIgnore);
      		}
      		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNo))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandNo);
      		}
      		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNone))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandNone);
      		}
      		else if (e.KeyCode == Keys.Enter && fd.CanExecuteCommand(Helpers.FlyoutCommandOK))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandOK);
      		}
      		else if ((e.KeyCode == Keys.R || (e.Alt && e.KeyCode == Keys.R)) && fd.CanExecuteCommand(Helpers.FlyoutCommandRetry))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandRetry);
      		}
      		else if ((e.KeyCode == Keys.Y || (e.Alt && e.KeyCode == Keys.Y)) && fd.CanExecuteCommand(Helpers.FlyoutCommandYes))
      		{
      			fd.ExecuteCommand(Helpers.FlyoutCommandYes);
      		} 
      	}
      

      any suggestion is highly appreciated thank you

      G Offline
      G Offline
      Godhaniketan
      wrote on last edited by
      #2

      there is switch....catch option is availabel as alternate option of if condition

      G 1 Reply Last reply
      0
      • G Gilbert Consellado

        Ive create a method that will handle keydown event it has a lot of "if" statment and it say i has a high complexity and its scores 33 is there way to decrease its complexity actually the method is not finish here is the code

        	private static void FlyouCommandEvent(object sender, KeyEventArgs e)
        	{
        		var fd = (FlyoutDialog)sender;
        		if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandAbort);
        		}
        		else if (e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
        		}
        		else if ((e.KeyCode == Keys.I || (e.Alt && e.KeyCode == Keys.I)) && fd.CanExecuteCommand(Helpers.FlyoutCommandIgnore))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandIgnore);
        		}
        		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNo))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandNo);
        		}
        		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNone))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandNone);
        		}
        		else if (e.KeyCode == Keys.Enter && fd.CanExecuteCommand(Helpers.FlyoutCommandOK))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandOK);
        		}
        		else if ((e.KeyCode == Keys.R || (e.Alt && e.KeyCode == Keys.R)) && fd.CanExecuteCommand(Helpers.FlyoutCommandRetry))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandRetry);
        		}
        		else if ((e.KeyCode == Keys.Y || (e.Alt && e.KeyCode == Keys.Y)) && fd.CanExecuteCommand(Helpers.FlyoutCommandYes))
        		{
        			fd.ExecuteCommand(Helpers.FlyoutCommandYes);
        		} 
        	}
        

        any suggestion is highly appreciated thank you

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        There's a heck of a lot of code going on here, and it certainly appears as though you're violating SRP here. Rather than doing things like this, I would look to implement separate classes that do what you need to do - each class would then decide whether it was the one that handled the case. There's even a name for this, as a pattern; it's called the Chain Of Responsibility. So, you would end up with something like this:

        public abstract class HandlerBase
        {
        private IHandler successor;
        public void SetSuccessor(IHandler successor)
        {
        this.successor = handler;
        }
        public virtual void HandleRequest(KeyEventArgs e)
        {
        if (CanHandleRequest(e))
        {
        return;
        }
        if (successor != null)
        {
        successor.HandleRequest(e);
        }
        }

        public abstract bool CanHandleRequest(KeyEventArgs e);
        public abstract void HandleRequest(FlyoutDialog fd);
        }

        public class FlyoutAbort : HandlerBase
        {
        public virtual bool CanHandleRequest(KeyEventArgs e)
        {
        return (e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort);
        }

        public abstract void HandleRequest(FlyoutDialog fd)
        {
        fd.ExecuteCommand(Helpers.FlyoutCommandAbort); // In reality, I would look to move the command from Helpers, into this class.
        }
        }

        public class FlyoutCancel : HandlerBase
        {
        public virtual bool CanHandleRequest(KeyEventArgs e)
        {
        return e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel;
        }

        public abstract void HandleRequest(FlyoutDialog fd)
        {
        fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
        }
        }

        Then, in your main class, you simply do this:

        private FlyoutAbort flyoutAbort;
        private FlyoutCancel flyoutCancel;

        private void InitializeChain()
        {
        flyoutAbort = new FlyoutAbort();
        flyoutCancel = new FlyoutCancel();
        flyoutAbort.SetSuccessor(flyoutCancel);
        }

        Finally, all you need to do to handle the request (if it can be handled that is), is call:

        flyoutAbort.HandleRequest(e);

        G 2 Replies Last reply
        0
        • G Godhaniketan

          there is switch....catch option is availabel as alternate option of if condition

          G Offline
          G Offline
          Gilbert Consellado
          wrote on last edited by
          #4

          Thank you so much, but i guess switch cant handle expressions. Correct if I am wrong.

          1 Reply Last reply
          0
          • P Pete OHanlon

            There's a heck of a lot of code going on here, and it certainly appears as though you're violating SRP here. Rather than doing things like this, I would look to implement separate classes that do what you need to do - each class would then decide whether it was the one that handled the case. There's even a name for this, as a pattern; it's called the Chain Of Responsibility. So, you would end up with something like this:

            public abstract class HandlerBase
            {
            private IHandler successor;
            public void SetSuccessor(IHandler successor)
            {
            this.successor = handler;
            }
            public virtual void HandleRequest(KeyEventArgs e)
            {
            if (CanHandleRequest(e))
            {
            return;
            }
            if (successor != null)
            {
            successor.HandleRequest(e);
            }
            }

            public abstract bool CanHandleRequest(KeyEventArgs e);
            public abstract void HandleRequest(FlyoutDialog fd);
            }

            public class FlyoutAbort : HandlerBase
            {
            public virtual bool CanHandleRequest(KeyEventArgs e)
            {
            return (e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort);
            }

            public abstract void HandleRequest(FlyoutDialog fd)
            {
            fd.ExecuteCommand(Helpers.FlyoutCommandAbort); // In reality, I would look to move the command from Helpers, into this class.
            }
            }

            public class FlyoutCancel : HandlerBase
            {
            public virtual bool CanHandleRequest(KeyEventArgs e)
            {
            return e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel;
            }

            public abstract void HandleRequest(FlyoutDialog fd)
            {
            fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
            }
            }

            Then, in your main class, you simply do this:

            private FlyoutAbort flyoutAbort;
            private FlyoutCancel flyoutCancel;

            private void InitializeChain()
            {
            flyoutAbort = new FlyoutAbort();
            flyoutCancel = new FlyoutCancel();
            flyoutAbort.SetSuccessor(flyoutCancel);
            }

            Finally, all you need to do to handle the request (if it can be handled that is), is call:

            flyoutAbort.HandleRequest(e);

            G Offline
            G Offline
            Gilbert Consellado
            wrote on last edited by
            #5

            Nice one, let see what I can do. Thank you so much.

            1 Reply Last reply
            0
            • G Gilbert Consellado

              Ive create a method that will handle keydown event it has a lot of "if" statment and it say i has a high complexity and its scores 33 is there way to decrease its complexity actually the method is not finish here is the code

              	private static void FlyouCommandEvent(object sender, KeyEventArgs e)
              	{
              		var fd = (FlyoutDialog)sender;
              		if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandAbort);
              		}
              		else if (e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
              		}
              		else if ((e.KeyCode == Keys.I || (e.Alt && e.KeyCode == Keys.I)) && fd.CanExecuteCommand(Helpers.FlyoutCommandIgnore))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandIgnore);
              		}
              		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNo))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandNo);
              		}
              		else if ((e.KeyCode == Keys.N || (e.Alt && e.KeyCode == Keys.N)) && fd.CanExecuteCommand(Helpers.FlyoutCommandNone))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandNone);
              		}
              		else if (e.KeyCode == Keys.Enter && fd.CanExecuteCommand(Helpers.FlyoutCommandOK))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandOK);
              		}
              		else if ((e.KeyCode == Keys.R || (e.Alt && e.KeyCode == Keys.R)) && fd.CanExecuteCommand(Helpers.FlyoutCommandRetry))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandRetry);
              		}
              		else if ((e.KeyCode == Keys.Y || (e.Alt && e.KeyCode == Keys.Y)) && fd.CanExecuteCommand(Helpers.FlyoutCommandYes))
              		{
              			fd.ExecuteCommand(Helpers.FlyoutCommandYes);
              		} 
              	}
              

              any suggestion is highly appreciated thank you

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Off topic with regards to your question but is your key testing code correct? I assume the intention of the following code is to allow A or Alt-A

              if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A))

              The first condition does not test for modifier keys so would be true for A with any combination of modifiers keys (including none). Due to shortcut evaluation the second condition will be evaluated only if the first is false, that is if the Key is NOT A and I'm sure you can see the problem with that. Even if it was evaluated the condition does not exclude additional modifiers so Ctrl-Alt-A would be allowed. The solution is to test the key and the modifiers separately. Specifically the modifiers test should be for none or just Alt.

              if ((e.KeyCode == Keys.A) && (e.Modifiers == Keys.None || e.Modifiers == Keys.Alt))

              Hope that helps, Alan.

              G 1 Reply Last reply
              0
              • P Pete OHanlon

                There's a heck of a lot of code going on here, and it certainly appears as though you're violating SRP here. Rather than doing things like this, I would look to implement separate classes that do what you need to do - each class would then decide whether it was the one that handled the case. There's even a name for this, as a pattern; it's called the Chain Of Responsibility. So, you would end up with something like this:

                public abstract class HandlerBase
                {
                private IHandler successor;
                public void SetSuccessor(IHandler successor)
                {
                this.successor = handler;
                }
                public virtual void HandleRequest(KeyEventArgs e)
                {
                if (CanHandleRequest(e))
                {
                return;
                }
                if (successor != null)
                {
                successor.HandleRequest(e);
                }
                }

                public abstract bool CanHandleRequest(KeyEventArgs e);
                public abstract void HandleRequest(FlyoutDialog fd);
                }

                public class FlyoutAbort : HandlerBase
                {
                public virtual bool CanHandleRequest(KeyEventArgs e)
                {
                return (e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort);
                }

                public abstract void HandleRequest(FlyoutDialog fd)
                {
                fd.ExecuteCommand(Helpers.FlyoutCommandAbort); // In reality, I would look to move the command from Helpers, into this class.
                }
                }

                public class FlyoutCancel : HandlerBase
                {
                public virtual bool CanHandleRequest(KeyEventArgs e)
                {
                return e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel;
                }

                public abstract void HandleRequest(FlyoutDialog fd)
                {
                fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
                }
                }

                Then, in your main class, you simply do this:

                private FlyoutAbort flyoutAbort;
                private FlyoutCancel flyoutCancel;

                private void InitializeChain()
                {
                flyoutAbort = new FlyoutAbort();
                flyoutCancel = new FlyoutCancel();
                flyoutAbort.SetSuccessor(flyoutCancel);
                }

                Finally, all you need to do to handle the request (if it can be handled that is), is call:

                flyoutAbort.HandleRequest(e);

                G Offline
                G Offline
                Gilbert Consellado
                wrote on last edited by
                #7

                Its all works now. Thank you so much.

                1 Reply Last reply
                0
                • A Alan N

                  Off topic with regards to your question but is your key testing code correct? I assume the intention of the following code is to allow A or Alt-A

                  if ((e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A))

                  The first condition does not test for modifier keys so would be true for A with any combination of modifiers keys (including none). Due to shortcut evaluation the second condition will be evaluated only if the first is false, that is if the Key is NOT A and I'm sure you can see the problem with that. Even if it was evaluated the condition does not exclude additional modifiers so Ctrl-Alt-A would be allowed. The solution is to test the key and the modifiers separately. Specifically the modifiers test should be for none or just Alt.

                  if ((e.KeyCode == Keys.A) && (e.Modifiers == Keys.None || e.Modifiers == Keys.Alt))

                  Hope that helps, Alan.

                  G Offline
                  G Offline
                  Gilbert Consellado
                  wrote on last edited by
                  #8

                  Oh I didn't realize that. Thank you so much

                  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