r/pythonhelp • • 5d ago

Putting everything into seperate functions

Hello, i recently started making my own Qt layout, and i need to run the same chunk of code multiple times, should i split every little chunk of code into seperate functions calling each others or should i not?

1 Upvotes

5 comments sorted by

•

u/AutoModerator 5d ago

To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/AlexMTBDude 5d ago

The D.R.Y principle applies here. Don't Repeat Yourself. So, yes, split everything so there is no copy-pasted code. https://www.geeksforgeeks.org/software-engineering/dont-repeat-yourselfdry-in-software-development/

1

u/Mathie1729 5d ago

Eh, DRY is good, but splitting every little block into its own function can make code harder to follow. If a chunk is only used once, just leave it inline until you see actual duplication. Then factor it out.

1

u/duane11583 4d ago

Really old coding standard from Vulcan the original mars Viking lander in the 1970s 

  1. no function longer the 24 lines
  2. nothing wider then 80 columns

3)and tabs are 8 wide

Why this way?  Back in the day they would print the functions on paper one per page and have a formal code review in a conference room

1

u/dnult 3d ago edited 3d ago

That approach makes for readable code and improves separation of concerns, however function calls have overhead - pushing and popping data on the stack. So if you've got computationally complex functions that get called iteravely thousands of times, then you may want to consider how that will impact your design. Most of the time it isn't an issue unless you're processing millions of records and timing is critical.

Like most of the recommendations you hear repeated about best programming practices, it depends on what you're trying to do whether or not they make sense for your specific task. Things like KISS and DRY are generally good so long as they don't create other problems.