This is a great use of functions.
This will be a pretty anemic function (doesn't do much)...
That's good. Functions should only do one thing.
rather localized...
In an OO language, make it private.
(so not general purpose)
If it handles more than one case, it is general purpose. Moreover, generalising isn't the only use of functions. They are indeed there to (1) save you writing the same code more than once, but also (2) to break code up into smaller chunks to make it more readable. It this case it is achieving both (1) and (2). However, even if your function was being called from just one place, it might still help with (2).
and doesn't stand well on its own (can't figure out what you need it for unless you see where it is used).
Come up with a good name for it, and it stands fine on its own. "ValidateFileParameters" or something. Now stands fine on its own.
This is a pretty common problem for scientists. I've seen it a lot, and it always stems by the fact that programming is something you pick on the side as a tool to do your job.
So your scripts are a mess. I'm going to go against common sense and say that, assuming you're programming alone, this is not so bad! You're never going to touch most of what you write ever again, so spending too much time to write pretty code instead of producing "value" (so the result of your script) isn't going to do much to you.
However, there is going to be a time where you need to go back to something you did and see exactly how something was working. Additionally, if other scientists will need to review your code, it's really important for it to be as clear and concise as possible, so that everyone can understand it.
Your main problem is going to be readability, so here's a few tips for improving:
Variable names:
Scientists love to use concise notations. All mathematical equations usually use single letters as variables, and I wouldn't be surprised to see lots and lots of very short variables in your code. This hurts readability a lot. When you'll go back to your code you're not going to remember what those y, i and x2 represent, and you'll spend a lot of time trying to figure it out. Try instead naming your variables explicitly, using names that represent exactly what they are.
Split your code into functions:
Now that you renamed all your variables, your equations look terrible, and are multiple lines long.
Instead of leaving it in your main program, move that equation to a different function, and name it accordingly. Now instead of having a huge and messed up line of code, you'll have a short instructions telling you exactly what's going on and what equation you used. This improves both your main program, as you don't even have to look at the actual equation to know what you did, and the equation code itself, as in a separate function you can name your variables however you want, and go back to the more familiar single letters.
On this line of thought, try to find out all the pieces of code that represent something, especially if that something is something you have to do multiple times in your code, and split them out into functions. You'll find out that your code will quickly become easier to read, and that you'll be able to use the same functions without writing more code.
Icing on the cake, if those functions are needed in more of your programs you can just make a library for them, and you'll have them always available.
Global variables:
Back when I was a beginner, I thought this was a great way for passing around data I needed in many points of my program. Turns out there are many other ways to pass around stuff, and the only things global variables do is giving people headaches, since if you go to a random point of your program you'll never know when that value was last used or edited, and tracking it down will be a pain. Try to avoid them whenever possible.
If your functions need to return or modify multiple values, either make a class with those values and pass them down as a parameter, or make the function return multiple values (with named tuples) and assign those values in the caller code.
Version Control
This doesn't directly improve readability, but helps you do all the above. Whenever you do some changes, commit to version control (a local Git repository will be fine enough), and if something doesn't work, look at what you changed or just roll back! This will make refactoring your code way easier, and will be a safety net if you accidentally break stuff.
Keeping all this in mind will allow you to write clear and more effective code, and will also help you find possible mistakes faster, as you won't have to wade through gigantic functions and messy variables.
Best Answer
This is commonly referred to as functional decomposition and is generally a good thing to do, if done correctly.
Also, the implementation of a function should be within a single level of abstraction. If you take
largeReportProcessingFunction
, then its role is to define which different processing steps are to be taken in which order. The implementation of each of those steps is on an abstraction layer below andlargeReportProcessingFunction
shouldn't depend on it directly.Please note that this however is a bad choice of naming:
You see
callAllSection1Functions
is a name that doesn't actually provide an abstraction, because it doesn't actually say what it does, but rather how it does it. It should be calledprocessSection1
instead or whatever is actually meaningful in the context.