![]() Horizontal SpacingĮmpty space is just as important for readability as the code you write. Black uses formatting rules that programmers can live with, letting us spend less time arguing and more time programming. I don’t like everything about how Black formats code, but I take that as the sign of a good compromise. The Python code guidelines you learn in this chapter also generally apply to other languages, which might not have automatic formatters available. ![]() You should learn these code formatting guidelines, because you might not always have Black conveniently at hand. When in doubt, use your best judgment.” Whether you follow all of it, some of it, or none of it, it’s worthwhile to read the PEP 8 document.īecause we’re using the Black code formatter, our code will follow Black’s style guide, which is adapted from PEP 8’s style guide. PEP 8 even includes the following advice: “Know when to be inconsistent-sometimes style guide recommendations just aren’t applicable. The “A Foolish Consistency Is the Hobgoblin of Little Minds” section of the guide reminds the reader that maintaining consistency and readability within a project, rather than adhering to any individual formatting rule, is the prime reason for enforcing style guides. Many Python programmers view PEP 8 as an authoritative set of rules, although the PEP 8 creators argue otherwise. But some software companies have established their own style guides as well. The Python Enhancement Proposal 8 ( PEP 8) is one such style guide written by the Python core development team. Style Guides and PEP 8Īn easy way to write readable code is to follow a style guide, a document that outlines a set of formatting rules a software project should follow. These style choices can be much like deciding which side of the road to drive on it doesn’t matter whether people drive on the right side of the road or the left side, as long as everyone consistently drives on the same side. Deciding whether to, say, put one or zero spaces after a comma is a matter of personal preference. Or worse, the programmers will constantly be reformatting each other’s code to their own style, wasting time and causing arguments. If several programmers working on the same source code files write in their own style, the code can become an inconsistent mess, even if it runs without error. Programming is difficult enough, and writing understandable code for others (or for yourself in the future) can minimize this problem.Īlthough you might start out coding on your own, programming is often a collaborative activity. But it’s valuable for beginners to establish good code formatting habits. But programmers sometimes choose the latter, because they don’t want to worry about details that have no impact on whether the program works correctly.īeginners often ignore code formatting because they’re focused on programming concepts and language syntax. Programmers who prefer the former approach might like the visual separation that the spaces add and the uniformity of the quote characters. ![]() For example, we can write a list with a single space after each comma and use one kind of quote character consistently: spam = īut even if we wrote the list with a varying number of spaces and quote styles, we’d still have syntactically valid Python code: spam= We can write code in many ways that result in identical behavior. How to Lose Friends and Alienate Co-Workers Then you’ll learn how to install, use, and customize the tool. You’ll first learn about the rationalization for the code style choices Black makes. Black is useful, because it’s tedious to manually format your code in a text editor or IDE. This chapter introduces you to Black, a code formatting tool that can automatically format your source code into a consistent, readable style without changing your program’s behavior. Python’s readability is a critical reason for the language’s popularity. Formatting code isn’t a mere cosmetic issue. If your code is difficult for humans (whether it’s you or a co-worker) to understand, it will be hard to fix bugs or add new features. Although unimportant to the computer parsing your program, code formatting is vital for readability, which is necessary for maintaining your code. Code formatting is applying a set of rules to source code to give it a certain appearance.
0 Comments
Leave a Reply. |