Was listening to a podcast with DHH on Tim Ferris show and I really like his definition of beautiful code.

Basically said it’s code where all the methods and abstractions in a class are at the same level. Some methods aren’t doing really low level things. Others aren’t too high. They are all at the same level of abstraction.

Good way to describe any nice API.

For example you can do this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDelegates];
    [self setupLayouts];
    [self setupSearchBar];
    [self setupReachability];
    [self displayTracks];
}

Or you can do this

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchBar.delegate = self;
    self.collectionView.delegate = self;
    self.collectionView.collectionViewLayout = self.collectionFlowLayout;
    [self setupSearchBar];
    [self setupReachability];
    [self displayTracks];
}

See the difference? The methods are not all at the same level of abstraction. Which makes the code harder to read.

Advertisements