Introduction to various program code beautification tools

Source: Internet
Author: User
Tags beautifier coding standards tidy checkstyle
C Java PHP Perl Python Program Code Beautification tools

Author: chelong published on: Last updated on:
Copyright statement: You can reprint it at will. Please mark it as a hyperlink during reprinting.ArticleSource and author information and this statement.
Http://www.chedong.com/tech/indent_tools.html

Summary:
The purpose of writing this article is nothing more than two:

    1. If the indentation of the former programmer is very untidy, the reading speed of the current maintainer's code will be greatly affected.
    2. The new project code (no matter which editing environment is used) can adapt to the company's indentation specifications and facilitate communication between developers.

If you can find what you need from the following list immediately, you don't have to be patient. Finally, the document in the corresponding toolkit will provide more detailed instructions for use.

 
C, C ++ C #=> indent astyle

Java ==> astyle jalopy jacbe importscrbber

PHP ==> phpcodebeautifier

Perl ==> perltidy

Python ==> pydent

ASP ==> vbsbeaut

Programmers should be good at communication. The significance of code beautification is to reflect the developers' respect for others. Using good coding conventions can makeSource codeUnderstanding, easy to read, accurate, more intuitive and consistent with other language conventions, so that programmers can better communicate.

Statistics show that code with good indentation can improve code reading efficiency by more than 40%. Another statistics shows that more than 70% of software development efforts maintain previous code, therefore, beautifying the format of previous code is often the first step in refactoring. The use of the code beautification tool (pretty print program) can make the basic code indentation standard a system that is easier to execute.

In addition, if you are the creator of the department code specification, you should also understand that programmers are not machines. It is almost impossible to write code without making code indentation errors. Tools-based batch formatting and sorting tools facilitate the configuration import/export mechanism to develop a unified indentation specification is a truly operable system.

Introduction to several open-source code beautification tools

Tool Name Applicable Language Introduction Installation/Use
Indent C Indent is synonymous with code beautification tools Indent is a standard tool attached to GCC,
Indent [Options] [input-files]
Indent [Options] [Single-Input-file] [-O output-file]
Perltidy Perl Perltidy is also written in Perl. Download: Perl makefile. pl; Make; make install
Perltidy [Options] file1 file2 file3...
(Output goes to file1.tdy, file2.tdy, file3.tdy ,...)
Perltidy [Options] file1-O OUTFILE
Perltidy [Options] file1-ST> OUTFILE
Perltidy [Options] <infile> OUTFILE
Astyle C ++ Java (PHP) A very fast C/C ++/Java source code beautification tool.
Astyle has many complete set of style definitions better than indent: ANSI Java Linux... you don't have to remember the complex indentation specific options.
Download the source code, decompress the package, make, and generate the astyle executable file.
Astyle [Options] <original> beautified
Astyle [Options] Foo. cpp bar. cpp [...]
Astyle -- style = ANSI *. cpp
I tried to use it to format the PHP program, which is also very effective (of course, it is not a pure PHP code mixed with HTML code ).
Jalopy Java Powerful Java code formatting tool. In addition to the standard interface, the command line tool can also be used as a plug-in for ant JBuilder eclipse jdeveloper and other tools, and provides APIs Download from http://prdownloads.sourceforge.net/jalopy/
See the relevant installation documents.
Pydent Python Pythius contains two tools:
Pydent: Code indent Tool
Pystat: Code statistics Tool
After the source code is downloaded and unwrapped:
Run "Python setup. py build"
Run "Python setup. py install"
Htmltidy Html/XML The error correction tool for HTML code can help your HTML code better comply with W3C specifications. It is now called tidy because it is not only used for HTML, but also for xhtml xml formatting.

However, JSP is not suitable for the code beautification of embedded scripts such as asp php jsp.

After the source code is downloaded, make generates the tidy executable file:
Tidy file1 file2...
Note: Use the-raw option for pages with Chinese characters

Tidy.exe-raw-imuq-wrap 132-F % F. Err % F option description:
-Raw: do not modify Chinese characters (output values above 127 without conversion to entities)
-I indend: by default, HTML is Indented by two spaces.
-M overwrites the original file
-U forces all HTML tags to be capitalized (this can be left blank)
-Wrap Page code with a width greater than 132 lines force line feed
-F % F. Err: output the error to the "corresponding file name. Err" file.
Htmltidy supports XML format beautification:
Tidy-XML-imq web. xml
Tidy-XML-imq build. xml

Other tools:

PHP code beautification Tool
Phpcb: PHP code beautifier has the command line version and graphic interface code viewer.
Http://www.phpedit.net/products/phpCodeBeautifier/

Java code beautification tools
For Java code formatting, jacbe is also a good choice. By default, it contains a configuration file that is fully formatted according to Sun's code specifications. There are even Commercial Code Organization tools such as jindent to achieve better results.

Importscrbber: the sorting tool declared by Java import. It can convert a reference such as import java. util. * into a single reference and help delete a reference that is no longer needed. Http://importscrubber.sourceforge.net/

To introduce these tools, you can find: jacbe jindent jxbeautifier...
Http://directory.google.com/Top/Computers/Programming/Languages/Java/Development_Tools/Code_Beautifiers? IL = 1

Use the command line tool to modify code in batches

In Windows 2000, use the for command to implement directory traversal, file filtering, and command execution. For example:
For/R % F in (*. Java) Do astyle -- style = Java % F
For/R % F in (*. htm *) Do tidy-raw-imq-wrap 132-F % F. Err % F
For/R % F in (*. asp) Do vbsbeaut.exe-I-S4 % F

Linux: Execute traversal with find-exec, for example:
Find./-name *. Java-exec astyle -- style = ANSI {}\;

Therefore, do not be confused by the beautiful graphic interfaces of many commercial software. When faced with thousands of source files, batchcompute Based on command lines is also a very important feature.

Use tool plug-ins for eclipse and other IDE development environments

More and more IDE development environments contain code beautification functions, but some are still not as powerful as these specialized tools. If some developers use eclipse, some are loyal users of JBuilder, how can they easily comply with code specifications? The answer is to use all jalopy plug-ins for these mainstream IDE development environments.
Jalopy: Java code beautification tools: http://jalopy.sourceforge.net/
Eclipse plug-in: http://jalopy.sourceforge.net/plugin-eclipse.html
Checkstyle: Java code specification (indentation, naming) Check Tool http://checkstyle.sourceforge.net/
Eclipse plug-in: Eclipse checkstyle plug-in

Installation:
It is very convenient to install the plug-in as Eclipse. After downloading the plug-in, unpack it to the eclipse/plug-in/directory and restart Eclipse:

Configuration:
Jalopy: Windows ==> import/export the jalopy preference configuration in the general menu,
Checkstyle: Windows ==> perference ==> import/export of checkstyle configuration is on the right of the Configuration Interface
The import/export function of standard configuration can greatly reduce the learning time of developers for the above tools. For most developers, it is not necessary to have a detailed understanding of all the option configurations, you only need to know how to import the standard configuration. My common jalopy configuration files

Usage:
Jalopy: in addition to the original format option, the format with jalopy option is displayed in the right-click menu on the code editing interface, the source => orgenize Imports Provided By eclipse can achieve the same effect as imports srubber.
Checkstyle is a very complex code style check process: including indentation, naming conventions, etc. Therefore, the default value is disable. To start the checkstyle for a project, you need: project Properties ==> checkstyle ==> enable.

All of the above tools include ant extensions, which can be easily added to the ant script for Automatic Code Sorting/statistics.

References:

GNU coding standards
Http://www.gnu.org/prep/standards_toc.html

Code conventions for the Java (TM) Programming Language

Http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

PHP coding standard
Http://alltasks.net/code/php_coding_standard.html

Perl Style
Http://www.perldoc.com/perl5.6.1/pod/perlstyle.html

Pb: PERL/JavaScript beautifier
Http://www.arachnoid.com/lutusp/ftp/cgi/pb.txt

Html xml xhtml css...
Http://www.w3c.org/

C-C + + beautifier how-:
Http://www.tldp.org/HOWTO/C-C++Beautifier-HOWTO/

VBScript code specification
Http://msdn.microsoft.com/library/en-us/script56/html/vbsCodingConventions.asp

Vbsbeaut code sorting Tool
Http://www.daansystems.com/vbsbeaut/

Dependency analysis between Java components
Http://www.clarkware.com/software/JDepend.html

Code check
Http://www.thecortex.net/clover/index.html

More XP tools
Http://directory.google.com/Top/Computers/Programming/Languages/Java/Coding_Standards/
Http://www.xprogramming.com/software.htm
Http://www.qsm.com/CodeCounters.html

Appendix: Why is htmltidy unsuitable for reformatting HTML embedded scripting languages such as jsp php asp?

Some unresolvable errors are actually caused by code irregularities.

    1. "<" In Javascript"

      For example
      For (I = 1; I <A; I ++)
      Change
      For (I = 1; I <A; I ++)
      ^ "<" Requires a space before and after it; otherwise, htmltidy regards <A as an HTML Tag.
    2. The HTML attribute contains the JSP Tag:
      Tidy output for common JSP attributes can be recognized.

      For example: <input type = text size = "9" name = "alias" maxlength = 20 value = <% = infoform. getalias () %>
      But code like this:
      <% = infoform. getphoto () %> width = 120>
      Because the start of the attribute is not <%, the IMG does not end with an error.
    3. Htmltidy completely complements the code by default. In fact, many HTML code is contained in the file. Htmltidy also has some problems in Chinese encoding recognition (currently only big5 is supported)

Cause:

Why is it difficult to maintain embedded code? After all, the non-standard HTML syntax checker is much more complicated than a language parser. For example, for such code:
<? PHP echo "<Table>" ;?>
</Table>

In this form of code, beautification tools must understand both HTML and PHP syntax. Therefore, most of the formatting tools are for one language or for formatting the program part of the embedded script, or, only HTML-based formatting is supported. There are only a few large development tools such as virsual age and forte that contain JSP code beautification, and a large commercial text editor such as slickedit.

Solution: For the formatting of HTML parts of jsp asp and other files containing programs, it is best to use a specialized HTML editing tool Dreamweaver (≥4) and FrontPage (≥2000). For example, for FrontPage, select "Use the following rules to reformat the source code" from the menu tool ==> webpage options ==> HTML source code menu ".

In short, the embedded scripting language that uses HTML and programs together is a very bad habit of development. In the long run, using XML, XSLT, and other technologies to separate data, presentation, and logic is a more suitable mode for large-scale development.

For the current large number of asp php jsp applications, I still found some tools on Google and believe in homesite. In fact, the vbsbeautifier mentioned above actually calls Perl 5.8 on Windows.

A code beautification tool for ASP: vbsbeaut
Http://www.daansystems.com/vbsbeaut/

A tool for editing asp jsp php with code beautification: bphtml

Http://www.bphtml.com/

A Java-based HTML editor, Embedded C ++ Java JavaScript Perl Class C language code beautify: http://www.arachnoid.com/arachnophilia/index.html

Beautifier highlights and indents source code using highlight configuration files (which are similar to ultraedit highlighting files ). as such, it supports C, C #, Java, Perl, PHP, Pascal, lisp, mumps, Eiffel, euphoria, and x86 explorer, amongst others.
Http://www.beautifier.org/

Trita is a source code beautifier which learns your personal formatting style by examining examples of your code.
Http://www.trita.com/
Supported: Java, javscript, CSS, JSP, HTML, ASP, lisp, Delphi, C #, C ++, PHP, Perl, Python, and SQL.

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.