Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function declaration ordering #103

Open
rofinn opened this issue Feb 24, 2022 · 4 comments
Open

Function declaration ordering #103

rofinn opened this issue Feb 24, 2022 · 4 comments

Comments

@rofinn
Copy link
Contributor

rofinn commented Feb 24, 2022

Does anyone else wish we had a convention on ordering of functions in a file? I typically find putting private (more specific) functions further down in a file is more readable, but I don’t think that’s consistent. I also think some folks prefer the opposite to make copy-pasting into a REPL more intuitive? I suppose you could make the claim that they should just live in a different utilities file, but if they’re only used in that one file it’s kinda nice to keep them together...

@iamed2
Copy link
Collaborator

iamed2 commented Feb 24, 2022

My preference is usually a topological sort, so functions below depend on functions above. I find this reduces the amount of jumping around the file I do while reading code as by the time I'm reading a function I will have encountered all the functions it calls.

@mattBrzezinski
Copy link
Member

This is something which I've gone back and for on for quite some time, and still don't have an answer that I really like sadly.

The few things that I've tried so far,

  • Internal functions at the top of the file, prefaced with an _ then your exported functions at the bottom. When I read through the file, I can make quick notes of what these functions are doing, then get to the meat of the file at the bottom. Also the order of these functions is then laid out such that no function which depends on another is out of order
  • If there are a lot of internal functions, say 7+, I pull them into a utility file and then reference them that way

@nickrobinson251
Copy link
Contributor

I've personally no strong opinions about the ordering, only that consistency within a file is good (don't have "helpers" both above and below the function that uses them, if you can help it, as that leads to the most jumping about).

I think i tend to put the "main" function top, and helpers after.

But perhaps a very small reason to favour the other way is that @generated functions require it:

Generated functions are only permitted to call functions that were defined before the definition of the generated function.

@iamed2
Copy link
Collaborator

iamed2 commented Feb 25, 2022

@nickrobinson251 that may very well be how I picked up this habit, after working on AxisArrays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants