Howto - Ruby clarity http://rubyclarity.com/ Refactorings of Ruby/Rails projects Thu, 07 Apr 2016 04:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=5.4.7 Be lazy and don’t keep context in your head https://rubyclarity.com/2016/04/be-lazy-and-dont-keep-context-in-your-head/?utm_source=rss&utm_medium=rss&utm_campaign=be-lazy-and-dont-keep-context-in-your-head https://rubyclarity.com/2016/04/be-lazy-and-dont-keep-context-in-your-head/#respond Thu, 07 Apr 2016 16:39:07 +0000 https://rubyclarity.com/?p=101 Sometimes, code we read makes us to remember context. Consider the following code (it sends an invite email to a user): Lines 2-8 (see above ↑) deal with the case of existing user, and the rest deal with the case of new user. When reading lines 3-5 you have to remember that you're dealing with an existing user, same with lines 12-22, only there you deal with a new user. After all, both existing user and new user cases use

The post Be lazy and don’t keep context in your head first appeared on Ruby clarity.

]]>
Sometimes, code we read makes us to remember context. Consider the following code (it sends an invite email to a user):

Lines 2-8 (see above ↑) deal with the case of existing user, and the rest deal with the case of new user.

When reading lines 3-5 you have to remember that you're dealing with an existing user, same with lines 12-22, only there you deal with a new user. After all, both existing user and new user cases use same variable name: user.

But there's a better way than keeping it in your head. Keeping it in the code:

Reading lines 3-5 (see above ↑), you clearly see that you're dealing with an existing user. And same for the lines dealing with a new user.

Happy hacking!

The post Be lazy and don’t keep context in your head first appeared on Ruby clarity.

]]>
https://rubyclarity.com/2016/04/be-lazy-and-dont-keep-context-in-your-head/feed/ 0