引语:代码风格,是一个长期以来养成的一些编写代码的习惯,其实并无绝对的好坏之分!但是,基本上可以这么说,有一个很好的代码风格的人,技术水平也不会太低;反之,没有好的代码风格的人,却一定是技术水平不怎么高的的人,哪怕是他已经有很多年的编程经验!因为,在业界有一个不成文的现象,那就是每个工程师都有一个追求技术提升,追求完美的梦!结果就是,随着个人技术水平的提高,风格也越来越成熟,而这个风格就体现着其个人水平!
本文以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. 会适当使用事务;
...
好吧,晚了,洗洗睡吧。有空再补补。总之,一些小细节,总是要用经验堆砌才会体验!
其实,好与不好,大家已有定论,只是作个参考,罢了!
习惯,就好!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)