Using static classes over packages

5 visualizzazioni (ultimi 30 giorni)
Eugene Ofori
Eugene Ofori il 17 Lug 2022
Modificato: Eugene Ofori il 12 Dic 2023
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 Commenti
Philip
Philip il 11 Dic 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.
Eugene Ofori
Eugene Ofori il 12 Dic 2023
Modificato: Eugene Ofori il 12 Dic 2023
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.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 17 Lug 2022
Modificato: Matt J il 17 Lug 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 Commenti
Steven Lord
Steven Lord il 18 Lug 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.
Eugene Ofori
Eugene Ofori il 18 Lug 2022
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by