Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Created January 5, 2015 20:23
Show Gist options
  • Save paulghaddad/c38d8c2a437e1eccafc6 to your computer and use it in GitHub Desktop.
Save paulghaddad/c38d8c2a437e1eccafc6 to your computer and use it in GitHub Desktop.
LevelUp 4: Knows how to use view helpers to aid clarity
1. Explain how helpers improve the readability and organization of your rails app.
View helpers allow you to extract logic from your views in order to keep them as simple as possible. They improve the readability of view templates in several ways:
- Replacing complicated logic with a call to a helper method eliminates a lot of code from the view template itself. In addition, the code that remains in the view template is focused on the view layer, namely HTML elements and ERB tags consisting of calls to view helpers and objects from the controller.
- The purpose of the view helper can be expressed by the name you choose. For example, a view helper that displays admin controls can be encapsulated in a method named, "admin_controls". The purpose of the method is clear from its name. If you instead put all the logic to determine whether a user is an admin and then conditionally display elements, the purpose of the code would be difficult to comprehend.
In addition to making the view templates easier to read, view helpers improve the organization of the app itself. The primary way it achieves this is by removing duplication. A view helper can be reused throughout the application, and if it needs to be changed later on, it only needs to be changed in one location. DRYing out the code in this fashion makes the app easier to maintain and change over time, in addition to reducing the amount of code you have to write in the first place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment