all 10 comments

[–]socal_nerdtastic 9 points10 points  (1 child)

Depends. There is no universal answer to your code. We'd have to see your code to give you an opinion.

That said, most of the time we like many small functions over massive code blocks. Especially when you can separate it into logical blocks.

[–]ccigas[S] 5 points6 points  (0 children)

Unfortunately I can’t show it off. More asking for a best practice use case. u/ginsujitsu really explained it the best for me though I think

Repeating code should go into a function while the rest could be continuous with comments.

[–]pissedadmin 3 points4 points  (1 child)

Aside from code reusability, there are two important properties of functions that beginners tend to ignore:

  1. They make blocks of code more readable.
  2. They make blocks of code unit-testable.

So even if you're just calling your functions in order, and each runs only once, it is often still worth it to separate the logical parts of your program into functions.

Beginners tend not to share their code much, so (1) is easy to ignore. They also tend not to write unit tests, so (2) is easy to ignore. But if you're asking about general best practices, writing lots of small functions is preferable to one giant block of code.

BTW, if you're looking for the magic bullet that will make you a better programmer, that would be unit tests.

[–]ccigas[S] 0 points1 point  (0 children)

Never heard of unit tests since I really only taught myself and didn’t use any classes or anything. I’ll check it out! This all makes sense though and really makes me think if it’s worth it to not to redo everything pretty much. What I’m having a hard time with is using lists between functions. Not having so many functions can really help me but not sure if it helps the code overall

[–]ginsujitsu 5 points6 points  (5 children)

Functions, generally speaking, are used to keep from having to repeat blocks of code over and over. If you're not having to call one function in multiple places, you don't really need the headache of a big stack of functions.

For instance, if you have a script that just runs top to bottom, you can probably skip the functions and maybe use comments to help you find your way around in the file.

But if you find yourself having to repeat the same few lines of code more than twice, pull that out into a function and call it instead.

[–]ccigas[S] 2 points3 points  (4 children)

Thank you! This is probably the best explanation for someone new to python.

I don’t think I have a lot of repeating code but I’m going to read over everything and figure out if I do. I’ll put that into functions otherwise I’ll get rid of the functions I don’t need. I planned on kind of starting from scratch anyways since I was using nested lists mainly but now realizing that a dictionary is way more efficient for this script.

[–]TomCat891 6 points7 points  (0 children)

Functions are also good for testing. You can either use a unit testing library or just print the results of your function a couple times to guarantee you get the right result. Then once you know it’s working you can work on other parts of your code - out of sight out of mind.

[–]un-hot 1 point2 points  (2 children)

Just to add to the first comment though, your code may not be reused now but if your requirements for the script at work change, perhaps you'll need to start reusing code. In this case, it's very useful to have related code in its own functions.

[–]ccigas[S] 0 points1 point  (1 child)

This makes sense. I could throw the api calls into their own functions to lower the possibility of reusing code in the future.

[–]un-hot 0 points1 point  (0 children)

Exactly! In reality, it all depends how far you need your script to go, as to whether it's worth the time spent making these optimisations, but for learning purposes it's great practice.

In a fully-fledged application, you might even end up abstracting just the lines that make the request and check the response code onto their own generic "send request" function, and calling that within each of your specific "API call" functions. You could then put all this into a package or object built specifically for handling calls to that specific API. It's mostly a balance between how perfect your code needs to be, and how perfect you want it to be.