[Reprinted] C Java PHP Perl Python program code beautification Tool

Source: Internet
Author: User
Tags beautifier coding standards tidy checkstyle

Copyright Disclaimer: You can reprint the document at will. During reprinting, you must mark the original source and author information of the article as hyperlinks and this statement.

Keywords: Pretty print indent perltidy pydent astyle htmltidy source code beautifier

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 Jacobe ImportScrbber

php ==> phpCodeBeautifier

perl ==> perlTidy

python ==> Pydent

ASP ==> vbsbeaut

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

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.

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...

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

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.

ForEclipseIDE development environment tools and plug-ins

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

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:

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

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.


GNU coding standards

Code conventions for the Java (TM) Programming Language

PHP coding standard

Perl Style

Pb: PERL/JavaScript beautifier

Html xml xhtml css...

C-C + + beautifier how-:

VBScript code specification

Vbsbeaut code sorting Tool

Dependency analysis between Java components

Code check

More XP tools

Appendix: Why is htmltidy not suitable for replaying?FormatJSP, PHP, ASP, and other HTML embedded scripting languages

Some unresolvable errors are actually caused by code irregularities.

  1. "<" In Javascript"

    For example
    For (I = 1; I <A; I ++)
    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 = "/oblog312/<% = 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)


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>" ;?>

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

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

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.

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

<A href = "http://www.chedong.com/tech/indent_tools.html"> http://www.chedong.com/tech/indent_tools.html </a>

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.