The MFAssignR package was designed for multi-element molecular formula (MF) assignment of ultrahigh resolution mass spectrometry measurements. A number of tools for internal mass recalibration, MF assignment, signal-to-noise evaluation, and unambiguous formula selections are provided.
As the function is really long and the code is hard to read, the function should be refactored so that it also becomes easier to re-use code and extend the module in the future.
It also seems that some code from the IsoFiltR is copy-pasted in the MFAssignCHO module, so it is worth checking where code was potentially taken from or whether some steps might already be contained in other functions.
While refactoring, the following procedure often makes sense. After every change, unit tests should be run to make sure that the code still does the same
format the code
remove not used variables - the vscode syntax highlighting can help with this
try to reduce the number of temporary variables used etc.
rename variables which have bad names!
extract functions - this can be sometimes a bit tricky
often there are pieces of code which as a result, change a single variable. many new variables might be defined in that section but never used again. These pieces of code are great candidates to be extracted into functions. Start checking for those first. You can see whether a symbol (aka variable) is used using the "find all references" operation in vscode.
large if/else blocks often as a result should be two functions. If they return the same variable, you could use ternary assignment to initialize the variable.
contents of loops. this can be tricky due to the way how results are combined.
When extracting functions. the best way is to refactor layer by layer. Look at the top level function and see if you can see multiple things happening at the same level which are the overarching steps. Basically, try to split the highest layer.
As the function is really long and the code is hard to read, the function should be refactored so that it also becomes easier to re-use code and extend the module in the future.
It also seems that some code from the IsoFiltR is copy-pasted in the MFAssignCHO module, so it is worth checking where code was potentially taken from or whether some steps might already be contained in other functions.
While refactoring, the following procedure often makes sense. After every change, unit tests should be run to make sure that the code still does the same
When extracting functions. the best way is to refactor layer by layer. Look at the top level function and see if you can see multiple things happening at the same level which are the overarching steps. Basically, try to split the highest layer.