4 ms·
Interestingly, the factorial function is typically used as a trivial example case for introductions to learning a language. Used for the purpose of learning fun
by giberson 16y ago
Interestingly, the factorial function is typically used as a trivial example case for introductions to learning a language. Used for the purpose of learning function calls and recursion and as such we tend to think of it in the simplest context.
From that context, the factory function makes this article a great example of over designing. And certainly in some senses the point of the article is quite accurate.
However, there exists another less common context for the factorial function--serious usage of the function. Not as an introductory example to the language, but as the basis of actual software that has purpose. In this context questions like memory usage, processing power, and even mathematical precision come in to play. Is BigInt large enough for the required use? Maybe you need to use a class based number type capable of having a limitless value. Perhaps you are targeting a distributed processing system, or a single super computer. Will your computing have memory limitations, processing limitations? Suddenly, when considering real applications for the factorial function, all those satirical over-design considerations are now principal necessities that you'd curse the programmer for not having implemented.
Ultimately, the key is scope. It's necessary to know the intended scope of the functionality you are coding. You need to consider who (will use code), how (the code will be used) and where (the code will be used [platform]). In Java, the big GET is the DRY philosophy. An immense library of code is available for your reuse. And it's certainly true when perusing the code base of those libraries you'll see a lot of what might seem like over-designing. However, when you consider the intended scope, it's rarely actually over-designed. Because frankly the scope of most code in the library boils down Everyone (who), Any purpose (what), Any platform (where). In such a scope, simple three liner utility functions seldom are sufficient or appropriate.
- powera 16y agoUm, this problem really is that simple. If you're doing something where you really need many factorials, you use a lookup table (and probably don't call a factorial function at all), or you use a closed-form function to give an estimate. And it's going to be painfully obvious which one you want.