jmsnet has asked for the wisdom of the Perl Monks concerning the following question:
Hi all
Have some general questions about subroutines in terms of good practices to follow to make the code as readable but also efficient as possible -my $ticket = get_ticket($rest_client, $sdn_controller); if (! defined $ticket) { print "Could not get ticket from $sdn_controller\n"; exit; } more code ..... sub get_ticket { my ($rest_client, $sdn_controller) = @_; my $ticket; ........; return $ticket; }
so the subroutine connects to an SDN controller and sends a REST call, gets JSON back and extracts an authorisation ticket for subsequent requests. I have another subroutine for sending all the other requests because the initial ticket request is handled differently. But both subroutines take the same arguments, are doing error checking on the HTTP response code etc. so there is some code replication which leads to first question
1) should a subroutine do one thing and one thing well rather than trying to combine both subroutines into one because of the common code between them. In other words I could use one subroutine to use the common code but then would have to make it more complex and test whether I was asking for a ticket or a just requesting data from the controller.
2) the return value from the ticket and data subroutines are values, scalar for the ticket, and a reference to a data structure for the data subroutine, the exact type of structure depending on what JSON sent back. So if the ticket request or data request fails then the return value is undef. Each time i call the subroutine and get a return value i then have to do an if loop to check whether the value is defined or not. Is there a more efficient way of doing this because I am basically just repeating if statements after each call eg. if i didn't get a ticket I could just call exit in the subroutine but it seems more logical to me and easier to follow if I handle it outside the subroutine.
3) finally as you can see from above i use the same variable names in both main and the subroutine but use my to keep them separate. I can see how that might confuse someone else looking at the code later and also if I forget to use my how it might confuse me :) I just keep running out of names. Is there a good standard to follow that others have found works ?
Thanks for your time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: subroutine and good practices
by Athanasius (Archbishop) on Mar 05, 2016 at 02:38 UTC | |
|
Re: subroutine and good practices
by Tanktalus (Canon) on Mar 04, 2016 at 22:52 UTC | |
|
Re: subroutine and good practices
by dsheroh (Monsignor) on Mar 05, 2016 at 09:05 UTC | |
by Anonymous Monk on Mar 05, 2016 at 10:58 UTC | |
|
Re: subroutine and good practices
by poj (Abbot) on Mar 05, 2016 at 12:24 UTC |