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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

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-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

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' =>

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.

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

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles