How To Improve My Code?

Updated on October 18, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on October 17, 2018

I’m building an Anime Desktop Mascot program for Windows and need some help with rewriting some code.
This code tracks cursor movement so I can calculate its speed, so I need to track X & Y movement.
Do you see any way I can merge the two blocks? Also, is there a better practice for declaring many variables at once?

 

bool calculateNewSpeedX = true;
bool calculateNewSpeedY = true;
int cursorSpeedTimerX;
int cursorSpeedTimerY;
int initialCursorPosX;
int initialCursorPosY;
int lastCursorPosX;
int lastCursorPosY;
int curCursorPosX;
int curCursorPosY;
 
private void TrackCursorMovement()
{
    curCursorPosX = Cursor.Position.X;
    curCursorPosY = Cursor.Position.Y;
    cursorSpeedTimerX++;
    cursorSpeedTimerY++;
 
    // X Axis
    if (calculateNewSpeedX)
    { 
        calculateNewSpeedX = false;
        initialCursorPosX = curCursorPosX; 
        cursorSpeedTimerX = 1; 
    } 
    else
    {
        if (curCursorPosX != lastCursorPosX)
        {
            lastCursorPosX = curCursorPosX;
        }
        else
        {
            calculateNewSpeedX = true;
        }
    }
    // Y-AXIS
    if (calculateNewSpeedY)
    {
        calculateNewSpeedY = false;
        initialCursorPosY = curCursorPosY;
        cursorSpeedTimerY = 1;
    }
    else
    {
        if (curCursorPosY != lastCursorPosY)
        {
            lastCursorPosY = curCursorPosY;
        }
        else
        {
            calculateNewSpeedY = true;
        }
    }
}
  • Liked by
  • CleverAI
Reply
1 on October 17, 2018

That reminds me of the desktop sheeps many years ago XD

Nevertheless, we had tracked that idea too, to create something like that, but never get the pep to do it.

For your code: Did it work as aspected and you want only a more efficient way or does something not behave as wanted?

 

Devoted
on October 18, 2018

The code works fine, this is refactored code that’s 1/3 the original length. I just wanted to know if there was a method of merging the blocks as I dislike having nearly-identical blocks of code.
 

I honestly doubt there is, but it can’t hurt to ask.

Show more replies
  • Liked by
Reply
Cancel
1 on October 18, 2018

Probably if you create such method and give it the variables as references as follows:

private void TrackCursorMovementExtension(ref bool calculateNewSpeed, ref int initialCursorPos, ref int lastCursorPos, ref int curCursorPos, ref int cursorSpeedTimer)
{
    if (calculateNewSpeed)
    {
        calculateNewSpeed = false;
        initialCursorPos = curCursorPos;
        cursorSpeedTimer = 1;
    }
    else
    {
        if (curCursorPos != lastCursorPos)
        {
            lastCursorPos = curCursorPos;
        }
        else
        {
            calculateNewSpeed = true;
        }
    }
}

Then you can use this in your main method:

private void TrackCursorMovement()
{
    curCursorPosX = Cursor.Position.X;
    curCursorPosY = Cursor.Position.Y;
    cursorSpeedTimerX++;
    cursorSpeedTimerY++;
    TrackCursorMovementExtension(ref calculateNewSpeedX, ref initialCursorPosX, ref  lastCursorPosX, ref curCursorPosX, ref cursorSpeedTimerX)
    TrackCursorMovementExtension(ref calculateNewSpeedY, ref initialCursorPosY, ref  lastCursorPosY, ref curCursorPosY, ref cursorSpeedTimerY)
}

 

I didn’t tested it, but hopefully it works though.

Devoted
on October 18, 2018

Holy shit, this ref keyword is amazing. How the hell haven’t I been using this? It solves so many issues I’ve been having to work around for ages. Thank you so much for this info dude, it’s invaluable!

Show more replies
  • Liked by
Reply
Cancel
0 on October 18, 2018

.

  • Liked by
Reply
Cancel