Declare variable near their usage, also in tests
I’m still trying to decide if I still like RSpec or not. Definitely I don’t want to suggest to ditch it if I’m the only team member that doesn’t like it.
I want to make sure that we still use good coding practice, even if I don’t like the syntax.
I’m not a fan of let statements. But there are definitely a couple of usages that makes it harder for me to understand what’s going on in a spec:
- Lazy loaded by default: that makes debugging somewhat not obvious. If you don’t need them to be lazy loaded, please use let! or just use a variable.
- Far away from their usage: the further away they are from the usage, the harder they are to understand. This is true for non-test code too. Please read: http://c2.com/cgi/wiki?DeclareVariablesAtFirstUse. A corollary of this is, keep your context small. I don’t want to scroll up for several pages before finding the declaration of a variable used in a spec.
- Used to setup another variable: if the only use of one of this lets is to setup another one, then just declare a local variable in that block declaring the latter.
I think they are reasonable guidelines. I’d love every spec I work with is written with them in mind
Interested in making an Impact? Join the carwow-team!
Feeling social? Connect with us on Twitter and LinkedIn :-)