metamath / metamath-exe

Metamath program - source code for the Metamath executable
GNU General Public License v2.0
77 stars 25 forks source link

A simple question #113

Closed benjub closed 1 year ago

benjub commented 1 year ago

Beginner question: Why do we have the function declaration at https://github.com/metamath/metamath-exe/blob/469dd4d967328e0cbfb0a9edbfec3d44f86dfbf2/src/metamath.c#L724 and its definition at https://github.com/metamath/metamath-exe/blob/469dd4d967328e0cbfb0a9edbfec3d44f86dfbf2/src/metamath.c#L790 (while it is called at https://github.com/metamath/metamath-exe/blob/469dd4d967328e0cbfb0a9edbfec3d44f86dfbf2/src/metamath.c#L776) ? Can we "merge" the declaration and the definition ?

wlammen commented 1 year ago

You need a declaration (or its extension: definition) before its first use. If you delete line 724, you will get a compiler error in line 776. C does not look ahead to see what is coming in the rest of the file. The alternative could be to move line 790ff to line 724. I don't know whether this would drag even more dependent code besides command() to this position. I think the author wanted main() to appear early. I do not often look into the issues section, so I came across your question not before today.

benjub commented 1 year ago

Thanks @wlammen. After looking a bit at metamath.c, I think that it would make more sense to actually move the command function to a new file, say mmcmd0.c with the header mmcmd0.h. This would make metamath.c less huge. The only real development experience I have is with smaller codebases and in other languages than C, but I liked having a not-too-large "main file". What do you think, @wlammen @digama0 ?

digama0 commented 1 year ago

I don't think it helps that much, considering that the command() function is 90%+ of the whole file. The entry point comes at the beginning of the file, which I think is a fine convention, but I don't think it necessarily has to be the only thing in the file.

If the command() function was split into smaller functions, it might make sense to have a file to group all those sub-functions together, but having the top level logic in metamath.c also makes some sense. Finding commands in metamath.c is somewhat difficult due to the size of the main function; having sub-functions would help with that issue.

wlammen commented 1 year ago

From a design/architecture perspective it makes a lot of sense to break down big functions into smaller subunits, and even detach definable blocks of code from the rest and access them through interfaces. Also it makes sense to me to separate layers of code ranging over infrastructure / library / user interface. I could elaborate more on this, but I feel I am a bit out of the picture now.

benjub commented 1 year ago

Regarding "the entry point" (typically void main (void)?), I'm surprised to see that beginning of the file is a fine convention. I had the habit (in Python and OCaml) of putting / looking for it towards the end.

If I understand @digama0 and @wlammen, it would help to subdivide the function command into smaller functions, but as long as it's a single block, moving it to another file does not help much. So I'll leave it at that for the moment.

Feel free to close the issue if you want.