Using static classes over packages
4 views (last 30 days)
Show older comments
I'm planning to make a library of code for my team and I'm trying to plan how I structure this library. The fact that MATLAB only allows one function per file is very frustrating to me. Does anyone use static classes over packages to try and keep a cleaner and more readable code? I want to avoid having tonnes of function files, with some having as little as 10 lines. The main reason is to make this readable and prevent having to navigate back and forth between files as much as possible. I think that will encourage people to use this library. Is this a bad idea and I should stick to the packages format or is using static classes not a bad way to go?
2 Comments
Philip
on 11 Dec 2023
I am curious about how you decided to proceed? I have the exact same question. As an experienced python programmer it seems overly restrictive and unnartural for matlab code to have only one public function per file. So after learned that I can use static functions in a matlab classes, I immediately thought that would be a "better way" to approximate python modules in matlab. But I do not see much code in python that does this, so I am wondering if I should continue down this path.
Accepted Answer
Matt J
on 17 Jul 2022
Edited: Matt J
on 17 Jul 2022
I don't think static classes have any pitfalls, but I'm not sure I agree that it is "cleaner". Why is having 10 functions in a file better than having 10 files in a folder? The latter has the advantage that the functions can be opened and navigated separately in the Matlab Editor.
3 Comments
Steven Lord
on 18 Jul 2022
You can have multiple functions in a file in MATLAB. Only the main function will be directly callable by functions outside that file, but if you have helper functions that are only usable by one function you could write them as local functions in that main function's file.
Alternately if you're willing to have your main function "cooperate" it could return function handles to the local functions when it is called, thus allowing you to indirectly (via the function handles) call the local functions.
If some of the functions are not intended to be called directly by users, but are only intended to be called by functions in your application, you could make them private functions in that directory.
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!