Vim consolidated Astyle for code beautification

Source: Internet
Author: User

1, astyle Format code

Install Astyle and add the bin directory to the environment variable in VIMRC add the following setup code

"Call the Astyle program, do the code beautification
func codeformat ()
          " Get the current cursor at the line number let
          linenum = lines (".")
          " C source program
          if &filetype = = ' C '
                    executes the command calling external program
                    exec '%! Astyle-a3lfpjk3ns\<cr> "
          " H header file (file type recognition as CPP), CPP source program
          ElseIf &filetype = = ' cpp '
                    "execute command to call external program
                    Exec "%! Astyle-a3lfpjk3ns\<cr> "
          " Java source program
          ElseIf &filetype = = ' java '
                    "executes the command calling external program
                    exec"%! Astyle-a2lfpjk3ns\<cr> "
          Else 
                    " prompt message
                    echo "does not support". &filetype. " The file type. "
          endif
          " returns the previous cursor on the line
          exec linenum
endfunc
"Mapping code beautification function to shift+f shortcut
map <S-F> < Esc>:call Codeformat () <CR>
2 Sourcemonitor: Identify bad taste
After solving the code style, whether it can be a beautiful code to do it.
To know the beautiful clothes to wear to the fat body, also beautiful not where to go (Amen, forgive me). For code, code style is just the first step, and beautiful code is easy to understand. Easy to understand, a single function of the line of code can not be too long, nesting layer can not be too many, the branch conditions can not be judged too much. These jobs are not code-style solutions. We need tool-sourcemonitor that can automatically check the quality of the code.
Sourcemonitor is a code quality check software. Can check the number of functions in the file, the number of lines per function, the annotation ratio, the function of the call depth, cyclomatic complexity, and so on.
The most important of these is the complexity of each function loop and the number of lines of code for each function. Cyclomatic complexity refers to the function can be carried out independently of the path, so the function of each occurrence of a if/else/while,switch/case/break, the cyclomatic complexity is added 1. The higher the cyclomatic complexity, the more complex the path that can be executed in the function. If you exceed a certain value (15 or 10), consider whether you can refactor the function. Furthermore, the number of lines of code for a function that is too long to be displayed on a screen is not easy to remember and understand, and the function needs to be refined.


3 semi-automatic reconfiguration of Eclipse
Using Sourcemonitor to find the functions that need to be refactored or refined, do I have to refactor them manually, or do I have refactoring tools? Fully automatic currently has not seen, if a classmate to see, please leave a message. In comparison, semi-automatic refactoring tools are more, Eclipse has a refactoring menu, providing function extraction, variable or method renaming and so on a number of basic refactoring functions.

Eclipse's semi-automatic refactoring reduces the amount of effort that developers expend by reducing human refactoring, which is why I am more and more fond of eclipse as a C/s + + programmer.

4, Visualstdio can use STYLECOP for code review

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.