![]() Code is read more often than it’s written, so explicit, readable code is more important than terse, undocumented code. Don’t drop vowels from your names or write overly terse code. While strcmp() may obviously mean the “string compare” function to someone who has been programming in C since the 1970s, modern computers have enough memory to write out the full function name. Code that is spread out over multiple lines is often easier to read than dense one-liners. While code like this may impress their friends, it’ll infuriate their coworkers who have to try to understand it. Print('\n'.join("%i bytes = %i bits which has %i possible values." % (j, j*8, 256**j-1) for j in (1 << i for i in range(8)))). Programmers often like to cram as much functionality into as little code as possible, such as one-liners like the following: If you make packages and modules that require code like import .foo.bar, then you’re making your code too complicated. ![]() It’s okay to have code in just one top-layer module or class instead of split up across multiple submodules or subclasses. These hierarchies often don’t add organization so much as they add bureaucracy. Programmers love to organize things into categories, especially categories that contain subcategories which contain other sub-subcategories. Prefer simplicity to complexity, but know the limits of simplicity. Even if you could technically make a diesel train engine using birdhouse techniques, you probably would end up with a complicated, Rube Goldberg arrangement of birdhouse parts that wouldn’t be an ideal solution. Building a diesel train engine, on the other hand, is a complex problem that requires complex techniques. With a simple problem, such as building a birdhouse, a simple solution is better. These two aphorisms remind us that building anything can be done using simple or complex techniques. Simple is better than complex Complex is better than complicated Hiding code functionality might have repercussions as other programs might not be able to understand the code. It means that it is better to make the code more verbose and explicit. Of course, not every script needs to be beautiful, and beauty is subjective, but much of Python’s popularity is a result of being so easy to work with. While code doesn’t have to be readable, the code of the Python language itself must be thought out, consistent, and a joy to use. Programmers often write code quickly without concern for readability.
0 Comments
Leave a Reply. |