Conditions | 10 |
Total Lines | 65 |
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:
Complex classes like RainmeterNewSkinCommand.create_skin() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
51 | def create_skin(self, name): |
||
52 | """Callback method executed after the user entered the skin name.""" |
||
53 | skinspath = get_cached_skin_path() |
||
54 | if not skinspath or not os.path.exists(skinspath): |
||
55 | sublime.error_message( |
||
56 | "Error while trying to create new skin: " + |
||
57 | "Skins path could not be found. Please check the value" + |
||
58 | " of your \"skins_path\" setting.") |
||
59 | return |
||
60 | |||
61 | name = os.path.normpath(name.strip("\\").strip("/")) + "\\" |
||
62 | |||
63 | # Path where the new ini file will be created |
||
64 | newskinpath = os.path.join(skinspath, name) |
||
65 | |||
66 | # Path where the @Resources folder should be created |
||
67 | basepath = os.path.join(skinspath, |
||
68 | re.match("(.*?)\\\\", name).group(1)) |
||
69 | |||
70 | try: |
||
71 | os.makedirs(newskinpath) |
||
72 | except os.error: |
||
73 | sublime.error_message( |
||
74 | "Error while trying to create new skin: " + |
||
75 | "Directory " + newskinpath + " could not be created. " + |
||
76 | "Does it already exist?") |
||
77 | return |
||
78 | |||
79 | # Check which folders should be created |
||
80 | settings = sublime.load_settings("Rainmeter.sublime-settings") |
||
81 | make_resources = settings.get( |
||
82 | "rainmeter_new_skin_create_resources_folder", |
||
83 | True) |
||
84 | make_images = settings.get( |
||
85 | "rainmeter_new_skin_create_images_folder", |
||
86 | True) |
||
87 | make_fonts = settings.get( |
||
88 | "rainmeter_new_skin_create_fonts_folder", |
||
89 | True) |
||
90 | make_scripts = settings.get( |
||
91 | "rainmeter_new_skin_create_scripts_folder", |
||
92 | True) |
||
93 | |||
94 | try: |
||
95 | if make_resources: |
||
96 | os.makedirs(os.path.join(basepath, "@Resources")) |
||
97 | if make_images: |
||
98 | os.makedirs(os.path.join(basepath, "@Resources\\Images")) |
||
99 | if make_fonts: |
||
100 | os.makedirs(os.path.join(basepath, "@Resources\\Fonts")) |
||
101 | if make_scripts: |
||
102 | os.makedirs(os.path.join(basepath, "@Resources\\Scripts")) |
||
103 | except os.error: |
||
104 | sublime.status_message("Did not create @Resources folder or" + |
||
105 | " subfolders because they already exist") |
||
106 | |||
107 | window = self.window |
||
108 | filename = os.path.basename(os.path.normpath(name)) |
||
109 | open(os.path.join(newskinpath, filename + ".ini"), 'a') |
||
110 | newview = window.open_file(os.path.join(newskinpath, |
||
111 | filename + ".ini")) |
||
112 | |||
113 | # We have to wait until the file is fully loaded (even if it's empty |
||
114 | # because it was just created) |
||
115 | sublime.set_timeout((lambda: self.open_skin_file(newview)), 100) |
||
116 | |||
134 |