While reading “Python scripting for computational science” I came across the following text in the section discussing generators:
Whether to rapidly write a generator or to implement the class methods
__iter__and__next__depends on the application, personal tast, readibility, and complexity of the iterator. Since generators are very compact and unfamiliar to most programmers, the code often becomes less readable than a corresponding version using__iter__and__next__.
This led me to wonder whether unfamiliarity (of other programmers) is a good reason NOT to use relatively new and powerful features of a language (like Python generators).
If you don’t use it, how can it ever become popular and familiar?
So, my question: is unfamiliarity sometimes a good reason not to use new language features?
Your own unfamiliarity with a language feature may be a good reason to tread lightly. For example, in C#, if you aren’t certain about the differences between
object y = func1() ?? func2();andobject y = func1() != null ? func1() : func2();(hint: left-to-right order of evaluation), then maybe you are better off writing the correspondingifclause just because it’s clearer what actually is going on. Someone who knows the nuances of the language better may very well come around and refactor later, and in the meantime, the cost is usually low.However, if you know how to use a language feature, I see little reason to avoid using it simply because others may find it difficult to understand. If you really feel the need to, then add a comment (such as, perhaps, “
?? is the _null coalescing operator_“) to help fellow developers know what to look for if they can’t figure out from the code alone what it is doing, and you are afraid that they may have to go it alone.This, mind you, is about production code. Experimenting certainly has its place, but its place is not necessarily in the mainline codebase. I always keep a “scratch” project handy for when I want to try something out without risking impact to anything else. There, I often take liberties far beyond those I take in production or to-be-production code.