Software Development

Summarized using AI

Stay DRY with Helper Methods

Sam Galindo • October 25, 2022 • online

In the video titled "Stay DRY with Helper Methods" presented by Sam Galindo at the WNB.rb Meetup, the speaker discusses the concept of helper methods within Ruby on Rails. Sam, a relatively new developer, shares her journey and experiences in learning to code while presenting the utility of helper methods in creating cleaner, more manageable code.

Key Points Presented:

- Introduction to Helper Methods:

- Helper methods are defined as macros for models and controllers that extract complex logic or duplicate code into reusable methods.

- They are commonly used in Ruby on Rails to simplify coding tasks such as handling forms and generating links.

  • Application of Helper Methods:

    • Sam illustrates her understanding of helper methods through a personal example involving user group management in her code.
    • She faced challenges with button text that failed to change based on the form's action (creating or editing a group).
    • With guidance from her mentor, she learned to implement a helper method that efficiently manages this logic without cluttering her views.
  • Placement and Organization of Helper Methods:

    • Sam advises that the best place to organize helper methods is within the appropriate helper files instead of dumping them into the application helper, which can lead to disorganized code.
    • Helper methods can also be added to controllers, making them accessible throughout various parts of the application.
  • Common Pitfalls and Best Practices:

    • Sam warns against creating multiple helper methods with the same name, as this could lead to confusion.
    • Developers should avoid overusing the application helper to prevent it from becoming a catch-all for miscellaneous code.
  • Conclusion and Flexibility:

    • Sam concludes by emphasizing the importance of flexibility and adapting the use of helper methods to fit the needs of the application and the development team.
    • The speaker suggests considering alternative options like decorators if they suit the project's requirements better than traditional helper methods.

Overall, the presentation serves as a practical guide for new developers to understand and implement helper methods effectively, promoting cleaner code and adherence to the DRY (Don't Repeat Yourself) principle.

Stay DRY with Helper Methods
Sam Galindo • October 25, 2022 • online

This presentation offers a humble explanation of what helper methods are and how they can help you build a lighter and cleaner app.

Sam Galindo's pronouns are she/her/they/them. Sam is from Mexico but now lives in the U.S in Durham, North Carolina. She is a baby developer, and new to the Tech Industry. She is a libra that loves dancing, and going on hikes. When she is not glued to her computer reading about technology she is taking deep breaths or laughing with her 6-year-old kid

https://www.wnb-rb.dev/meetups/2022/10/25

WNB.rb Meetup

00:00:00.000 want to thank Emily for your encouraging
00:00:03.000 words
00:00:04.319 I was really nervous at first when I
00:00:08.280 mentioned that I wanted to do a
00:00:10.800 presentation
00:00:13.320 um
00:00:13.980 and you really helped me move on
00:00:17.340 to do this so thank you Emily
00:00:20.520 um okay so helper methods
00:00:23.880 um I am
00:00:25.320 I I mentioned I'm a baby developer
00:00:27.539 because I've been uh learning how to
00:00:31.080 code uh for the last for the past year
00:00:34.020 and
00:00:36.180 um
00:00:37.860 um started learning Ruby Ruby and rails
00:00:40.500 of the past five to six months so I I
00:00:44.100 feel like I'm a baby
00:00:45.780 um uh a developer
00:00:48.719 um yeah so helper methods right
00:00:52.320 um
00:00:53.700 let's start
00:00:55.260 so I have a tiny agenda going on having
00:00:57.780 to drug in the introduction the
00:01:00.300 implementation how a noob like me
00:01:02.520 implements them placement where does the
00:01:05.640 logic go conclusion
00:01:09.000 um okay
00:01:10.380 so
00:01:12.299 okay so what are helpers and what are
00:01:15.540 they used for I'm gonna read uh from
00:01:18.540 here
00:01:19.979 um so I as I was browsing through the
00:01:22.439 internet looking for
00:01:24.720 um definitions I found uh this
00:01:28.140 definition was uh in very more
00:01:31.080 interesting to me
00:01:33.060 um so helpers are models macros model
00:01:36.240 and controller methods that help us try
00:01:38.820 up our code by extracting complex logic
00:01:41.340 or duplicate code into reusable methods
00:01:44.520 to be used in or by our views and
00:01:48.119 controllers
00:01:49.380 so helpers are used all across
00:01:52.579 rails and
00:01:55.680 um they're used to deal with forms to
00:01:58.079 generate hyperlinks and uh for so many
00:02:01.500 other things
00:02:06.540 in this presentation I will not dive
00:02:09.179 super deep into what are they uh you can
00:02:12.060 see my image though
00:02:15.360 um
00:02:16.140 it's um as I'm still like processing and
00:02:19.800 learning what they are
00:02:22.440 um here I wanted to share where did you
00:02:25.620 find your helpers or where do you place
00:02:28.920 them right
00:02:30.720 um
00:02:33.360 so
00:02:34.620 uh if you go to your
00:02:37.739 um to your code editor uh to your
00:02:40.739 directory tree you can see that you have
00:02:42.840 a CEO files
00:02:44.400 right if you uh use uh if you generate
00:02:48.000 them with a scaffold
00:02:50.099 um
00:02:50.879 uh command you'll have a lot of um oops
00:02:55.080 a lot of uh files but uh you will go to
00:02:59.040 your folder app and then I had an image
00:03:02.459 here that shows you that once you click
00:03:04.080 an app you will see more folders and a
00:03:06.959 folder that says helpers uh sorry you
00:03:10.319 can see it this thing was working I'm
00:03:13.019 not sure what happened but let's move on
00:03:16.680 okay you can see it here now so
00:03:21.720 um when I was working uh with a group of
00:03:26.879 um I was working with a group of other
00:03:28.980 students we had a project the purpose
00:03:31.860 was not to deploy an app but it was more
00:03:35.099 to learn how to collaborate with each
00:03:36.900 other uh through GitHub and how to
00:03:40.500 remain organized so
00:03:43.200 um in this example my helper method is
00:03:45.959 extracting some conditionals some of the
00:03:48.659 complex logic from My Views so the
00:03:51.299 buttons text of my object group
00:03:54.680 uh changes depending on my action uh if
00:03:59.580 it's if the action is a new or if if I
00:04:02.819 want to update the information from a
00:04:06.360 group
00:04:07.980 the the the name of the button or the
00:04:11.099 text of the button will also change
00:04:13.560 uh so this allows me to keep my views
00:04:16.500 more organized and
00:04:18.959 dry
00:04:21.359 um
00:04:23.639 so this is how
00:04:27.660 so this is how I wanted my views to look
00:04:30.060 like
00:04:31.800 um I'm just I didn't share a lot of like
00:04:33.720 the app right but you know have a user
00:04:36.960 and that user is able to join groups
00:04:41.220 um this person is or this user is able
00:04:43.380 to create a group and my button as I
00:04:46.380 mentioned uh when I create one the
00:04:48.900 button the button text is create
00:04:51.660 and the issue that I had is that
00:04:53.759 whenever I wanted to edit my uh my group
00:04:57.660 I would uh
00:05:00.120 the text of my button will not change so
00:05:03.440 I decided to create you know I was using
00:05:07.620 uh you know my form partial to keep uh
00:05:11.699 my code to not repeat myself but
00:05:14.220 I couldn't figure out how to
00:05:16.680 uh make sure that my button will change
00:05:18.840 depending on the action so I just
00:05:21.660 created another file another partial
00:05:25.680 um and when my mentor uh looked at my
00:05:29.039 code uh she was like
00:05:31.080 hey you know uh I see what you're doing
00:05:33.600 here uh but let's remember one of the
00:05:36.660 main principles
00:05:38.100 uh don't repeat yourself
00:05:40.639 and she told me you should check out
00:05:43.979 helper methods read about them and see
00:05:46.440 how you can use them with your views
00:05:48.900 so
00:05:50.340 um
00:05:51.960 so yeah
00:05:53.220 um I was able to read through them and
00:05:54.720 implement it on my previous example
00:05:58.259 um so
00:06:00.180 I know that
00:06:01.919 um
00:06:02.520 uh sometimes our helper what what I was
00:06:05.460 reading on the internet is that uh you
00:06:08.699 know sometimes or you can place your
00:06:10.380 helper methods in different places but
00:06:13.680 the best
00:06:15.300 place to the the best place to place
00:06:19.500 your helpers
00:06:20.759 or helper methods are in in your helper
00:06:24.060 files
00:06:25.740 um now
00:06:27.900 um
00:06:28.680 you could add this help using your
00:06:31.020 controllers right
00:06:33.600 um but what we want um
00:06:36.479 so by putting a helper method in our
00:06:39.060 application controllers we make it
00:06:40.560 available to all of our controllers and
00:06:42.780 Views but uh if we just want to keep
00:06:47.400 um
00:06:48.020 we we want to keep our um
00:06:52.639 our code accessible to specific things
00:06:56.160 that's why we separate them into our
00:06:58.199 helper
00:06:59.520 files
00:07:02.120 and here's just an example of uh this is
00:07:06.539 not like a personal example
00:07:10.800 um there are some good uh I I read a
00:07:13.860 couple of things that I had no idea
00:07:15.780 about but
00:07:17.699 um that I thought that they were good to
00:07:19.560 know uh to remember it
00:07:21.780 um so I'm just going to read them
00:07:24.599 um some of them say a boy creating
00:07:26.520 multiple helper methods with the same
00:07:28.440 name even if they have their own
00:07:31.020 isolated helper model and they work fine
00:07:33.840 it's another popular source of confusion
00:07:37.199 and as I am working through uh projects
00:07:42.180 um I can see how that could be confusing
00:07:46.139 um
00:07:48.000 um another one says helpers our models
00:07:50.400 and do not allow inheritance so if you
00:07:52.620 want to combine methods from other
00:07:54.060 helper models you'd have to include
00:07:56.160 those models in your own helper model
00:07:59.639 um for this one I am also trying to
00:08:02.340 understand what inheritance mean
00:08:05.120 and I feel like I can't see what this is
00:08:09.060 uh what is the meaning of of this tip uh
00:08:12.840 but I am still I'm trying to understand
00:08:15.360 it
00:08:17.099 um it says be uh be warned though that
00:08:19.379 the application helper should be avoided
00:08:21.720 as much as possible as it tends to
00:08:23.819 become a Dumping Ground for a code that
00:08:25.979 doesn't seem to belong anywhere in
00:08:27.599 particular
00:08:29.220 um I saw a couple of samples again in
00:08:31.080 the internet that were
00:08:33.839 um sometimes people are just dump their
00:08:36.899 uh helpers there and uh even though uh
00:08:41.760 they're functional they're not organized
00:08:43.380 and it's hard to maintain and read the
00:08:45.660 code especially if other people are uh
00:08:48.480 yeah interacting with the application
00:08:51.480 so
00:08:52.920 um
00:08:53.600 uh in conclusion I uh remain flexible by
00:08:58.920 adapting to your application needs and
00:09:01.800 yeah remember to drop stay dry and
00:09:03.720 explicit but I will say that I did read
00:09:06.360 uh something uh as well that say many
00:09:10.860 good developers believe that there are
00:09:12.540 inherent problems with rails helper
00:09:14.760 methods because of the way they are
00:09:16.500 namespaced and made available globally
00:09:19.500 for these developers using decorators
00:09:21.839 with a tool like Trapper Jam it's a
00:09:24.959 better option so I didn't go in deep
00:09:27.180 more in deep as of
00:09:29.000 why why is this I think I'm also like
00:09:32.160 learning about this other side of helper
00:09:35.040 methods and I'm not sharing or I didn't
00:09:38.160 go into it but uh yeah I think that
00:09:40.740 whatever it's best for your personal app
00:09:44.100 or your team I will say uh you know stay
00:09:48.720 flexible and adapt
00:09:50.580 yes thank you
Explore all talks recorded at WNB.rb Meetup
+20