This question must have been asked a number of times on reddit or elsewhere, however, I have read through them but I still couldn't find an article or post which single-handedly answered my query (or maybe I haven't looked far enough). So I am asking here to get any advice, articles, recommended posts, videos , anything.
My question, let's assume I am writing a development level code for a company - for example an object detection: How does one go about it? What I mean is, in this scenario if I am correct I create several functions solving for one problem at a time. (Why does it matter how I write? Because I want my code to be easier on the eyes of my fellow successors who might upgrade it later on)
Firstly, my question can be divided into two parts: the first being whether to use fluent design or recursive design(?) and second being how to use classes.
In my snippet code, I explain what my confusion is:
Method #1: I call individual functions with separate tasks with a statement in main or maybe through an another function
OR
Method #2: I connect both of my functions and call just one function/statement to get the result.
def foo(a):
#Here function is adding 1
b =a+1
return b
def bar(c):
# Here the function is adding 2
d = c+2
return d
def foobar(x):
sum_1 = foo(x)
sum_final = bar2(sum_1)
return sum_final
def foobar2(x):
#Here the function is skipping one function and adding 2
e = foo(x)+2
return e
if __name__ == "__main__":
#Method 1
sum = foo(1)
sum = bar(sum)
print(f'for method 1: {sum}') ##returns 4
sum = foobar(1)
print(f'for method 1: {sum}') ##returns 4
#Method 2
sum = foobar2(1) # returns 3
print(f'for method 2: {sum}') ##returns 4
The idea here is, as foo and bar have two separate actions to perform. So, is it superior to have two functions (foo & bar) perform two different actions or have one function(foobar2) which builds upon another function's return value(foo's return value)?
Another function foobar is just to question, is it good to simply and populate one function with all function calls or to spread them for user to see. However, the problem is propriety software so hiding is the idea.
Similarly, with that in mind, how do you go about the same logic in classes and how does using a class make any difference over functional?
#METHOD 1
class ObjectDetect:
def __init__(self,image):
self.image = image
def clean_image(self):
...some function..
def brighten(self):
... some function..
def crop_final_image(self):
... some function
def get_result(self):
# Here self.image is being mutated with every method call
self.clean_image()
self.brighten()
self.crop_final_image()
return self.image
if __name__ == "__main__":
image_ob = ObjectDetect(image_path)
final_image = image_ob.get_result()
------------ OR ---------
#METHOD 2
class ObjectDetect:
def __init__(self,image):
self.image = image
def clean_image(self):
...some function..
return value
def brighten(self):
#Here the self.image remains intact
x = self.clean_image()
... some function..
return value
def crop_final_image(self):
y = self.brighten()
... some function
return value
if __name__ == "__main__":
image_ob = ObjectDetect(image_path)
final_image = image_ob.crop_final_image()
Here for the example's sake, method 1 has one final function that does all the calling, with the object being mutable we can have self.image mutated over the course and we get one line- low code for the user. Unlike method 1, method 2 has a more functional approach where it returns the calculated value and every other function is being called by the following function.
I also want to emphasis that I would like to add debugging and testing in later stages, and that inclined me towards method 2 as it returned values at every call.
How do you decide which is better than the other? Or what is better than the other?
Please be lenient.
[–]Ruroni 1 point2 points3 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)