How would you shorten that piece of code?

Just a quick question.
I have a piece of Code and i dont know how i can shorten that better.
How would you compress that?

IEnumerator ActivatePickUp()
{
	if (Random.Range(0,2) == 0)
	{
		if (shotAmount < 5)
		{
			shotAmount ++;
			yield return new WaitForSeconds(PickUpTime);
			shotAmount --;
		}
		else
		{
			if (fireRate > 0.075)
			{
				fireRate /= 2;
				yield return new WaitForSeconds(PickUpTime);
				fireRate *= 2;
			}
		}
	}
	else
	{
		if (fireRate > 0.075)
		{
			fireRate /= 2;
			yield return new WaitForSeconds(PickUpTime);
			fireRate *= 2;
		}
		else
		{
			if (shotAmount < 5)
			{
				shotAmount ++;
				yield return new WaitForSeconds(PickUpTime);
				shotAmount --;
			}
		}
	}
}

Well, as in literal lines of code, you could use “else if” at a few places…

Like so:

IEnumerator ActivatePickUp()
{
    if (Random.Range(0,2) == 0)
    {
        if (shotAmount < 5)
        {
            shotAmount ++;
            yield return new WaitForSeconds(PickUpTime);
            shotAmount --;
        }
        else if (fireRate > 0.075)
        {
            fireRate /= 2;
            yield return new WaitForSeconds(PickUpTime);
            fireRate *= 2;
        }
    }
    else if (fireRate > 0.075)
    {
        fireRate /= 2;
        yield return new WaitForSeconds(PickUpTime);
        fireRate *= 2;
    }
    else if (shotAmount < 5)
    {
        shotAmount ++;
        yield return new WaitForSeconds(PickUpTime);
        shotAmount --;
    }
}

Normally you could then combine duplicate code into seperate methods and call these methods instead of continuously using the duplicate code. But considering you have just a limited piece of duplicate code and because yield return is a part of your duplicate code (which is annoying as it would mean that any seperate method would require to be a ienumerator aswell). I don’t really see any benefits of doing that as in this use case it would really just make the code less readable…

You mean something like this?

IEnumerator ActivatePickUp()
{
    bool c1 = (fireRate > 0.075);
    if (((Random.Range(0,2) == 0) || !c1)&& (shotAmount < 5))
    {
        shotAmount ++;
        yield return new WaitForSeconds(PickUpTime);
        shotAmount --;
    }
    else if (c1)
    {
        fireRate /= 2;
        yield return new WaitForSeconds(PickUpTime);
        fireRate *= 2;
    }
}

This ended up actually being longer, but the following piece of code has no duplication of logic, that is you have one place where you can change the rules for each condition.

void ActivatePickUp()
{
	if (Random.Range(0,2) == 0)
	{
		if (!tryShotAmount()) tryFireRate();
	}
	else
	{
		if (!tryFireRate()) tryShotAmount();
	}
}

bool tryShotAmount()
{
	if (shotAmount < 5)
	{
		StartCoroutine(ActivateShotAmount());
		return true;
	}
	return false;
}

bool tryFireRate()
{
	if (fireRate > 0.075)
	{
		StartCoroutine(ActivateFireRate());
		return true;
	}
	return false;
}

IEnumerator ActivateShotAmount()
{
	shotAmount ++;
	yield return new WaitForSeconds(PickUpTime);
	shotAmount --;
}

IEnumerator ActivateFireRate()
{
	fireRate /= 2;
	yield return new WaitForSeconds(PickUpTime);
	fireRate *= 2;
}