--- title: Code Style tags: COMP1010-21T3 slideOptions: transition: slide --- <style> .reveal { font-size: 20px; } .reveal div.para { text-align: left; } .reveal ul { display: block; } .reveal ol { display: block; } img[alt=drawing] { width: 200px; } </style> # COMP1010 ## Code Style `x = x + 1` rather than `x = 1 + x` hello my name is sim this is my tenth year teaching comp my favourite thing about lockdown is driving places is quicker Hello my name is Sim. This is my tenth year teaching comp. My favourite thing about lockdown... ## Why is Code Style Important? * Readable * Maintainable When things go wrong, we want to find out why and fix them as quickly as possible. ## Variable Names * all lowercase letters * underscores to break up words * meaningful ## Strings * Use either double quotes or single quotes consistently throughout your project to contain strings. --- # Line Length Lines (text or code) should not exceed 80 characters. --- ## Indentation and Line Length * Indentation: * Not tabs - spaces. * 4 spaces per indentation level is ideal. * 2, 3 or 4 spaces are acceptable, but they must be consistent. * Line length: * Line length should not exceed 79 characters. * [Reasoning](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) * If you need help (eg your line of code is longer than this, and you need to put it over 2 lines, but then the code breaks), post your specific example on the forums and we can show you how to do this. --- # Functions * Functions: * Don't leave chunks of code floating around your file. * When in doubt, put your code in a (well-named) function, and call it at the right time. --- # File Structure ```{python} # import libraries (if you have them) # define constants (if you have them) # other functions you write (if you have them) def main(): # Your code here if __name__ == '__main__': main() ``` --- ## Constants * Variables, except... * Don't change throughout your entire code. * Style: should be defined in capital letters. * Examples: * Guess My Number: * `START_HIGH = 100` * `START_LOW = 0` * Tic Tac Toe: * `BOARD_SIZE = 3` * `PLAYER_1 = 'X'` * `PLAYER_2 = 'O'` * `EMPTY = '-'` ---