While it's sometimes better to switch network at end of your contract, it really does depend how long you've been with the network you are on, and whether they'll offer a good deal to keep you.
I have only started using O2 in the last week (but have used Orange and Virgin for 10 to 15 years on PAYG, had a contract in 19998 with T-Mobile and have only just started using them again, and have never used Vodafone). So don't know what policies there are for upgrades, and feel it's something of a lottery.
Near the end of contract, if you ask for your PAC (code to allow porting your mobile number to another network) they may start ringing you with offers as they know they have the potential to tempt you back if they can match the best deal you have found. It's certainly worth running some checks for phones (and monthly charge you would be willing to pay) to see how the minutes/ texts/ data compares with what you get and what you are paying, and then doing similar for a SIM Only deal (so you are ready for offers which include or exclude a phone, from your existing network)... Then when you have about 30 days left of your contract, ring and ask for the PAC, and if they start ringing you, aim to get them to match the package you have found elsewhere. Sometimes they will, or may drop the price of what you have been getting a lot, or a little. Other times they may not budge. Might be worth waiting until contract has 3 or 2 weeks remaining (if the day when you ring is close to the end of the month) to see if they have any targets to meet, and keeping you happy by giving you a good deal might be to your advantage and help them towards their target.
If you have some other deal lined up, you have the upper hand as you can say "Sorry, I can get better deal with XXX and will use my PAC in the next 48 hours" (which allows them a final chance to match the best alternative you've found).