143: Where And How To Use Whitespace.
Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorías:
Making code more readable sometimes means knowing where to put nothing. Programming has strict rules about the meaning of the code. And compilers don’t care how easy the code is to understand. It either follows the rules or not. But as humans, we do care. Because if code is hard to read, then it’s hard to understand. And that increases the chances that mistakes will be made resulting in bugs. That’s assuming the project can be finished at all. I’ve already explained many aspects of making your code more understandable from how to name variables, methods, and classes, to the proper use of comments. And there’s the whole subject of design patterns that can help structure your code into common designs that have evolved and been proven to work well. This episode is about whitespace which includes spaces, tabs, and new lines. If you were to print your source code on plain white paper, then whitespace is anything that results in nothing being printed. For example, take the placement of opening and closing curly braces. Some people prefer to put the opening curly brace at the end of the line that starts the block. An if statement would then have the if keyword followed by the expression in parenthesis and then an opening curly brace all on the same line. I think this style is liked by some because a lot of books take this approach. But if you think about it, a book needs to save space and having a curly brace on a line all by itself wastes a lot of space. When programming though, vertical space is usually not as much of a problem. And especially these days with the size of computer monitors as big as they are. I like my curly braces to line up. Not only does this help me to read the blocks of code better but it’s consistent. Make sure to listen to the full episode for more explanation. Or you can also read the full transcript below. Transcript Have you ever tried stream of consciousness writing? This is a great way to get ideas down quickly without stopping to judge anything. You just keep the pen moving or keep typing keys without stopping and write down everything that comes to mind. You don’t capitalize anything or use any punctuation. Now, have you ever tried to read the output of this? You’ll get ideas, sure. But it’s not easy to read. Programming has strict rules about the meaning of the code. And compilers don’t care how easy the code is to understand. It either follows the rules or not. But as humans, we do care. Because if code is hard to read, then it’s hard to understand. And that increases the chances that mistakes will be made resulting in bugs. That’s assuming the project can be finished at all. I’ve already explained many aspects of making your code more understandable from how to name variables, methods, and classes, to the proper use of comments. And there’s the whole subject of design patterns that can help structure your code into common designs that have evolved and been proven to work well. This episode is about whitespace which includes spaces, tabs, and new lines. If you were to print your source code on plain white paper, then whitespace is anything that results in nothing being printed. This is not high school English class where lines have to be double-spaced and either single or double spaces appear after periods. Where the first lines of paragraphs are indented and your name appears in the top-right corner. Some of this is about personal style but not all of it. For example, take the placement of opening and closing curly braces. Some people prefer to put the opening curly brace at the end of the line that starts the block. An if statement would then have the if keyword followed by the expression in parenthesis and then an opening curly brace all on the same line. I think this style is liked by some because a lot of books take this approach. But if you think about it, a book needs to save space and having a curly brace on