Code optimization tools

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.
Http://www.chedong.com/tech/indent_tools.html

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

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

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.

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

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.