对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”和两对多余的括弧。但是代码有了更好的可读性和可理解性。

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
