How Ruby Manipulates Variables and Objects and Passes Them Around
- Understand if a method returns a value, or has side effects, or both
- Side effects - displaying something to the output or mutating an object
- Understanding the methods you are writing and using methods from existing libraries, etc., it is critical to understand their side effects vs return values when writing code that is well organized and easy to read
- Avoid using a method that has both side effects and a meaningful return value
- Name methods appropiately, preface the name of the method with whatever the output value is
- A method should do one thing, and be named appropiately
- Don’t write a method that mutates, outputs and returns a meaningful value. Make sure the method does one of those things
- Don’t mutate a collection while iterating through it
- Run rubocop to catch variable shadowing for you!
- Don’t use assignment in a conditional
- Use underscore for unused parameters and ones you don’t care about
- Or if you have an unused parameter when there are multiple parameters
- Gain experience through struggling
- It’s less useful to learn “best practices” without first learning why they’re best practices, learn why first
- Be ok with struggling through the “bad” or sub-optimal practices first
- Spend the time programming, learn to debug through problems, struggle with it, search for the right terms, play around with the code. Another problem occurs, repeat.
- Move repetitive logic into hashes and arrays comes up in all kinds of programs
Written on April 24, 2019
