Using static classes over packages

4 次查看(过去 30 天)
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 个评论
Philip
Philip 2023-12-11
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.
Eugene Ofori
Eugene Ofori 2023-12-12
编辑:Eugene Ofori 2023-12-12
The library is still a work in progress so I don't really have any lessons learnt yet. I think it depends on the usage. I'm still frequently using once off m file scripts rather than a library of code. I have tried static classes and they seem to work quite well. I haven't noticed any negatives. A few things I am/was worried about is:
  1. Performance? From this link below it look like it is just as good as m file functions post-2018a. https://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong/1745686#1745686
  2. Annoyingly calling functions inside the static class have to use the class name when calling other class functions, but the problem exists for packages as well.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-7-17
编辑:Matt J 2022-7-17
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 个评论
Steven Lord
Steven Lord 2022-7-18
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.
Eugene Ofori
Eugene Ofori 2022-7-18
Thank you, I was aware of being able to have helper functions, but not the use function handles. I probably won't take that path as it seems a bit messy, but it's nice to be aware of the capabilities. I will probably use the private function, functionality though, that seems very helpful.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by