Conditions | 1 |
Total Lines | 58 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """A sample GUI.""" |
||
31 | def init(self, root): |
||
32 | padded = {'padding': 5} |
||
33 | sticky = {'sticky': NSEW} |
||
34 | |||
35 | # Configure grid |
||
36 | frame = Frame(root, **padded) |
||
37 | frame.rowconfigure(0, weight=1) |
||
38 | frame.rowconfigure(1, weight=1) |
||
39 | frame.columnconfigure(0, weight=1) |
||
40 | |||
41 | def frame_inputs(root): |
||
42 | frame = Frame(root, **padded) |
||
43 | |||
44 | # Configure grid |
||
45 | frame.rowconfigure(0, weight=1) |
||
46 | frame.rowconfigure(1, weight=1) |
||
47 | frame.columnconfigure(0, weight=1) |
||
48 | frame.columnconfigure(1, weight=1) |
||
49 | frame.columnconfigure(2, weight=1) |
||
50 | |||
51 | # Place widgets |
||
52 | entry = Entry(frame, width=7, textvariable=self.feet) |
||
53 | entry.grid(column=1, row=0) |
||
54 | entry.focus() |
||
55 | label = Label(frame, text="feet") |
||
56 | label.grid(column=2, row=0) |
||
57 | label = Label(frame, text="is equivalent to") |
||
58 | label.grid(column=0, row=1) |
||
59 | label = Label(frame, text="meters") |
||
60 | label.grid(column=2, row=1) |
||
61 | label = Label(frame, textvariable=self.meters) |
||
62 | label.grid(column=1, row=1) |
||
63 | |||
64 | return frame |
||
65 | |||
66 | def frame_commands(root): |
||
67 | frame = Frame(root, **padded) |
||
68 | |||
69 | # Configure grid |
||
70 | frame.rowconfigure(0, weight=1) |
||
71 | frame.columnconfigure(0, weight=1) |
||
72 | |||
73 | # Place widgets |
||
74 | button = Button(frame, text="Calculate", command=self.calculate) |
||
75 | button.grid(column=0, row=0) |
||
76 | self.root.bind('<Return>', self.calculate) |
||
77 | |||
78 | return frame |
||
79 | |||
80 | def separator(root): |
||
81 | return Separator(root) |
||
82 | |||
83 | # Place widgets |
||
84 | frame_inputs(frame).grid(row=0, **sticky) |
||
85 | separator(frame).grid(row=1, padx=10, pady=5, **sticky) |
||
86 | frame_commands(frame).grid(row=2, **sticky) |
||
87 | |||
88 | return frame |
||
89 | |||
104 |