Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original Design
- Volume gallonsLeft = Volume.Gallons(1.0f);
- gallonsLeft += 1.0f; // 2 gallons
- gallonsLeft += Volume.Ounces(64.0f); // 2.5 gallons
- Volume litersLeft = gallonsLeft.inLiters; // 9.46 liters
- MethodThatExpectsFloat(gallonsLeft); // Gets 2.5 gallons
- MethodThatExpectsLiters(litersLeft); // Gets 9.46 liters
- // Suggested Design
- Volume volumeLeft = 1f * Volume.Gallon;
- volumeLeft += 1f * Volume.Gallon; // 2 gallons
- volumeLeft += 64f * Volume.Ounce; // 2.5 gallons
- volumeLeft -= 500f * Volume.Milliliter; // 2.37 gallons
- float gallonsLeft = volumeLeft / Volume.Gallon;
- float litersLeft = volumeLeft / Volume.Liter;
- MethodThatExpectsFloat(gallonsLeft); // 2.37 gallons
- MethodThatExpectsFloat(litersLeft); // 8.97143 liters
- // Hybrid Design
- Volume gallonsLeft = 1f * Volume.Gallon; // Volume.Gallon keeps gallons as its 'native' unit; this is inherited by gallonsLeft. The actual stored value is still in the common unit, liters.
- gallonsLeft += 1f; // 2 gallons
- gallonsLeft += 64f * Volume.Ounce; // 2.5 gallons
- gallonsLeft -= 500f * Volume.Milliliter // 2.37 gallons
- Volume dramsLeft = gallonsLeft.inDrams // 'Native' unit is drams, as there's an explicit instead of implicit conversion here.
- MethodThatExpectsFloat(gallonsLeft); // Gets 2.37
- MethodThatExpectsFloat(dramsLeft); // Gets 2424.74; internally both gallonsLeft and dramsLeft store the same value: 8.96 (liters).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement