Table of Contents
1. Follow the single responsibility principle
2. Minimize shared state
3. Localized side effects
4. Prefer immutable objects
5. Use more interfaces and less classes.
6. Apply good principles to modules
7. Avoid inheritance
8. Test as much as you design and develop
9. Prefer over hand-written standard library
10. Avoid writing new code
Home Backend Development PHP Tutorial 10 programming tips you must know

10 programming tips you must know

Sep 17, 2017 am 10:25 AM
learn Skill programming

Summary: How to type good code? Good code can be defined as easy to read, easy to understand, easy to debug, easy to change, and most importantly, has few defects. Obviously, it takes a lot of time to type good code, but this makes sense in the long run because you can spend less time and energy maintaining and reusing your code. This is a summary from an experienced programmer with 30 years of software experience.

Why do you knock out a good code?

Good code can be defined as easy to read, easy to understand, easy to debug, easy to change, and most importantly, has few defects. Obviously, it takes a lot of time to type good code, but this makes sense in the long run, because you can spend less time and energy maintaining and reusing your code.

In fact, we can equate good code with reusable code, which is also one of the important principles mentioned below. The code may only fulfill a specific function for a short-term goal in the programming work, but if no one (including yourself) is willing to reuse your code, the code can be said to be insufficient and defective in some way. Either it's too complex, or it's too specific, or it's very likely to break under different circumstances, or other programmers may not trust your code.

No matter your experience level, if you consistently apply the following tips to your code (including your experiments or prototypes), you will have good code at your fingertips.

1. Follow the single responsibility principle

Function is the single most important abstract form in the programmer's library. The more opportunities there are for reuse, the less code you have to write, and the more reliable that code is. Small functions that follow the single responsibility principle are more likely to be reused.

2. Minimize shared state

The implicit shared state between functions should be minimized, whether it is a file scope variable or a member field of an object, which is conducive to explicit Pass the desired value as a parameter. When it is clear that a function achieves the desired result, the code becomes easier to understand and reuse.

One conclusion can be drawn from this, you should prefer static stateless variables to member variables of objects.

3. Localized side effects

Ideal side effects (such as printing to the console, logging, changing global state, file system operations, etc.) should be placed in separate modules rather than scattered across throughout the code. Functional side effects often violate the single responsibility principle.

4. Prefer immutable objects

If the state of an object is set once in its constructor and not changed again, debugging becomes much easier because once constructed correctly Just stay valid. This is one of the easiest ways to reduce the complexity of your software project.

5. Use more interfaces and less classes.

Functions that accept interfaces (or template parameters or concepts in C++) are more reusable than functions that operate on classes.

6. Apply good principles to modules

Decompose software projects into smaller modules (such as libraries and applications) to achieve modular reuse. Some key principles for modules are:

  1. Minimize dependencies

  2. Every project should have a single well-defined functionality

  3. Don’t repeat

You should strive to keep your projects small and clear.

7. Avoid inheritance

In object-oriented programming, inheritance, especially virtual functions, is often a Achilles heel in terms of reusability. I've rarely had success using libraries that override classes.

8. Test as much as you design and develop

I'm not a big fan of test-driven development, but when you start writing test code, there are a lot of guidelines that naturally follow when writing tests. It also helps expose errors early. Avoid writing useless tests, good coding means that more advanced tests (for example, integration tests or functional tests in unit tests) are more effective in showing defects.

9. Prefer over hand-written standard library

I can't tell you how long it will take before you see a better version of std::vector or std::string, but it's almost always Waste of time and energy. Except for the obvious fact that you are introducing bugs into a new place. (See Tip 10) Other programmers are less likely to reuse your code than code that is widely understood, supported, and tested.

10. Avoid writing new code

The most important point is that every programmer should follow: "The best code is the code that isn't written" (The best code is code that never needs to be copied). The more code you have, the more defects there will be, and the harder it will be to find and fix bugs.

Before writing a line of code, ask yourself, is there a tool, function or library that already does what you need? Do you really need to implement this function yourself instead of calling another already existing function?
Summary
Programming is like an art form or a sport. Only through continuous practice and continuous learning from others can you continuously improve the quality of your code, which will help you become more efficient. programmer.

The above is the detailed content of 10 programming tips you must know. For more information, please follow other related articles on the PHP Chinese website!

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 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)

Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

Instead of fighting, play ball! Introduction to the gameplay of the new mode of 'Eternal Tribulation': Hot-Blooded Fighting Ball Instead of fighting, play ball! Introduction to the gameplay of the new mode of 'Eternal Tribulation': Hot-Blooded Fighting Ball Apr 11, 2024 pm 01:52 PM

Tired of playing ranked? Then come and play ball! Teammates who cooperate tacitly can give opponents a taste of surprise! In the ever-changing battlefield, try to see how you can use your skills to break the situation with one move! "Eternal Tribulation" has launched a new team confrontation mode in the update on April 11. The following is an introduction to the "Eternal Tribulation" hot-blooded fighting ball activity. Overview of how to play the everlasting hot-blooded fighting ball: Use various means to bounce the fighting ball and defeat the enemy. Number of people: 9 people in single row, 6 people in three rows. How to play: Round: 1. The game is divided into multiple rounds. At the beginning of each round, Douqiu will be born in the center of the field and randomly select a player to chase. Players use various means to attack. The ball will speed up the fighting ball, and being hit by the fighting ball will deduct your stamina. 2. Each person in the single row has one point of physical strength each round, and each person in the three rows has one point of physical strength each round.

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

Use golang's error wrapping and unwinding mechanism for error handling Use golang's error wrapping and unwinding mechanism for error handling Apr 25, 2024 am 08:15 AM

Error handling in Go includes wrapping errors and unwrapping errors. Wrapping errors allows one error type to be wrapped with another, providing a richer context for the error. Expand errors and traverse the nested error chain to find the lowest-level error for easy debugging. By combining these two technologies, error conditions can be effectively handled, providing richer error context and better debugging capabilities.

See all articles