Xamarin.Forms Code You Can Delete Today! (part 1)
Your Xamarin.Forms app definitely contains some code that you can delete. Code that is causing you a performance hit. Code you can delete today, right now, pronto, with no risk. Seriously
Let's get philosophical for a moment.
As much as we as developers really love to solve problems by writing code - deleting code, (or writing less of it in the first place) should be a fundamental desire of mature developers who recognize that each line of code written is a trade-off: is this code worth the potential bugs and the cost of care and feed (maintenance) down the road?
Good News for Deleters
This tip comes from the now-famous Optimizing App Performance with Xamarin.Forms talk that Jason Smith gave at the 2016 Xamarin Evolve conference. Even though the talk was given nearly 2 years ago, I/we/apps are still breaking many of the cardinal Xamarin.Forms no-no’s and needlessly paying the price in performance.
Here are the detailed steps to delete this hurtful code from your app:
- Go to Visual Studio and search for LayoutOptions.Fill in your entire Xamarin.Forms solution
- Delete all those lines of code. (I like to use cmd+x)
That’s it! Hasta la visa, baby!
This code may seem innocuous, not really hurting anything - but there are 2 reasons why you need to delete this code from all your Xamarin.Forms apps ASAP.
The default value of a view’s HorizontalOptions and VerticalOptions properties are already LayoutOptions.Fill, so it’s completely unnecessary to set this.
Every time you set it, your app is taking an unnecessary performance hit on the chin. Even though you’re setting those properties to what they already were, it comes with a cost, so just don’t do it. In addition, the Xamarin docs say changing these properties “consumes memory, even when setting them to the default values”
Now that your app will benefit from this change, do your team a favor and point out this necessary code-ectomy in your next PR. We’re all learning to be better Xamarin.Forms developers.
- Optimizing App Performance with Xamarin.Forms, by Jason Smith
- Kent Boogart put together this really great, easily accessible summary that’s helpful as a reference
- Optimize Layout Performance from Xamarin docs