Recently I had to refactor some legacy code. As in most cases, I had to split big parts of code into smaller, cleaner and readable functions. I ended with many functions, that had multiple, weird parameters. Let me show an example:
public void SendOrder(Order order, XmlOrder xmlOrder, Insider insider) { string customerCode = CustomerServices .Get(insider.CustomerId) .Code; OutputData outputData = CreateOutputData(order, xmlOrder, customerCode); CreateReservations(order, customerCode, outputData); PlaceOrder(order, xmlOrder, outputData, customerCode); } ... private void CreateReservations(Order order, string customerCode, OutputData outputData) { ... try { ReservationServices.AddReservation(reservation); } catch (BusinessException ex) { Logger.Log(ex); outpuData.Status = Statuses.BusnessError; throw; } }
(That is just demonstration code, not real one)
The problem is I had to pass for example outputData to other functions just to change it’s status if an exception happens or pass customerCode to multiple functions. Class is responsible of sending multiple, not connected messages to WebService, so when I was creating it, I didn’t plan to put variables connected to given order as a class state. Is it a good practice to exclude such variables from function and make them a class members? Are there any guidelines for such situations? What are your practices and solutions for such problems?