 
 // MARK: Constants
 
-private let kReuseIdentifier = "CategoryCell"
+private let cellReuseIdentifier = "CategoryCell"
 
-// MARK: - Types
+// MARK: Types
 
 private enum SegueIdentifier: String {
-    case EditCategory
+    case editCategory = "EditCategory"
 }
 
-// MARK: - CategoriesTableViewController: UITableViewController
+// MARK: - CategoriesTableViewController: UITableViewController -
 
-class CategoriesTableViewController: UITableViewController {
+final class CategoriesTableViewController: UITableViewController {
     
-    // MARK: Properties
+    // MARK: Instance Variables
     
     var persistenceCentral: PersistenceCentral!
 
-    // MARK: View Life Cycle
+    // MARK: UIViewController lifecycle
     
     override func viewDidLoad() {
         super.viewDidLoad()

    // MARK: - Navigation
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == SegueIdentifier.EditCategory.rawValue {
            let navigationController = segue.destination as! UINavigationController
            let controller = navigationController.topViewController as! ManageCategoryTableViewController
            controller.persistenceCentral = persistenceCentral

            let indexPath = tableView.indexPathForSelectedRow!
            let category = persistenceCentral.categories[indexPath.row]
            controller.category = category
        } else {
            fatalError("Receive unknown segue identifier: \(String(describing: segue.identifier)).")
        }
    }

}

// MARK: - CategoriesTableViewController: UITableViewDataSource -

extension CategoriesTableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return persistenceCentral.categories.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)!

        let category = persistenceCentral.categories[indexPath.row]
        cell.textLabel?.text = category.name

        return cell
    }

    override func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCellEditingStyle,
                            forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            deleteCategory(at: indexPath)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

}

// MARK: - CategoriesTableViewController (Private helpers) -

extension CategoriesTableViewController {

    private func setup() {
        navigationItem.rightBarButtonItem = editButtonItem

        NotificationCenter.default
            .addObserver(self,
                         selector: #selector(reloadData),
                         name: NSNotification.Name(rawValue: kManageCategoryTableViewControllerDidDoneOnCategoryNotification),
                         object: nil)
    }

    @objc private func reloadData() {
        tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
    }

    private func deleteCategory(at indexPath: IndexPath) {
        let category = persistenceCentral.categories[indexPath.row]
        persistenceCentral.deleteCategory(category)
    }

}

