00:00:06.359
Video equipment rental cost is paid for by Peep Code.
00:00:18.840
Today, I want to talk to you about Ruby best practice patterns.
00:00:24.359
This is something that I have spent a lot of time thinking about in my work with Ruby. It's a concept that has helped me become a better programmer, not just in Ruby but in general.
00:00:38.520
For those of you who are not familiar with the term, it’s actually quite simple: Ruby is Ruby. We all love Ruby.
00:00:44.200
When we talk about best practices, there's a legal definition that pertains to doing work according to industry-recognized standards to prevent liability or negligence.
00:00:56.600
However, what I'm discussing here is different. In the context of Ruby, I am referring to a set of techniques that experts recognize and use.
00:01:09.360
One of the key words here is 'currently,' because best practices change over time. They represent our current understanding of the best ways to handle specific problems.
00:01:21.880
A pattern is simply a decision that an expert makes repeatedly; it’s something that is repeatable.
00:01:27.840
So, when we combine these ideas, Ruby best practice patterns refer to a series of techniques that Ruby experts currently use to make good decisions while writing code.
00:01:41.000
If the decisions we make are repeatable, we can continue to make them because we often write similar code.
00:01:54.040
So how do these patterns actually help us when writing code? Well, there are many aspects of optimization when coding.
00:02:00.799
Performance optimization is often important, but I believe one of the most vital things to optimize for is the communicativeness of your code.
00:02:08.200
This is where I encountered a sort of moral dilemma while preparing this presentation.
00:02:14.120
If you're writing code that others can easily understand, that means they can work on it.
00:02:21.120
Consequently, you might not need to work on it, which means they could potentially replace you easily. This raises an important question: how secure is your job really?
00:02:34.400
Studies show a strong correlation between the maintainability of code and the expendability of its author.
00:02:53.640
This leads me to propose a new paradigm called 'unfactoring' from patterns.
00:03:06.440
So, what is unfactoring? It's actually quite simple. It involves taking well-designed software and making it completely unmaintainable through small, iterative changes.
00:03:14.920
This might sound counterintuitive, but let’s explore this idea.
00:03:20.000
Now that you're all curious about how to unfactor your code, here are some tips: look for code that is simple, communicative, and maintainable.
00:03:29.120
Recognize the traits of good code and then work against them.
00:03:41.000
Let's discuss what kind of code falls into the category of things to unfactor. Small methods are bad. Simple classes that don't perform more than one task are also undesirable.
00:03:54.960
Additionally, DRY (Don't Repeat Yourself) code is bad, especially when encapsulation is involved.
00:04:00.840
Perhaps most importantly, the names of variables and methods should not be intention-revealing, as they allow for better maintainability.
00:04:08.760
Looking through your projects, you may find code that you want to unfactor.
00:04:20.879
The first step in this process is ensuring you have a comprehensive test suite.
00:04:27.480
Having thoroughly tested code means that as you undergo the process of unfactoring, you can rely on your tests to guide you.
00:04:33.880
After you have a solid test suite, you can start making small changes to your code.
00:04:40.000
For instance, you can complicate your code by introducing unnecessary coupling or damping.
00:04:51.199
This process can be amusing, but it can also lead to confusion.
00:05:04.120
To illustrate this point, let’s analyze some case studies. Here is an example code snippet from Capistrano.
00:05:11.639
This is a straightforward remote deploy command; very simple and easy to read. It effectively updates a repository cache and then copies it.
00:05:23.560
This simplicity means that anyone can read this code, understand what it does and how to work on it.
00:05:36.280
The first step in our unfactoring process is to complicate this code. Let's break out those methods and inline the code.
00:05:48.200
By removing those clearly named methods, the code becomes harder to read and understand.
00:05:58.720
Maintaining intention-revealing method names is important for comprehension, but we'll eliminate that.
00:06:07.160
Now we have a well-factored method which is simple and easy to read, the goal is to reverse this.
00:06:14.360
Next, we look at a code example from Webby, a lovely static site generation tool.
00:06:21.320
This also demonstrates clear, maintainable methods that do straightforward tasks, but we can unfactor this too.
00:06:28.000
By adding confusion and complexity, we obscure its clarity. Remember, our goal is to ensure our code is not intuitive.
00:06:42.679
As it stands, it's easy to work with, but we can unfactor even the simplest code.
00:06:52.080
Unfactored code represents a challenge that keeps you in your job longer.
00:07:01.320
Now, to move on from our humor, what we really need to focus on is writing maintainable code.
00:07:11.280
Writing maintainable code allows you to solve problems effectively.
00:07:20.000
In our practice patterns, we learn to identify our shared decisions and refine them as we write again and again.
00:07:30.080
The relations of decisions change as we tackle different problems.
00:07:40.000
For instance, how do I give a variable a name that helps capture its intent?
00:07:47.880
Practice regular naming conventions, as this can help in maintaining an overview of the program's goals while coding.
00:08:02.520
Simplifying code through better understanding is vastly important; small changes in approach create cumulative improvement.
00:08:12.560
Patterns help coders create methodologies that support better practices.
00:08:20.960
Using composed methods is one such technique. The basic idea is to break a complex method into simpler, singular methods.
00:08:28.480
When methods are smaller and well-controlled, they become much easier to read and test.
00:08:36.440
It’s significantly more effective to break down methods for clear understanding and documentation.
00:08:42.120
Let’s return to Capistrano as an example. An unfactored method awaits refactoring.
00:08:53.600
By restructuring and allowing clarity over complexity, we create confidence in our deployments.
00:09:00.800
Next, explore the execute around method; a technique essential to establishing control in our operations.
00:09:06.960
Every time you need assurance that two operations happen in concert, this method helps with that.
00:09:17.000
Moving onto Ruby's implementation, we can appreciate how it manages resource allocation.
00:09:24.000
Writing good Ruby code means managing memory effectively.
00:09:34.720
Equally vital are around filters in Ruby on Rails, which provide seamless operations before and after action calls.
00:09:40.280
It’s important to ensure precautions are in place for unavoidable exceptions and errors.
00:09:46.480
This quality is paramount, as is the performance of code.
00:09:53.640
Each technical decision should be purposeful, grasping the balance of elements.
00:10:02.120
Tools such as tap functions in Ruby, help encapsulate and clarify execution.
00:10:11.440
By implementing these techniques consistently, you're ensuring greater control over result implementations.
00:10:20.080
These manipulation fundamentals can help developers code with intent, avoiding ambiguity.
00:10:31.120
Next, recognize state in method objects when dealing with repeated parameters; this method structure provides clarity.
00:10:39.960
Such a class holds behavior and state through execution.
00:10:45.480
The output generator class can consolidate repeated tasks, eliminating redundancy.
00:10:51.440
By breaking down the code, we can ensure better comprehensibility and less overhead.
00:10:58.960
Deliberate application of design patterns not only simplifies our coding but fosters better practices.
00:11:06.120
Patterns bring structure to our methods, especially when scaling complexity.
00:11:17.200
As you implement these learnings in your coding endeavors, remember their context.
00:11:24.480
Performance and understanding should be fundamental in each action proposed.
00:11:31.640
Applying these patterns to encourage simplicity promotes confidence in your code.
00:11:39.680
It's vital to encourage readability and checkpointability in practice.
00:11:44.760
Ultimately, striving for simplicity over complexity builds a responsive code environment.
00:11:52.840
Clear variable naming is essential when creating complex structures.
00:12:01.240
Be mindful of your decisions; they will define your outcome.
00:12:06.960
To improve, engage with code structure and patterns to encourage maintainable practices.
00:12:14.320
Always keep your expertise eager; embody the spirit of a learner, for it never concludes.
00:12:20.720
How do we learn these patterns over time effectively? Start small, challenge your decisions.
00:12:27.200
Growing toward intentional coding fosters better skills.
00:12:36.160
Collaborate with others, understand their decisions as well as your own.
00:12:43.680
Diving into open-source, facing and adapting different patterns, fosters rich learning.
00:12:52.960
Experimenting with an understanding, adjust and practice your own methods.
00:13:02.400
I encourage you all to embrace change and document your patterns.
00:13:10.160
Patterns will surely align your goals with real-world applications.
00:13:19.120
Join me in learning by each opportunity, make these your own.
00:13:29.280
Thank you for your attention; I look forward to further discussions.
00:13:35.360
If there are questions, let’s begin that dialogue.
00:13:41.360
Yes, it seems many of us are familiar with Small Talk best practice patterns.
00:13:49.360
Do you believe Ruby has unique patterns distinct to itself?
00:13:57.280
Ruby is indeed idiomatic, and it displays certain creative patterns specific to its nature.
00:14:05.360
This requires more emphasis on elegant coding within Ruby's framework.
00:14:11.200
Ruby does this distinctively, promoting an individual idiom.
00:14:16.200
My current writing includes Ruby best practice patterns expanded beyond previous texts.
00:14:28.320
I hope to encourage awareness for advancing Ruby's syntax; it is worth exploring.
00:14:37.760
Are you contemplating beta-testing that work?
00:14:49.320
Not currently, but I would love to explore that in the future.
00:14:52.800
It’s vital that we keep alignment between intent and implementation.
00:15:00.320
For now, let's break for lunch, and thank everyone for participating!