Calculations to reposition a logo I have this block of code which clearly looks like duplicate, but I don't know how to refactor.

    float offsetDifference;
            nfloat newLogoHeight;
            nfloat newHeaderHeight;
            nfloat newLogoWidth;

            if (offset &gt; _previousOffset)
            {
                offsetDifference = offset - _previousOffset;

                newHeaderHeight = (HeaderViewHeightConstraint.Constant - offsetDifference);
                newLogoHeight = (LogoImageViewHeightConstraint.Constant - offsetDifference);
                newLogoWidth = (LogoImageViewWidthConstraint.Constant - offsetDifference);


                HeaderViewHeightConstraint.Constant = newHeaderHeight &lt; headerViewMinHeight ? headerViewMinHeight : newHeaderHeight;
                LogoImageViewHeightConstraint.Constant = newLogoHeight &lt; logoViewMinHeight ? logoViewMinHeight : newLogoHeight;
                LogoImageViewWidthConstraint.Constant = newLogoWidth &lt; logoViewMinWidth ? logoViewMinWidth : newLogoWidth;
            }
            else
            {
                offsetDifference = _previousOffset - offset;

                newHeaderHeight = (HeaderViewHeightConstraint.Constant + offsetDifference);
                newLogoHeight = (LogoImageViewHeightConstraint.Constant + offsetDifference);
                newLogoWidth = (LogoImageViewWidthConstraint.Constant + offsetDifference);

                HeaderViewHeightConstraint.Constant = newHeaderHeight &gt; headerViewMaxHeight ? headerViewMaxHeight : newHeaderHeight;
                LogoImageViewHeightConstraint.Constant = newLogoHeight &gt; logoViewMaxHeight ? logoViewMaxHeight : newLogoHeight;
                LogoImageViewWidthConstraint.Constant = newLogoWidth &gt; logoViewMaxWidth ? logoViewMaxWidth : newLogoWidth;
            }

            //HeaderViewHeightConstraint.Constant = (headerViewMaxHeight - offset) &lt; logoViewMaxHeight ? logoViewMaxHeight : (headerViewMaxHeight - offset);
            //LogoImageViewHeightConstraint.Constant = (logoViewMaxHeight - offset) &lt; 0 ? 0 : (logoViewMaxHeight - offset);
            _previousOffset = offset;


Basically, I receive an offset, and according to the previous offset (wether is smaller or bigger), I reorganize some other values.

The thing is, the calculation is almost the same, and I'd love a method that just does the calculation in which I can pass the parameters, but I don't know how to write the "common code" since all the calculations varies in the + and -, or &lt; and >.

Anyway, you're seeing this as well as I am, but you probably know how to refactor it better than I do. How would you do it ? :)

Note : I've rewritten most of this from my phone, sry for the terrible code formatting, it's really impractical. Feel free to edit
If I take newHeaderHeight = HeaderViewHeightConstraint.Constant - offsetDifference and expand offsetDifference I get: newHeaderHeight = HeaderViewHeightConstraint.Constant - (offset - _previousOffset) which is newHeaderHeight = HeaderViewHeightConstraint.Constant - offset + _previousOffset

If I then go to the else statement and expand newHeaderHeight = (HeaderViewHeightConstraint.Constant + offsetDifference); I get newHeaderHeight = HeaderViewHeightConstraint.Constant + (_previousOffset - offset); which is newHeaderHeight = HeaderViewHeightConstraint.Constant + _previousOffset - offset;

They're the same thing. So you can basically remove them from the if/else statement and only write them once.

The second part is a bit more tricky to refactor because you compare it with min values in one condition and max values in the other. It would however still refactor each ternary operator statement with Math.Min() or Math.Max() (careful not to get them the other way round).