I have been developing an app for retrieving records from a database (SQL, tkinter, windows).
Over time I have externalised 8 functions into 8 classes . "Single-purpose" functions like creating the tkinter Gui, etc.
Why ? Readability, maintainability etc...etc...
I could do at least 5 or 6 more classes . (Like handling the SQL query & results...)
My question: will the growing number of external classes slow down the app ?
Are there things that should definitely not be in a class ?
Just wondering.
thx,
Paul
(Jul-25-2024, 07:37 AM)DPaul Wrote: [ -> ]My question: will the growing number of external classes slow down the app ?
Are there things that should definitely not be in a class ?
For the first question the answer is no. There should be no significant impact unless you instantiate hundreds of thousands of objects.
For the second question, I'd say no too, but it is more a personal opinion. Some people advocate to write more functions and less classes. These discussions often look like a waste of time to me.
(Jul-25-2024, 07:37 AM)DPaul Wrote: [ -> ]My question: will the growing number of external classes slow down the app ?
In general: All abstractions costs RAM or CPU.
Yes, it slows down the app in a range of
microseconds.
We don't care about this, until we call a method a million times.
But the same for functions or massive iterations.
import time
class Foo:
pass
start = time.perf_counter()
for _ in range(1_000_000):
Foo()
duration_us = (time.perf_counter() - start)
print(f"Creating new instances took {duration_us:.3f} µs per instance.")
This was on a Raspberry Pi Zero W Rev 1.1
Output:
Creating new instances took 4.203 µs per instance.
Just don't do stupid things like creating an instance of a class in a loop, if the instance could be reused by the loop.
OK guys, Thanks.
You just gave me a truckload of extra work.
If you would have said "no, don't make more classes", I would have been
watching the oplympics, now I'm coding.
Paul
Find some time for the Olympics, they are ending soon
