Python Forum
Is it bad practice to return a variable and not use it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it bad practice to return a variable and not use it?
#1
I was just wondering, as in my application here, I have tried to stick to the principle that a method does just one thing, and in doing this, I am able to use functions a few times for different things, however, sometimes the Dictionary that I am returning I need to not use or it will duplicate data, is this ok, or is there a better way to do this?

kind regards
iFunk
Reply
#2
Could you give a small example?

If you're not using the return value, then it sounds like there's probably a side effect. Side effects are worth avoiding if it's reasonable. One convention for making side effects clear is to not return anything (like the sort() method on the built-in list object).
Reply
#3
You can leave it to the GC.


garbage = my_func(*args,**kwargs)
del garbage
# my_func(*args,**kwargs)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
So basically, I have a function that updates a table widget in Qt4, that's all it does, it gets sent a dictionary, it completes 2 fields that may or may not be in there, then puts the results to a single line of a Table Widget and then returns the completed dictionary back to the place it was called from, which then saves it to CouchDB, but also saves it locally to a txt file.

Now This text file is primarily a backup in case of a network failure so we don't lose data, but I have also used it to re populate the Application window should the application be closed and then re opened for any reason. On this occasion I use a loop to grab the data back into a dictionary and then back to this function that populates the Table Widget, which in turn sends the dictionary back. On this occasion, I obviously don't want it to save anywhere, as the data is already there and it will duplicate it in quite the nastiest way. This in turn leaves a variable that won't be used. I have accounted for side effects on this occasion, but I would be very much interested in a better practice for this kind of situation as my application is only small, but I can see things getting messier as I get into larger programs.
Reply
#5
If the place the function is called from is saving the files you don't want saved, I would add a parameter to that place: save = True. Then have a conditional based on that parameter, and put the saving code in that conditional. That should not change the behavior of your code normally. However, when you loop to refresh the Table widget, you call that place with save = False, preventing duplication of the data.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
You answered the question yourself.  Basically, you don't want to write code that will make your program more difficult to reuse or debug.  In other words, code should be clean, and minimize errors, because as you write more complex programs, you will have difficulty debugging the code as your programs get larger.  Further, object oriented programming is based around the idea of making your code easier to debug and reuse.  So with that said, you should practice good programming principles and not return values from functions when they won't be used.
Reply
#7
Use the conditional to decide if you want to use the returned object or not. If you do not use it GC will take care of it.

if condition:
    returned = my_func(*args,**kwargs) # use the returned object to back up the dict
else:
    my_func(*args,**kwargs) # the function returns an object but since there is not a reference to it, it will be GCed
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
what the method returns should be what the method is defined to return.   whether it will return something useful or just return a None depends on the design.  this means we need a good clean design, first (even if we design parallel with coding).  how will the method be used?  could there ever be a case where the caller can use return data?  which data can it use?  could another method return it?  will the other method cause no harm for this purpose?

in general there is no harm in returning data and not using it.  only a reference is actually handled so this will only slightly delay garbage collection which only happens if no other references to it exist.  the biggest issue might be if the method does a lot of work building data that is not very likely to be used.

the only times i have ever had an issue with returning data is when there are two or more kinds of data to return.  in some cases i made a choice.  in some other cases i returned a tuple of all (or most of) the choices.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
@terrancepython11's answer is a good one. The code has to be clean, simple and readable as it could. If the code somehow is twisting your brain as you following it, try to change it. Simplify it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  best practice example data science mostafa705 1 758 Oct-13-2023, 04:44 AM
Last Post: KianLynch
  Practice coding through hackathon!! TheOutcast 0 1,444 Jun-11-2020, 12:03 PM
Last Post: TheOutcast
  Coding Practice BarakTu 2 3,278 Apr-14-2018, 05:31 AM
Last Post: buran
  is it considered good practice to ... Skaperen 9 6,183 May-10-2017, 02:42 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020