One of the core elements of clean code is having code that is consistent with identifiers. Head to the
elegate. Take a look at this code from the tip from last week.
I have a few problems with it. I’m not being very consistent in some of those identifiers such as the
ColorTableViewController. However, these identifiers are all over my code. There’s a great way to get consistency using refactoring.
HueColorTVC, then right click.
Click refactor… and then rename.
Xcode shows you all occurrences of this identifier, which in this case is only in the app delegate.
hueColorTableViewController. All the occurrences change at once.
Select and then right-click the
ColorTableViewController, and once again hit refactor.
Both cases of the class identifier, when it is instantiated in teh App delegate and the definition in HueColorTVC.swift are highlighted. Change to
Let’s do this one more time. I’m going to change the
Now I’d like my file name to match too. One way to change it is in attributes. Select HueColorTVC.swift.
Open the attributes panel. Change the name to HueColorTableViewController.swift.
and the file name changes.
One caution about all this renaming. Comments don’t refactor easily. You’ll see that at the top of the code. Select and refactor that. You can’t. Once you refactor, you might want to also search to find anything you didn’t get in your refactoring. You can replace all these cases by selecting replace, and adding the replacement in the text box.
Refactoring is a very useful and simple tool to get your code a lot more consistent in identifiers.