Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Connecting functions Vs. Handling from main

Hello,
I recently started to wonder, is it better calling 1 function from main and then call other functions from within it, or make every function return some value to main and decide which function to call next by it (from main)?

What is wiser and considered better coding?

Thanks in advance :)
(Aug 16, 2018 12:38 AM)ApexLead Wrote:  Hello,
I recently started to wonder, is it better calling 1 function from main and then call other functions from within it, or make every function return some value to main and decide which function to call next by it (from main)?

What is wiser and considered better coding?

Thanks in advance :)

better coding is totally up to the coder. say for example you have 3 random functions all returning something

PHP Code

<?php 
def myfun1
():
    
c = 15 + 3
    
return c

def myfun2
():
    
x = 1 + 3
    
return x


def myfun3
():
   
a = 33 -3
    
return a

main
():
     if 
myfun1() > myfun2() > myfun3():
          print(
myfun1())
     
elif  myfun2() > myfun1() > myfun3():
          print(
myfun2())
     else:
          print(
myfun2())
main() 

and you wanted to print the largest value you can do it from main which for me would be the best way.

however if each function rely on data returned from each other then it would be best to make them call each other and not use main

however if you have
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
There are a couple of best practices that you should know.

A method should only do 1 thing, that's it. If you have your architecture focus on building it in the right way according to software patterns. Afterwards make a class that is a mediator and basically calls the other methods. If you do it like this all the methods can stay the same and if you want to change something you only have to check the mediator. Never put logic in the main, you will have dependency problems later.

https://en.wikipedia.org/wiki/Mediator_pattern
[Image: 89758]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Robin2Jan 13, 2019 06:27 AM 2 123 Jan 13, 2019 06:27 AM
Last Post: lockhead
[C#] by severosMay 3, 2018 07:07 AM 8 262 May 3, 2018 07:07 AM
Last Post: ppaida
[Python] by TheWarOwlApr 21, 2017 05:41 PM 5 244 Apr 21, 2017 05:41 PM
Last Post: TheWarOwl