What's your opinion of the Ans Hack?
The Ans Hack is a dubious way to shave a few points off your solution score. Instead of a standard answer like this
function y = times_two(x)
y = 2*x;
end
you would do this
function ans = times_two(x)
2*x;
end
The ans variable is automatically created when there is no left-hand side to an evaluated expression. But it makes for an ugly function. I don't think anyone actually defends it as a good practice. The question I would ask is: is it so offensive that it should be specifically disallowed by the rules? Or is it just one of many little hacks that you see in Cody, inelegant but tolerable in the context of the surrounding game?
Incidentally, I wrote about the Ans Hack long ago on the Community Blog. Dealing with user-unfriendly code is also one of the reasons we created the Head-to-Head voting feature. Some techniques are good for your score, and some are good for your code readability. You get to decide with you care about.
10 Comments
Time DescendingI think it is perfectly fine and it should be allowed. When I first saw it I had to scratch my head and then agree it was a clever hack. Of course it is no longer clever since everybody (all the winning solutions) uses it.
This reply was flagged by Dyuman Joshi
I think it is allowable to let the matlab assign some values which do not have a specific variable to a default variable called 'ans' instead of blocking it. People use command line more oftenly finds this feature useful so that there's no need to assign a variable and may help to get recent work using ans variable; which is somewhat familiarized with. This is my opinion and any that anyone suggest is preferrable.
I've used it, usually on spam or repeated problems. I like it least when I'm trying to figure out someone's code on a harder problem. Getting the low Cody score doesn't motivate me too much--except when I have a code that's quite a bit smaller than others. Those cases are usually due more to a completely different approach than to shaving a couple of points with the ans hack.
I also think about William's submissions, which never use the ans hack. His Cody scores are almost always among the higher ones, yet he has solved more problems than anyone. There's a lesson in there for me--if only I were smart enough to learn it!
goc3 makes an interesting point about runtime. The timeout feature imposes that idea in some way. For example, I've thought about awarding myself points for codes that solve some of the cases of Ramon Villamangca's problems but time out on the rest. Ramon's problems are diabolical (in an admirable way!)
What I seriously dislike is it teaches newer users a programming style that is actually a bad one. Yes, it hacks the scoring in Cody. But since Cody is viewed as a great way to learn MATLAB by many people, including me, the ans hack preemptively hurts those new users. We should never be encouraging poor programming styles.
I agree with many of the sentiments in the comments to your (linked) blog regarding this topic. Have I used this hack? Yes. After all, there is a badge for having the lowest score to an answer... Do I like this hack? No. Is it an aspect of good code? No. But, in attempting to suppress some bad behavior, another bad behavior is prone to pop up.
My suggestion would be to change scoring to runtime. If only MATLAB had built-in timing functions...like tic/toc, timeit, or cputime.
It's a MATLAB feature, and a harmless one at that, so it strikes me as fair that it should remain allowed on Cody.
I wouldn't use it in real code, naturally, but Cody is not always about that: instead, it is, first and foremost, about fun. Sometimes that means golfing, and using ans to shorten your code is just one of many, MANY ways of golfing code like you never would if you were actually going to use it for anything in real life.
P.S. - do people actually use the head-to-head voting feature? I feel you should perhaps incentivize it - say, by letting people earn a bonus point (up to a daily maximum of five or so) for using it.
Sign in to participate