├── .gitignore └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bad-python-code 2 | 3 | #### If-Elif Hell 4 | ```python 5 | def func_x(num): 6 | if num == 1: 7 | return a() 8 | elif num == 2: 9 | return b() 10 | elif num == 3: 11 | return c() 12 | elif num == 4: 13 | return d() 14 | elif num == 5: 15 | return e() 16 | 17 | 18 | # Better 19 | def func_y(num): 20 | mapper = { 21 | 1: a, 22 | 2: b, 23 | 3: c, 24 | 4: d, 25 | 5: e 26 | } 27 | return mapper[num]() 28 | ``` 29 | 30 | #### Unnecessary If-Else Boolean 31 | ```python 32 | def func_x(is_chubby): 33 | if is_chubby is True: 34 | return a() 35 | elif is_chubby is False: 36 | return b() 37 | 38 | 39 | # Better 40 | def func_y(is_chubby): 41 | return a() if is_chubby else b() 42 | ``` 43 | 44 | #### Less Pythonic Loop 45 | ```python 46 | def func_x(array): 47 | for i in range(len(array)): 48 | print(array[i]) 49 | 50 | 51 | # Better 52 | def func_y(array): 53 | for item in array: 54 | print(item) 55 | ``` 56 | 57 | #### Returning boolean 58 | ```python 59 | def func_x(num): 60 | if num > 10: 61 | return True 62 | else: 63 | return False 64 | 65 | 66 | # Better 67 | def func_y(num): 68 | return num > 10 69 | ``` 70 | 71 | #### Too Many Variable Declarations 72 | ```python 73 | a = 1 74 | b = 2 75 | c = 3 76 | 77 | 78 | # Better 79 | a, b, c, = 1, 2, 3 80 | ``` 81 | 82 | #### Unpacking 83 | ```python 84 | x = [10, 20, 30] 85 | 86 | a = x[0] 87 | b = x[1] 88 | c = x[2] 89 | 90 | 91 | # Better 92 | a, b, c, = x 93 | ``` 94 | 95 | #### If/Else Loop Without a Break 96 | ```python 97 | def func_x(breeds, pet): 98 | for breed in breeds: 99 | if breed == pet: 100 | print("I have a dog.") 101 | else: 102 | print("I don't have a dog") 103 | 104 | 105 | func_x(["chihuahua", "jack russell"], "chihuahua") 106 | 107 | # => I have a dog. 108 | # => I don't have a dog 109 | 110 | 111 | # Better 112 | def func_y(breeds, pet): 113 | for breed in breeds: 114 | if breed == pet: 115 | print("I have a dog.") 116 | break 117 | else: 118 | print("I don't have a dog") 119 | 120 | 121 | func_x(["chihuahua", "jack russell"], "chihuahua") 122 | # => I have a dog. 123 | ``` 124 | 125 | #### Wildcard Imports 126 | ```python 127 | from typing import * 128 | 129 | 130 | # Better 131 | from typing import Union, Optional, OrderedDict 132 | ``` 133 | 134 | #### Context Manager 135 | ```python 136 | f = open("file.text", "r") 137 | try: 138 | content = f.read() 139 | finally: 140 | f.close() 141 | 142 | 143 | # Better 144 | with open("file.text", "r") as f: 145 | content = f.read() 146 | ``` 147 | 148 | #### Returning More than One Variable Type from Function 149 | ```python 150 | def func_x(param): 151 | if param: 152 | return 200, {"message": f"{param}"} 153 | else: 154 | return 400, "A useful error message" 155 | 156 | 157 | # Better 158 | def func_y(param): 159 | if param: 160 | return 200, {"message": f"{param}"} 161 | else: 162 | return 400, {"message": "A useful error message"} 163 | ``` 164 | 165 | #### Comparing None to a Variable 166 | ```python 167 | if something == None: 168 | print("Something is None!") 169 | 170 | 171 | # Better 172 | if something is None: 173 | print("Something is None!") 174 | ``` 175 | 176 | #### Comparing Boolean to a Variable 177 | ```python 178 | condition = True 179 | if conditon == True: 180 | print("'Tis true!") 181 | 182 | 183 | # Better 184 | if condition: 185 | print("'This true!") 186 | ``` 187 | 188 | #### Comparing types 189 | ```python 190 | doggos = ["tamago", "charlie"] 191 | if type(doggos) is list: 192 | print("'doggos' is a list") 193 | 194 | 195 | # Better 196 | if isinstance(doggos, list): 197 | print("'doggos' is a list") 198 | ``` 199 | 200 | #### Using Index to Return Tuple Values 201 | ```python 202 | def get_lego(): 203 | return "21103", "Back to the Future Time Machine", "401", "2013" 204 | 205 | my_lego = get_lego() 206 | print(my_lego[0], my_lego[1], my_lego[2], my_lego[3]) 207 | 208 | 209 | # Better 210 | from collections import namedtuple 211 | 212 | def get_lego(): 213 | lego = namedtuple("lego", ["serial_number", "name", "num_of_pieces", "release_year"]) 214 | return lego("21103", "Back to the Future Time Machine", "401", "2013") 215 | 216 | my_lego = get_lego() 217 | print(my_lego.serial_number, my_lego.name, my_lego.num_of_pieces, my_lego.release_year) 218 | ``` 219 | 220 | #### Overusing map() 221 | ```python 222 | numbers = [10, 20, 30] 223 | double_numbers = list(map(lambda num: num * 2, numbers)) 224 | 225 | 226 | # Better 227 | numbers = [10, 20, 30] 228 | double_numbers = [num * 2 for num in numbers] 229 | ``` 230 | 231 | #### Using Wildcard Imports 232 | ```python 233 | from elastic_appsearch import * 234 | 235 | 236 | # Better 237 | from elastic_appsearch import Client 238 | ``` 239 | 240 | #### Asking for Permission 241 | **EAFP** 242 | - "Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C." 243 | - [Python Glossary](https://docs.python.org/3/glossary.html) 244 | 245 | ```python 246 | if "key" in dictionary: 247 | item = dictionary["key"] 248 | else: 249 | item = "" 250 | 251 | 252 | # Better 253 | try: 254 | item = dictionary["key"] 255 | except KeyError: 256 | item = "" 257 | ``` 258 | 259 | #### Not Using get() in a Dict to Set a Default Value 260 | ```python 261 | try: 262 | item = dictionary["key"] 263 | except KeyError: 264 | item = "" 265 | 266 | 267 | # Better 268 | item = dictionary.get("key", "") 269 | ``` 270 | 271 | #### Masking Potential Bugs w/ Too Broad Try/Except Clauses 272 | ```python 273 | try: 274 | return do_something(my_dict[key]) 275 | except KeyError: 276 | return "" 277 | 278 | 279 | # Better 280 | try: 281 | value = my_dict[key] 282 | except KeyError: 283 | value = "" 284 | else: 285 | return value 286 | ``` 287 | 288 | #### Having Really Long Conditions in 1 Line: 289 | ```python 290 | if super_long_comparsion1 and super_long_comparison2: 291 | pass 292 | 293 | 294 | # Better 295 | cond1 = super_long_comparison1 296 | cond2 = super_long_comparison2 297 | if cond1 and cond2: 298 | pass 299 | ``` --------------------------------------------------------------------------------