r/learnpython • • Jul 09 '20

I made a calculator in Python!

Hello everyone!

I just wanted to share this project i made, its not my first programming project or python project whatsoever, but it is indeed the first one i didnt depended much on google or a video tutorial so that for me its a great advance.

The project is also my first on a github repository!

https://github.com/mexicanlefty/Python-Calculator

Please take a look and test it, in case you can provide any feedback it would be even better, it doesnt matter if you guys want to be very critical, i dont mind i want to keep practicing and i already have my next project in mind.

392 Upvotes

49 comments sorted by

View all comments

8

u/njape Jul 09 '20

Overall I think it looks good, nice work!

Small comment is that I would change the mode numbers to constants or similar for better readability.

As already noted I don't like global variables, it is a sin. For future projects, and maybe also as an addition to this project, I would urge you to add some unit tests to the code. While doing this you will notice one of the reasons global variables should be avoided. It will also help you to improve how you go about designing your program so that it is modular and testable. Don't care about 100%test coverage in the tests, focus on what functionality is important, special cases and edge cases and you should find a good middle ground.

If you find it hard to isolate a function for unit testing, without major mocking, you most likely have some problem with the design.

Keep up the good work!

2

u/mexicanlefty Jul 09 '20

Yeah, you are right i still have issues with code design. Any advice on how can i improve it?

2

u/njape Jul 09 '20

First a note I prefer to write the backend code, and does not have much experience with gui programming, or tkinter.

From a testing perspective I'm guessing your main issues comes from the global variables that are used within each function you would like to create tests for. There are a couple of possible solutions to this, s I me examples.

  1. Wrapping the calculator and its functions in a class which will contain your global variables. This would allow you to prep a calculator object in different ways before running each callback function.

  2. Change so that the callback function does not use the global variables, but instead take the necessary values as inputs. This will make the testing setup trivial, but it will require you to define function stubs, or lambda functions calling the correct function with the global variables.