Home Backend Development PHP Tutorial 从代码风格窥视一个工程师的技术水平/PHP版

从代码风格窥视一个工程师的技术水平/PHP版

Jun 20, 2016 pm 12:39 PM

引语:代码风格,是一个长期以来养成的一些编写代码的习惯,其实并无绝对的好坏之分!但是,基本上可以这么说,有一个很好的代码风格的人,技术水平也不会太低;反之,没有好的代码风格的人,却一定是技术水平不怎么高的的人,哪怕是他已经有很多年的编程经验!因为,在业界有一个不成文的现象,那就是每个工程师都有一个追求技术提升,追求完美的梦!结果就是,随着个人技术水平的提高,风格也越来越成熟,而这个风格就体现着其个人水平!

本文以PHP的视角,说几个代码风格上的事,同理可推至其他语言,其他技术,甚至其他通用道理!

本文将以从面到线,再到点的过程,讲述一些个人心得。仅供大家娱乐参考,如有不对,请多多指教。如有雷同,不胜荣幸!

本文就以一个虚拟项目作为线索吧!

1、面:所谓面,就是面对一个项目摆在眼前,怎样去部署大方向的问题的思路!准确的说,这里并不是真正地体现一个代码风格,而是一个做事风格!

风格1:1. 我需要需求书,书上怎么写,我就怎么做;2. 如果有一些未知的技术,尽量让需求上做一些让步,以减少开发难度;3. 找个牛逼的人,带着自己或其他人一起做;4. 时间是多久?

风格2:1. 我需要需求书,书上写的东西,清楚吗?有什么可要可不要的东西,有副作用吗?3. 大概需要什么样的辅助工具,在哪里可能会得到这些?4.时间是多久?5. 后期可能会有什么样的扩展?

2、线:所谓线,可以理解为流水线吧。就是怎样去完成这么一个计划,怎样调动生产?

风格1:1. 设计数据库; 2. 选择代码框架;3、编码;

风格2:1. 设计服务架构;2. 设计数据库;3. 选择代码框架;4. 模块细化; 5.编码;

3、 点:所谓点,其实才这里说的真正的编码风格,将着重说明一些。

风格1:1、任性,随意而为,没有规则!

风格2:着重讲解!

3.1. 可以使用现有成熟框架,但是一定要有MVC的概念,基本要抛弃面向过程编程方式,面向对象!

3.2. 结合多种设计模式进行开发,如单例模式、工厂模式、抽象工厂、观察者模式,这些都是成熟的概念,都要尽量多用!

3.3. 类内部变量定义方式写法, public static function fun1(){} , 即以权限放第一位,变量修饰符放第二位;

3.4. 使用统一命名方式,要么使用$get_child,$bind_value下划线方式命名,要么使用$getChild,$bindValue驼峰式命名;

3.5. 多个参数之间有逗号分隔时,逗号后要留一个空格如fun($param1, $param2);

3.6. 避免使用global变量,尤其是有些不是公共初始化时产生的变量;

3.7. 避免函数内部include文件,因为这样很难找到真正起作用的方法,或者说方法混乱;

3.8. 使用自动加载方式,而非include方式;

3.9. 尽量使用include_once,require_once 避免一个文件被引入多次从而报错情况;

3.10. 对可能多次引用的全局变量,使用某类的静态方法进行获取,如ConfigClass::get('main', 'field'); 对于数据库一类连接,使用静态变量,保存首次连接时打开的连接,从而多处使用DB实例时,仍然不会重复实例化,如$db = ConfigClass::getDbInstance();

3.11. 多使用isset(), empty()函数进行判断空操作而非!$var, $var == null;

3.12. 对于使用两次以上方法,就应该去考虑提出到公用地方或者类中;

3.13. 数据查询先确认当前索引,配合写SQL,特别地方,一定加上注释,变量名尽量不要用简写;

3.14. 会使用文件锁,数据库锁,会使用缓存如memcache,redis,mongodb等;

3.15. 会适当使用事务;

...

好吧,晚了,洗洗睡吧。有空再补补。总之,一些小细节,总是要用经验堆砌才会体验!

其实,好与不好,大家已有定论,只是作个参考,罢了!

习惯,就好!

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles