Anyway to avoid the + and @ nasty-ness in MATLAB?

2 次查看(过去 30 天)
Hello,
I'm trying to organize a large codebase by using packages. Quite frankly, the + and @ namespace nonsense is infuriating. It makes navigating at the command line painful, + and @ are often reserved for other purposes in Windows and Linux, and the requirements for having classes nested in folders and requiring import specific members are just silly IMO.
Is there any way to avoid this? Any plans to change it? Or should I just give up on using namespaces for organizational purposes?
Regards,
Isaac
  2 个评论
John D'Errico
John D'Errico 2023-2-9
You don't show explicitly what it is you don't like. And of course, not everybody hates what you seem to dislike so terribly. Some surely use the feature and like it. Which just says if you don't like it, then don't use it.
Isaac Wolf
Isaac Wolf 2023-2-10
Hey John,
I gave 3 general examples; I can list more if you like. Some specific examples:
  • Command line autocomplete being ruined since all packages start with +
  • Regex, find, etc. tools all have @ and + as reserved characters, so you have to escape them
  • When I'm searching with word boundaries, I have to remember to add the +/@ to what I'm searching for
  • Additional nesting of the @ changes my hierarchy, and just makes things more complicated in general.
And in action:
People may like the idea of namespaces and packages (as they should), but I seriously doubt anyone likes the + and @ part of it. And if they did, I'd love to hear the reasoning.
It's aesthetically painful and functionally unnecessary. Which is probably why no other language in existence uses anything similar.
Isaac

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2023-2-10
Although I am not a Mathworks employee, I have enough experience with Mathworks to doubt that there are plans to change this (or at least plans that would be more to your liking.)
It is within possibility that autocomplete / code hints might be improved for packages and classes, but IMHO Mathworks is highly unlikely to rework the class and package systems specifically to avoid using regexp metacharacters. The few ASCII characters that are not alphanumeric and are not metacharacters in any context, tend to also be forbidden in filenames for major operating systems. For example colon : is not (that I recall) ever meta but in Windows colon is strictly reserved for drive letters and device names such as NUL:
  1 个评论
Walter Roberson
Walter Roberson 2023-2-11
I checked more carefully. The characters that are never meta in regexprep() are:
  • ~ (tilde) -- used a lot in Unix shells, so using ~ as an introducer would cause problems
  • % -- valid in filenames on Unix and Windows, but requires constant escaping when computing filenames with sprintf() and compose()
  • _ (underscore) -- better than many of the other possibilities, but is matched by \w so it is not really clean from a regexp perspective
  • ; (semicolon) -- command separator in Unix, used as separator by path
  • " (double-quote) -- illegal in Windows filenames, gets messy when constructing external command strings
  • ' (apostrophe) -- legal in filenames, but gets messy when constructing external command strings, and requires care in constructing character vectors in MATLAB.
The least bad of these is underscore. A single character. So if use with regexp becomes a primary driver, then Mathworks will need to encode both behaviours (classes and packages) into a single character.
Using regexp with packages already requires escaping period, so unless you think that Mathworks also needs to change the chain-of-periods naming convention for package elements to make it easier to use regexp, just call regexptranslate with the 'escape' option.

请先登录,再进行评论。


Jan
Jan 2023-2-9
移动:Jan 2023-2-9
Matlab is a programming language. Each language, also the spoken ones, have features, which can be likes or disliked. This is not the point, because languages are used to communicate, and in case of programming languages the goal is to solve a problem.
The + and @ name spaces are a feature of Matlab. They are part of the language and not "nonsense". Use them, if they help you to solve a problem, or don't use them, if not. You can also decide to use another language, if it is more efficient for solving your problem.
I do not see, why + and @ impedes the navigation in the command line, and in my Windows and Linux setups these characters are not reverved for another purpose.
"Is there any way to avoid this?" - Yes: Don't use classes and packages, but the old style folders. Or, of course, another programming language.
"Any plans to change it?" - I'm a Matlab user, not its designer. I do not assume, that this is subject to changes.
"Or should I just give up on using namespaces for organizational purposes?" - If these methods makes your life as programmer harder, avoid it. They are designed to be useful, but of course not every tool hits every problem.
  1 个评论
Isaac Wolf
Isaac Wolf 2023-2-10
Jan, thanks for the response, see above comment for issues at command line.
I know the alternative of old style folders, but that's resorting to the bad practice of "importing" an entire directory. That's currently what is being done and it causes issues, because it's a legacy codebase and there is duplicating naming and the like. It's also not clear to the developer what is availabe at a given time, and makes refactoring and understanding the program flow difficult. So turning to packages was a way to begin managing this.
As mentioned in my other comment, I don't think the +/@ part of it is a feature. Namespaces are the feature, and +/@ are unfortunate side effects.
And moving to another language is the long term goal.
Thanks for your time!
Isaac

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by