Home Backend Development PHP Tutorial 对于Java开发人员必须遵从的十大基本法则_PHP

对于Java开发人员必须遵从的十大基本法则_PHP

Jun 01, 2016 pm 12:21 PM

对Java开发者来说,有许多的标准和最佳实践。本文列举了每一个开发人员必须遵从的十大基本法则;如果有了可以遵从的规则而不遵从,那么将导致的是十分悲惨的结局。

1.在你的代码里加入注释

每个人都知道这点,但不知何故忘记了遵守。算一算有多少次你“忘记”了添加注释?这是事实:注释对程序在功能上没有实质的贡献。但是,你需要一次又一次的回到你两个礼拜之前写的代码上来,可能一辈子都是这样,你一定记不住这些代码为什么会这样。如果这些代码是你的,你还比较的幸运。因为它有可能让你回忆起。但是不幸的是,很多时间,这些代码是别人的,而且很有可能他已经离开了公司。

2.不要让事情复杂化

我以前就这么干过,而且我相信所有的人都这么干过。开发人员常常为一个简单的问题而提出一个解决方案。我们为仅仅只有5个用户的应用而引入EJBs.我们为一个应用使用框架而它根本不需要。我们加入属性文件,面向对象的解决方案,和线程到应用中,但是它根本不需要这些。为什么我们这样做?我们中的一些人是因为不知道怎么做更好,但是还有一些人这样做的目的是为了学习新的知识,从而使得这个应用对于我们自己来说做得比较有趣。

3.牢牢记住“少即是多(less is more)”并不永远是好的

代码的效率是一伟大的事情,但是在很多情况下,写更少的代码行并不能提高该代码的效率。请让我向你展示一个简单的例子。

以下为引用的内容:
if(newStatusCode.equals("SD") && (sellOffDate == null ||

    todayDate.compareTo(sellOffDate)
    todayDate.compareTo(lastUsedDate)>0)) ||

    (newStatusCode.equals("OBS") && (OBSDate == null ||

    todayDate.compareTo(OBSDate)
    newStatusCode = "NYP";

    }

我想问一句:说出上面的那段代码的if条件想干什么容易吗?现在,我们再来假设无论是谁写出这段代码,而没有遵从第一条规则??在你的代码里加入注释。

如果我们把这个条件分到两个独立的if陈述句中,难道不是更简单一些吗?现在,考虑下面的修正代码:

 

以下为引用的内容:
if(newStatusCode.equals("SD") && (sellOffDate == null ||

    todayDate.compareTo(sellOffDate)
    todayDate.compareTo(lastUsedDate)>0))){

    newStatusCode = "NYP";

    }else

    if(newStatusCode.equals("OBS") && (OBSDate == null ||

    todayDate.compareTo(OBSDate)
    {

    newStatusCode = "NYP";

    }

 

难道它不是有了更好的可读性?是的,我们重复了陈述条件。是的,我们多出了一个多余的“IF”和两对多余的括弧。但是代码有了更好的可读性和可理解性。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles