Ned Gulley

What's your opinion of the Ans Hack?

Ned Gulley on 11 Jun 2024 (Edited on 12 Jun 2024)
Latest activity Reply by Alex on 25 Jun 2024 at 16:31

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;
you would do this
function ans = times_two(x)
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.
Alex on 25 Jun 2024 at 16:31
I 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.
Kalhara on 19 Jun 2024
This reply was flagged by Dyuman Joshi
Hello @Ned Gulley,
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.
Dyuman Joshi
Dyuman Joshi on 19 Jun 2024
Hi @Kalhara, Thank you for your feedback however this question is about MATLAB Cody, the online question solving platform, not about MATLAB - the programming language/software.
ChrisR on 15 Jun 2024
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!)
John D'Errico
John D'Errico on 13 Jun 2024
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.
Ned Gulley
Ned Gulley on 13 Jun 2024
I think of Cody not so much as a "coding school" but rather as a place where it's convenient to teach yourself to code. It's convenient because in one place you get to see so many ways to solve the same problem, good, bad, and ugly. Yes, the scoring encourages people to write abbreviated code, but it's my opinion that along the way it helps people develop good taste. I feel like it has worked that way for me, anyway.
goc3 on 11 Jun 2024
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 tic/toc, timeit, or cputime.
Christian Schröder
Christian Schröder on 11 Jun 2024
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.
Dyuman Joshi
Dyuman Joshi on 16 Jun 2024
I don't think it is completely harmless, as pointed out by John and Grant.
Yes, it could be used to shorten the code size wise, but the penalty might go to the performance of the code. (It would be interesting to check out what is the performance difference is when defining a variable vs using "ans" )
But if someone is aware about the cons of using "ans", and as you mentioned, is just using to have fun, then by all means continue.
I second your suggestion regarding incentivizing the head-to-head voting feature to provide it the momentum it needs to gain traction.
Ned Gulley
Ned Gulley on 12 Jun 2024
You're right that the head-to-head voting feature is not widely known and used. I was (am) excited about the possibilities, but as you say, we may need to provide more encouragement for people to use it.

See Also


No tags entered yet.