1

I wrote a simple program that accepts input from the user and capitalizes it, obviously this can be done in different ways

class example():

    def say_it(self):
        result = input('what do you wanna say to the world')
        return result

    def get_result(self):
        res = self.say_it()
        return res

    def capitalize(self):
        res = self.get_result()
        res = res.upper()
        print(res)


def main():
    Ex = example()
    res = Ex.capitalize()

if __name__ == '__main__': main()

This program has 3 methods in the class body, then a new instance is created in the main function and only the capitalize method is called and the class does the whole magic and prints out a capitalized in put from the user making the whole main method look very clean

    class example():
        def say_it(self):
            result = input('what do you wanna say to the world')
            return result

        def capitalize(self, words):
            words = words.upper()
            return words


    def main():
        Ex = example()
        res = Ex.say_it()
        final_result = Ex.capitalize(res)
        print(final_result)

    if __name__ == '__main__': main()

The second program does the same thing but it has less methods in the class body and more methods in the main method, it calls the methods in the class and works with the results returned, and then the final print statement is actually issued in the main method unlike the first program, thought it looks like the main method could get very confusing when the program expands and grows

My question is this which method will scale better in real life situations (i.e more readable, easier to debug) where they might be like 15 methods, will it be better to just call a single method that does all the magic and gets the result or call the methods one by one in the main method, i sometimes find myself writing programs the first way where i just call one method and the class handles everything else, Also is there any difference in speed between this two programs, which one will be faster?

3
  • I'm voting to close this question as off-topic because it belongs on codereview.stackexchange.com. Commented Apr 18, 2015 at 4:42
  • The code doesn't even run, you need to watch formatting carefully in Python! That said, the biggest issue with your program is IMHO that it uses classes without need. There are a bunch of lines that could all be stripped by writing the three relevant lines one after the other. Commented Apr 18, 2015 at 8:45
  • @Ulrich i must have made a mistake in the second run, i tested it before posting, and if you read my question properly i'm asking if it's better to have a single method that starts the whole program and allows the class do the work and print the result vs calling the methods one by one and working with the returned values i didn't want improvement on this program it's just a program i wanted to use as a reference to my question Commented Apr 18, 2015 at 9:33

1 Answer 1

2

Functions should do what they say they do. It is confusing to have a function called capitalize() that goes and calls a function to print and prompt and gather input.

A function shouldn't just call another function and not provide any value. The get_result() function doesn't serve a purpose. Calling say_it() instead produces the same result.

Your class should keep the data. That's the whole point of the object. Main can call the functions, but it shouldn't have the data. The words should have been stored in the class.

There is not perceptible performance difference between who calls the functions.

Sign up to request clarification or add additional context in comments.

1 Comment

forgive me if the whole program looks lame and pointless, i was just trying to demonstrate if there's any advantage in firing the whole program with just one method instead of returning values and using multiple methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.