Using static classes over packages

조회 수: 13 (최근 30일)
Eugene Ofori
Eugene Ofori 2022년 7월 17일
편집: Eugene Ofori 2023년 12월 12일
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