How can i beautify my code in Matlab (tabs, deleting unnecessary spaces etc.)? [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
i'm looking to find a way to beautify my code in MATLAB. I'm talking about tabs, deleting unnecessary spaces etc., the way Eclipse does it with Ctrl+Shift+F

The smart indentation (ctrl + I) is probably all you need (as #Matteo V and #Cris Luengo already mentioned).
However, there are a few other neat tricks that you might want to have a look at if you are really into code development:
Well, first have a look at the Improve Code Readability site of MATLAB. You could use the Apply smart indenting while typing option in the Preferences > MATLAB > Editor/Debugger > Language > Indenting section (it should be turned on per default but I like the Indent all functions setting). There are a bunch of other settings that you may want to explore
If you dig yourself deeper in the MATLAB IDE, you will notice that you can adjust almost everything to your preferences, but the way is not always documented in the web.... however, the local documentation (call doc) contains the info you may be looking for, see this blog-post
I am not aware of an automatic detection of double spaces or similar but you might end up writing your own little callback-function. Most languages ignore this anyway (perhaps except for Python). Code readability is usually a topic that the programmer(s) should care about... and not the machines ;)
Further tips:
respect the Right-hand text limit, which is the vertical gray line in your editor and shall indicate how many characters a single line of code should have as maximum. If it is a comment, wrap it. If it is an expression, try to outsource some commands to a dedicated variable
use equally long variable names. (There is no style guide as in Python, which says that you should use normal words and underscores etc) E.g. if you have two variables describing a commanded and a measured velocity, you could call them v_cmd and v_act and your code perfectly aligns if you apply the same manipulations to both variables ;)
use section. With %% (the space is important) at the beginning of a line in the editor, you create a section (you'll note the slight yellow background color and the bold writing that follows this command). It is convenient to structure your code. You can even run entire sections Editor-Tab > Run > Run section
Although there are programmers claiming that a good code speaks for itself (and therefore doesn't need any comments), to my experience writing comments has never been a bad idea. It improves the readability of your code
The answer might have been a bit elaborate for such an innocent question ... oO

Related

Netbeans 7.4 introduces "10 lines max" per method rule. Where does this rule come from? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
NetBeans 7.4 beta is currently available for public download, and it introduces a weird warning rule by default:
Method length is 16 lines (10 allowed)
My question is: Is this an accepted code convention rule, that can be proven somehow, somewhere ? NetBeans support/devs say it's not a bug, but don't give a statement why they only allow 10 lines, and where exactly this rule has its origin.
You can change the maximum method/function length warning behavior in NetBeans options (it's under Tools->Options->Editor, in the Hints tab, under "Too Many Lines" section in the checkboxes list).
There you can set the maximum number of lines, how you would like to be warned, etc.
I believe that the rule was introduced by NetBeans developers because when working in teams, the automated tools that QAs use to "inspect" code flag long method declarations/functions bodies. Unfortunately, the use of automated tools by "code analysts" is on the rise, whilst their understanding of the reasons behind that are still limited. I do not say that your functions should be hundreds of lines long - that's just plain wrong, but a hard-coded number as a coding law - come on!
The "10 lines rule" has to do with enforcing test-driven development. The theory is that any method that has more than ten lines can be better broken down into units that are testable. it holds up in theory, but in practice a warning like this is more annoying than helpful.
I think there is not a convention about that, and it's very hard to make small functions in particular working in big projects.
I feel that the problem in NetBeans (or the rule) is counting lines with just one bracket or documentation.
This article gives him opinion about write functions with 5-15 lines.
I always disable this warning, as well as the warning about too many nested blocks. I understand the idea around not having large methods but a LOT of the time it's just not practical, and as someone else mentioned if you keep splitting your code into arbitrary functions just to appease the IDE you end up with spaghetti code jumping all over the place, refactoring becomes a huge problem later on as well.
Same as the line length limit warning, maybe a line 50 characters long made you scroll sideways in 1985, but today we have larger monitors (in color now as well!). I've seen people mutilate a line of code by shortening variable names so that it fits within the limit, turning a perfectly readable line of code into an indecipherable mess just so it fits within the limit.
Personally I think those three rules together have caused more garbage spaghetti code than helped create readable / testable code.
I think there is no such rule. I always thought a good convention would be no more lines of code in a class than one can read without scrolling. 10 lines seems not very much for me but in general it's for overview purposes and easier testing..

Inputting latex without RSI [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I can't type latex on my laptop without sore hands. I use latex in org-mode to write math notes and my right pinky is always stretching for _, {, }, and \.
Does anyone advice for typing large amounts of latex? Alternative input methods or touch typing tips are welcome, as are obvious tips or alternatives. I can't understand how people do it.
Easing RSI is basically about
typing as little as possible,
typing as few commands that are trying on your hands as possible, and
taking pauses so that your hands may rest.
The first point can obviously be taken care of by not writing when you don't have to. Technically the first point can be taken care of by reducing unnecessary input, e.g. by constructing your LaTeX code so that you have less redundancies, but overly doing this may obfuscate your code (you want it readable). Another way to technically take care of the first point is by using different tools such as the features of an editor or by configuring your environment. The second point may technically be taken care of the same way as the first. The third point may obviously taken care of by taking pauses which may be enforced by tools such as those in https://superuser.com/questions/305940/linux-software-for-periodical-work-break/306276#306276 but this point does not seem relevant if you're taking lectures notes or similar. Thus, I'll focus on technical solution to the first and the second point.
Snippets manager
To ease writing construct that you often write you could use a snippets manager/template system such as yasnippet. With such a solution you can create your own snippets to be more efficient and nicer to your fingers. For example you can make a snippet such that if you write align and press Tab it prints
\begin{align}
\end{align}
and places the cursor inside the environment.
AUCTeX's LaTeX-math-mode
Math symbols are often cumbersome to write because many of them starts with \. AUCTeX comes with functionality that makes it easier to type math symbols, see http://www.gnu.org/s/auctex/manual/auctex/Mathematics.html. To use it you press the key that calls (LaTeX-math-mode) (the default key is ` but that may not be optimal for your keyboard, so give it a good bind) and a defined key to write a math symbol, e.g. if you type ` and then f it prints \phi. Here's a reference for the default bindings ftp://ftp.gnu.org/gnu/auctex/11.82-extra/tex-ref.pdf.
Remapping
You could remap your keys so that the commands that hurts for you to type gets easier to type. If you're on Linux remapping can be done by xmodmap and XKB and you can also remap the keys internally in Emacs. For details on remapping specific to LaTeX see the following excellent answer https://tex.stackexchange.com/questions/1979/good-keyboard-layouts-for-typing-latex/1985#1985 (there's also a blog post introducing it http://tex.blogoverflow.com/2011/08/great-questions-i-save-your-fingers/) and note that the other answers to the linked question might interest you.
Auctex provides much better support for writing LaTeX than org-mode (as I recall, haven't used org-mode much). The most frequent LaTeX constructs that require _, {, } and \ are provided via more convenient keybindings: C-c C-e to add an environment, C-c C-m to add a macro etc. It also includes a whole suite of math-mode shortcuts too, so once you're familiar with it you'll be able to stay in "home position" more.
You can have the best of both - just bind a key to switch from org-mode to auctex
if you want to use org-mode most of the time, and take advantage of Auctex only when adding specific LaTeX code.
You could try changing your keyboard layout. The qwerty layout was not designed to be ergonomic. It was designed to prevent the metal arms in typewriters from jamming, something that shouldn't be a problem on your laptop I think.
Dvorak is one possibility, where the characters are rearranged, so that the characters you type most often are on the home row, where little finger movement is needed.
Neo is another layout, which not only rearranges the characters. It also introduces two new modifier keys. Those modifiers make it possible to put all the characters you mention in good positions. One key used as a modifier is caps lock, so CAPS-a becomes \, CAPS-d {, CAPS-f } and CAPS-e _. Here is an image.
Neo is a German layout, so the first two layers (lowercase and uppercase characters) are not good for English text, but you can download the xmodmap and just use the third layer for "programming characters".

Syntax Highlighting Pager [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Right now, I use most as my pager. While it has helpful syntax highlighting for man pages, it lacks colored syntax highlighting for anything else (I'm specifically looking for diff/C++).
Meanwhile, pygments is a wonderful program. I can easily make colorized output with it:
# ./pygmentize -f console256 ${file}
hg diff | ./pygmentize -f console256 -l diff
Now, I would like to be able to page the output, so I just use:
# ./pygmentize -f console256 ${file} | most
hg diff | ./pygmentize -f console256 -l diff | most
At this point, most dumps all the colorizing control characters to my screen like so:
^[[38;5;28;01mclass^[[39;00m ^[[38;5;21;01mheap_allocator^[[39;00m
{
^[[38;5;28;01mpublic^[[39;00m^[[38;5;241m:^[[39m
This is, of course, unreadable. I looked though the man page for most, but I couldn't find any "Hey, show those control characters as colors instead of printing them" options. less has the same garbage behavior as most, but more shows the colors perfectly fine, with the obvious limitations of being more.
Is there a pager that supports syntax highlighting or some crazy combination of parameters and programs I can string together to make this work? Ultimately, I would like to get diffs and logs from Mercurial to be highlighted, so maybe there is a shortcut in there...
Might I suggest vimpager?
First off, recent vim distributions (I believe 6.0 and above) come with a pager-esque-mode script. It's quite simple and functional, and operates similarly to less. Try: vim '+help less' +only.
Even better, however, Rafael Kitover has written a much more robust and powerful script called vimpager. It's available on GitHub (or vimscripts). If you are on OS X and using Homebrew, it's as easy as brew install vimpager.
At that point, you can simply set $PAGER=vimpager, or even alias less=vimpager. It works excellently.
less -R shows ANSI color sequences as-is (instead of expanding to caret notation). That'll make syntax highlighting work!
You can also create an environment variable LESS=-R to make this the default behavior. Similarly for other options; see man less.
Look for bat: A cat(1) clone with wings.
bat supports syntax highlighting for a large number of programming and markup languages.
It is not a pager, but it automatically redirects output to less if needed.
You might try using jed. Yes, it's a text editor, not a pager, but it's quite lightweight and the default install contains excellent colorschemes for a wide variety of file types and languages.
Jed has syntax highlighting modes for different languages, simillar to the emacs ones. For example if you piped a C program to it, you can turn on the highlighting by pressing 'ESC', then 'x', then typing 'c-mode' . If it is a php program - change the last part to 'php-mode' and so on ...

syntax aware diff tools? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Are there any (ideally GUI) diff tools that are aware of syntax?
As an example of the kind of thing I'm looking for, I keep finding that my current tool miss aligns repetitive code:
Foo = { 'hello': 'world', | Foo = { 'hello': 'world',
'goodnight': 'moon' | 'goodnight': 'moon'
} <
<
Bar = { 'picture': 1000, <
} | }
I'd like a tool that would try and make matching braces on one side align with matching braces on the other.
Edit: I'm looking for a tool that can automatically spot that condition and correct it's alignment.
Not GUI based, but completely syntax driven: my company's Smart Differencer tools for many langauges (incluiding Java, C#, C++, PHP, Python, JavaScript, HTML, XML, COBOL,...)
The Smart Differencer parses the source text like the compiler, so it understands that language syntax and structures the way the compiler does. It compares these structures (using ASTs) to determine the "least edit distance" in terms of edit actions (move, copy, delete, replace, rename-identifier-in-block) on these structures and report them.
In your example, it would know the curly brace on the right belongs to foo, not bar, and would tell you that the entire statement involving bar was simply deleted.
The output style is diff-like if you want to feed the result to another tool, or more human readable if you want to examine it directly.
Beyond Compare will let you realign lines that it mismatches. I generally have good luck with it matching lines properly.
I think Beyond Compare has what you are looking for
http://www.scootersoftware.com/index.php
I like Source Gear's DiffMerge.
The Pretty Diff tool is syntax aware automatically. The process it uses is to minify code in order to remove comments and then beautify the code so as to normalize formatting and white space. It only supports a few web client-side languages though. This tool works on the client side so you can test it out in your browser and there is a sample page containing some demos.

What is the best code template facility for Emacs? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Particularly, what is the best snippets package out there?
Features:
easy to define new snippets (plain text, custom input with defaults)
simple navigation between predefined positions in the snippet
multiple insertion of the same custom input
accepts currently selected text as a custom input
cross-platform (Windows, Linux)
dynamically evaluated expressions (embedded code) written in a concise programming language (Perl, Python, Ruby are preferred)
nicely coexists with others packages in Emacs
Example of code template, a simple for loop in C:
for (int i = 0; i < %N%; ++i) {
_
}
It is a lot of typing for such common code. I want to invoke a code template or snippet which inserts
that boilerplate code for me. Additionally it stops (on TAB or other keystroke) at %N% (my input replaces it) and final position of the cursor is _.
TextMate's snippets is the most closest match but it is not a cross-platform solution and not for Emacs.
The second closest thing is YASnippet (screencast shows the main capabilities). But it interferes with hippie-expand package in my setup and the embedded language is EmacsLisp which I'm not comfortable with outside .emacs.
EDIT: Posted my answer here to allow voting on YASnippet.
Personally, I've been using Dmacro for years (ftp://ftp.sgi.com/other/dmacro/dmacro.tar.gz).
Here's a review of it that also mentions some alternatives: http://linuxgazette.net/issue39/marsden.html
The EmacsWiki has a page of template engines.
Of these, I've used tempo in the (distant) past to add table support to html-helper-mode, but don't know how it has progressed in the last 15 years.
I'd add my vote for tempo snippets ... easy to setup, powerful (you can run arbitrary elisp in your template - so that you can downcase things, lookup filenames & classes, count things, etc), set the indentation, integrate with abbrevs ... I use it a lot ;)
I vote for http://cedet.sourceforge.net/srecode.shtml
It has very clean syntax and has access to code environment through Semantic.
Also it is a part of a large well supported CEDET distribution (which was built into Emacs for 24.x version series).
UPDATE YASnippet is also a powerful template engine. But it uses an ugly file naming schema (your file name === template name) for you can't put several templates into a single file and have issues with national character sets...
You can try a lightweight solution muban.el
It is written completely in Elisp and has a very simple syntax.