Take Care of Your Code!

Simple. Clean.

Introduction

Methods can be very useful in programming. They are making our code looks better, if we use it the right way. But some programmers don’t really pay attention to method (the clean way). They just keep doing a lot of stuff in one method. In this article, I will give you an example of messy method vs clean method.

Example : Messy method

import UIKit
extension ViewController: UITableViewDelegate {
// Some UITableViewDelegate methods
//
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let verticalOffset = tableView.contentOffset.y
if verticalOffset >= 64 {
navigationController?.unTransparentNavBar(tintColor: Resources.Color.primaryColor, barTintColor: .white)
let textAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
} else {
navigationController?.transparentNavBar(tintColor: .white)
let textAttributes = [NSAttributedStringKey.foregroundColor: UIColor.clear]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
}
}

This example gives us understanding that writting a messy method is a bad habit. scrollViewDidScroll method do some different things. it’s calculating, and configuring the UINavigationBar appeareance. This is a bad example of a method.

Clean Method Rules

These are some rules which I think the most important :

  • Meaningful names
  • Object Params (if current Params are too many)
  • Short
  • do one thing!

Example : Clean method

import UIKit
extension ViewController: UITableViewDelegate {
// Some UITableViewDelegate method
//
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let verticalOffset = tableView.contentOffset.y
verticalOffset >= 64
? setupNavbarAppereanceNotTransparentWhenScolling()
: setupNavBarAppereanceTransparentWhenScrolling()
}
}
extension ViewController {
private func setupNavbarAppereanceNotTransparentWhenScolling() {
navigationController?.unTransparentNavBar(tintColor: Resources.Color.primaryColor, barTintColor: .white)
let textAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
private func setupNavBarAppereanceTransparentWhenScrolling() {
navigationController?.transparentNavBar(tintColor: .white)
let textAttributes = [NSAttributedStringKey.foregroundColor: UIColor.clear]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
}

Now you have witness the clean way of using and creating a method. This example makes the reader clarity when trying to understand what is really going on. On the scrollViewDidScroll method, it’s only one thing happend. Instead of doing manything on the method, It’s calling another method to handle other thing, which is have a descriptive name. With this approach, other programmers or maybe non-programmers can undestand easily.

Conclusion

Clean method delivers a clean approach to write and use a method. This approach give a clear statement what method is and what method meant for. If you apply the clean method way, your code will look nice, easy to read and understand by yourself or other programmers in a team, because you are taking care of your code. Keep your method clean and happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s