C# Console Programming Lesson 5
Archive (40 Kb) contains PDF version of lesson and all pertinent source code files. Return to Lesson 5 content.
View On WordPress
seen from Malaysia
seen from United States

seen from Germany
seen from United States
seen from United States

seen from Germany

seen from United States

seen from Australia

seen from United States

seen from Malaysia

seen from Germany
seen from China
seen from India
seen from United States
seen from United States

seen from El Salvador

seen from India
seen from Malaysia
seen from Malaysia

seen from United States
C# Console Programming Lesson 5
Archive (40 Kb) contains PDF version of lesson and all pertinent source code files. Return to Lesson 5 content.
View On WordPress
Helper Methods: Open Up The Door
Today in class, our instructor told us he'd like us to limit each method to around 7 lines of code. This made sense, based on our understanding of the single responsibiliy principle. Nonetheless, it was alarming -- what if fulfilling that responsibility required several steps? Well, that's when I realized I needed somebody - namely, a helper method.
A helper method does pretty much what it says on the tin - it is a method that helps another method perform it's task. You can use one to extract complicated logic to make a method more readable. You can also use one because you know you're going to be using that functionality frequently (ie: >=3 times).
In a way, the entire premise of Ruby on Rails is that it generates a giant pile of useful helper methods for developers to get their projects off the ground. For example, both "form_for" and "form_tag" are Rails helper methods that take in erb code and create clean HTML.
Rails also encourages that we write our own helper methods. To that end, it comes packaged with a class method, called helper, that declares another method (or a module) is available to help us with anything to do with that class.
Here's an example of how helpers cleaned up my code. I've been working on a side project I'm calling Movie Notebook. One of the goals of this project was to practice creating a functional app, from the ground up, without any instructions or expectations from teachers. Part of that, I knew, would involve writing my very first rspec test suite.
While creating my tests, I realized that I was writing @movie = Movie.create(et cetera) an awful lot. Since I already had five different spec files, I decided that I would create a module that would, among other things, create a movie for me to edit. Check out my module below.
And here are some before and after shots of my tests:
In fact, to get rspec to work at all, you'll need a helper--it's a configuration file found at (in my case) /spec/rails_helper.rb. So it was very easy to include my module in all my extant specs and any I create in the future. I just went into my "RSpec.configure do |config|" block and added "config.include MovieHelper" (though if I'd wanted it just for controller tests, I'd've added ", :type => :feature").
today’s title was courtesy of the Beatles
W4D5 - Partials and Helper Methods
Project
Reddit Clone! Most people use reddit to waste time. We used it to learn about partials and helpers, and to practice authentication again.
MVC
Rails separates its logic (for the most part) into the model, the view, and the controller. The controller is the main workhorse. The model temporarily provides an object oriented version of part of the database. The view is just a simple document to show to the user after all the work is done.
Depending on the state of the web app and the user's loggin status, we may want to show different things on a given page. Unfortunately, a natural tendancy is to write more and more of this logic in the view. This makes the view code ugly and bloated.
Partials and Helper Methods
Partials and helper methods to the rescue! Helper methods can be organized and stored in a different file and called with a short descriptive name from the view. This separates different logical pieces, making them easier to work with and debug. Likewise, partials can store large chunks of code that are repeated in several different views accross the website. When one needs to be edited, they all can be edited in one place!