3 ms·
Most of these have conventions. For copy, move, etc the convention is from target, to destination. And an IDE will show you the parameter names. If you still ge
by z3t4 7y ago
Most of these have conventions. For copy, move, etc the convention is from target, to destination. And an IDE will show you the parameter names. If you still get it wrong, there is also no guarantee that you would assign the correct type either. eg. mixing up foo and bar in {from: foo, to: bar} or foo:From, bar:To Also if there is any speculation you should always read the source code of the function you are calling, or read the documentation, and try the function in a REPL to see how it behaves. There is a line where over- verbosity will have the opposite effect eg. making the code harder/slower to comprehend. So keep it clean and simple. If people get it wrong a lot, make the function figure out the parameters so that it doesn't matter what order you pass them in. And have the function throw a friendly human readable error if you get the arguments wrong!
JavaScript example:
function copy(from, to, overwrite, cb) {
if(typeof overwrite == "function" && cb == undefined) {
cb = overwrite;
overwrite = false;
}
if(from == undefined) throw new Error('First argument "from" not specified!');
if(to == undefined) throw new Error('Second argument "to" not specified!');
if(!exist(from)) throw new Error("from=" + from + " does not exist!");
if(exist(to) && !overwrite) throw new Error("to=" + to + " already exist!");
...
}
- friend-monoid 7y ago* Not all users have IDEs with all that much help (vim, emacs users come to mind), and not all platforms have this ability (consider for example writing serverless functions in an online IDE), and sometimes you do code reviews from an online platform or from your phone, and sometimes you just kinda pull up a piece of code in notepad, and I think you should be able to tell that the code is obviously correct in all of those cases. * In C, the convention is "move(destination, source)". See memcpy, snprintf, strftime, others, but not all functions follow this convention. PHP is known for having lots of functions in standard library with differing conventions. * It's easier to tell that "move(from=X, to=Y)" is correct than "move(X, Y)" though? * I agree with the over-verbosity, but your last point that "annotating your arguments makes your code over-verbose and harder/slower to comprehend; instead, put all these guards and magic in the definition of the function" - isn't that also making your code harder/slower to comprehend, but this time you're putting the mindwork in understanding the function instead?
- z3t4 7y agoAn IDE is sometimes used as band-aid for bad design. I use an IDE for convenience, but try to make my code not depend on it. It's said that the time it takes to type the code is a very small part of software development. Yet programmers are lazy and would write the short-hand if one is available. I think it's best to learn one language, and one code-base well - if you want to be productive. (or maybe become bored) There is a trend in software, that things get dumbed down, and locked down. It shouldn't take 15 years to become productive, but if you aim for one month or less, you would have to sacrifice a lot.