“Tell yourself every time you write code that the code will end up being maintained by a violent freak who knows where you live.” “– John Woods

Production code is the code in the product that the user uses. Once the software is deployed to production, it means that users can access it publicly. This article introduces 13 of the best programming principles to help you write code that can be deployed in production. These principles come from Python Programming without A Teacher, which has improved my code a lot since I read it.



By Cory Althoff

Python Programming without A Teacher — The Making of a Professional Programmer

0113 Best Programming Tips

Writing code is a last resort

As a software engineer, you should write as little code as possible while working. Your first thought when confronted with a problem shouldn’t be “How can I solve this problem?” your first thought should be “Has someone else solved this problem and can I use their solution?” If you solve a common problem yourself, there’s a good chance someone else already has a solution. Search online for solutions, and only do it yourself if you’re sure no one has solved the problem before.

DRY

DRY is short for Don’t Repeat Yourself, which means don’t write duplicate or essentially identical code in your programs. The correct approach is to wrap the code in functions that can be reused later.

orthogonality

Orthogonality is another important programming principle advocated and popularized in The Pragmatic Programmer. Hunter and Thomas argue that “the term has come to mean some kind of independence or decoupling. There is orthogonality between two or more things if their changes do not affect each other. In a well-designed system, the database code is orthogonal to the user interface; Tweaking the user interface doesn’t affect the database, and replacing the database doesn’t change the user interface.” In practice, keep in mind that “A should not affect B”. Given that we have two modules, Module_A and Module_B, module_A should not modify the contents of Module_B and vice versa. If you design A system in which A affects B and B affects C, it quickly gets out of control and the system becomes unmanageable.

Each piece of data should be kept in only one place

Let’s say we have a piece of data, and we just need to store it in one place. For example, if we are developing software to handle mobile phone numbers, we have two functions that use lists of area numbers. We want to make sure that there is only one list of area numbers in the program, rather than creating it repeatedly for each function. The correct approach is to create a global variable that holds the locale number. A better solution is to keep the information in a file or database.

The function only does one thing

Each function we write should only do one thing. If you find that the function is too long, check whether it is doing more than one task. There are many benefits to limiting functions to one task. First, the code is more readable because the function name directly describes what it does. Debugging is also easier if the code goes wrong, because each function is only responsible for a specific task, and we can quickly isolate and debug the problem functions. In the words of many prominent programmers, “software complexity comes mostly from trying to do two things at once.”

If it takes too long, you’re probably doing it wrong

If you’re not dealing with a very complex problem, such as big data, but your application is taking a long time to load, you’re probably doing it wrong.

Do it the best way the first time

While you’re programming, you might think, “I know there’s a better way to do this, but I’ve already started coding and I don’t want to go back and rewrite.” Then I suggest you stop coding and do it a better way.

Follow the convention

Learning the conventions of a new programming language can improve your speed at reading code written in that language. PEP8 is a series of highly recommended guides to writing Python code, available at www.python.org/dev/peps/ pep-0008/.

Use a powerful IDE

So far, we’ve been using IDLE, Python’s native IDE, to code. But IDLE is only one of many IDE options, and I don’t recommend using it for the long term because of its limited functionality. For example, if you open a Python project using a more powerful IDE, each Python file will have a different TAB. In IDLE, a new window is opened for each file, which is complicated to operate and difficult to switch back and forth between files.

I used an IDE called PyCharm, developed by JetBrains. They offer both the free version and the professional version, which has the following features to help us save time.

1. If you want to see the definition of a variable, function, or object, PyCharm provides a shortcut to the place where the variable, function, or object was defined (even if it’s another file). PyCharm also provides a shortcut to jump back to the start page.

2. PyCharm has the ability to save local history, which can greatly improve work efficiency. PyCharm saves a copy every time a project changes, so you can treat PyCharm as a native version of your version management system without pushing it to the code base. The user does not need to do anything, and the IDE saves automatically. Before I learned about this feature, I often solved a problem and wanted to change solutions, only to later want to roll back to the original solution. If I hadn’t pushed the original solution to Github, it would have been lost and had to be rewritten. But with this feature, we can go back 10 minutes and reload the status of the project. If you change your mind again, feel free to switch back and forth between alternatives.

3. You’ll probably have to copy and paste code a lot in the course of your daily work. In PyCharm, you don’t need to copy and paste, you just move the code on the current screen.

PyCharm supports version control systems such as Git and SVN. You can use Git directly in PyCharm without using the command line. The fewer times you switch between the IDE and the command line, the more productive you will be.

PyCharm provides the built-in command line and Python Shell.

PyCharm has a built-in debugger. A debugger is a program that interrupts code execution to see the effect of the code line by line. The debugger allows us to view the values of variables in different code.

log

Logging refers to the practice of recording data while software is running. We can use logs to assist in program debugging and better understand the state of the program when it is running. Python comes with a logging module that allows logging on the console or in a file.

When a program goes wrong, we don’t want to be unaware — we should record that information so we can check it later. Logging also helps in gathering and analyzing information. For example, you can set up a Web server to record data, including the date and time of each request received. We can log all the logs in a database, write programs that analyze the data, and generate graphs that show how many people visit the site.

“One of the things that separates great programmers from mediocre ones is that great programmers log, making it easier to debug when things go wrong,” blogger Henrik Warne wrote on his blog.

test

Program testing refers to checking that the program “meets design and development requirements, returns correct results for various inputs, takes acceptable time to perform functions, is available enough to be installed and run in the target environment, and achieves the results expected by interested parties.” To test programs, programmers write additional programs.

In a production environment, testing must be done. Programs that are scheduled to be deployed in production should be considered incomplete until tests are written. However, if it is a temporary program that will not be used again, testing can be a waste of time. If you are writing a program that others will also use, you should write tests. Many well-known programmers have said, “Untested code is buggy code.”

Code review

During code review, colleagues will read your code and provide feedback. Code reviews are recommended as often as possible, especially for self-taught programmers. Even if you follow all of the best practices listed in this chapter, it’s possible to go wrong. You need an experienced programmer to review your code and point out the mistakes so it can be fixed.

Code Review is a community of programmers focused on Code Review. Anyone can go to the site and submit a code. The rest of the community reviews the code and gives feedback on what went well and what could be improved.

security

For self-taught programmers, security is an easy problem to overlook. We rarely get asked about security in interviews, and we don’t think about it when we’re learning to program. In practice, however, we need to take direct responsibility for the security of our code. This section provides several suggestions for improving code security.

In this book we have learned how to use the sudo command to execute commands as root. Never use sudo at the command line if you don’t have to, because if a hacker breaks into the program, he or she will get root access. If you are a server administrator, you should also prohibit root from logging in. Every hacker will look at the root account, which is the primary target of choice when attacking the system.

Also, always assume that user input is malicious. Part of the malicious attack occurs by exploiting a vulnerability that accepts user input, so we code on the assumption that all user input is malicious.

Another strategy to improve code security is to minimize your Attack surface, which is the area where a hacker can extract data from a program or attack the system. By minimizing the attack area, you can reduce the vulnerability of the program. Common methods to minimize the attack area include avoiding storing sensitive information, giving users minimum access rights, minimizing the use of third-party libraries (the smaller the code, the fewer the vulnerabilities), and eliminating function code that is no longer used (the smaller the code, the fewer the vulnerabilities).

Avoiding logging in as root, not trusting user input, and minimizing the attack area are some of the most important ways to ensure program security. But this is only a small part of improving security. We should try to think in terms of hackers. How will they use your code? This helps us find bugs that we might otherwise have overlooked. The topic of security is too big to cover in this book, so it is advisable to keep thinking and learning about how to improve security. Bruce Schneier summed it up nicely: “Security is a state of mind.”

The glossary

Production code: code used by users in a product.

Production: To put software into production is to release it to the public.

DRY: A programming principle, short for “Don’t repeat yourself”.

Orthogonality: The term has been used to denote some kind of independence or decoupling. There is orthogonality between two or more things if their changes do not affect each other. In a well-designed system, the database code is orthogonal to the user interface; Adjusting the user interface does not affect the database, and replacing the database does not change the user interface.

Debugger: A debugger is a program that interrupts code execution to see the effect of the code line by line. The debugger allows us to view the values of variables in different code.

Logging: Refers to the practice of recording data while the software is running.

Testing: Checks that the program “meets design and development requirements, returns correct results for various inputs, takes acceptable time to perform functions, is available enough to be installed and run in the target environment, and achieves the results expected by interested parties.”

Code review: The process by which others read your code and give feedback.

Attack area: The area in which a hacker can extract data from a program or attack a system.

02 Python Programming without A Teacher

Amazon in the United States, there is a highly influential Python introduction book, Kindle version in the United States amazon website in the computer software, software development category ranked the first, beyond many solid Python books, many five-star praise. As you may have heard, this book is Called Python Programming without A Teacher: The Making of a Professional Programmer.





The author is a self-taught programmer. As a liberal arts student, he taught himself programming, mastered programming skills and got a job as a software engineer at eBay. This book is written from the author’s personal experience to help the reader grow from a layman to a professional Python programmer.

This book can satisfy almost any beginner who wants to learn programming. This book is suitable for high school and college students who want to learn programming by themselves, as well as those who want to get into programming in other industries. It is also suitable as a training material for beginners in programming.

At the end of each chapter is a glossary of terms and challenging exercises to help you learn Python 3 better.


Directory in

Chapter 1 Overview

Chapter 2 starting

Chapter 3 Introduction to programming

Chapter 4 functions

Chapter 5 Containers

Chapter 6 string Manipulation

Chapter 7 Cycles

Chapter 8 Modules

Chapter 9 Documents

Chapter 10 Comprehensive exercises

Chapter 11 Exercises

The second part introduces object-oriented programming

Chapter 12 programming paradigms

Chapter 13 the Four Pillars of object-oriented programming

Chapter 14 delves into object-oriented programming

Chapter 15 comprehensive exercises

Part three: Introduction to programming tools

Chapter 16 Bash

Chapter 17 regular expressions

Chapter 18 Package Manager

Chapter 19 version Control

Chapter 20

Part iv Introduction to computer science

Chapter 21 data Structures

Chapter 22 Algorithm

Part five Finding a job

Chapter 23 best programming Practices

Chapter 24 First programming job

Chapter 25 Teamwork

More learning materials in chapter 26

Chapter 27 Next step


Books recommended

Author: Al Sweigart

Get started with Python programming — automate tedious tasks

By Zed A. Shaw

The “dumb way” to learn Python 3

Author: Kenneth A. Lambert

Data structures (Python)

Author: Wesley Chun

Python Core Programming (Version 3)

Author: Ye Weizhong

Python programming goes from beginner to master


– END –