Is it ok to write unsafe unoptimised code?


This might seem like a silly question, but here’s the thing.

For solving these problems, we are given a very specific entry. It has its own format, and there is a small amount of possible errors in given input.

I usually make very minimal checks when parsing the incoming data and write extremely unsafe code to sort it out without making a -in this specific problem-solving instance- somewhat unnecessary number of verifications.

Say, if the input data is an int with specific constraints, I don’t check to make sure it verifies these constraints, unless I specifically need to when solving the problem. If it’s a ‘xxxx;xxxx’ format, I don’t check to make sure that there is only one semi-colon, I write dirty code to parse it and give me xxxx and xxxx without too much hassle.

Since I’m also still in the ‘easy’ section, I tend to write mostly unoptimised code and don’t look out for the best data structures. So far, anyway. I’m fairly sure that once I start hitting the harder problems I’ll need to find more efficient data structures, find faster algorithms, etc.

I do tend to make notes of where and when my code is unsafe or much slower than it could be, but overall, would you agree with my approach or would you say that I should write a proper parser everytime and optimise my code to the point of overkill for every problem ?

It is safe, you can trust the Codingame team. :wink:

About code safety, it’s not such an easy question but I think you shouldn’t care of it as long as you know it’s unsafe. If you’re already aware of that kind of real world issues, you won’t learn anything new; if you’re not, nothing will tell you that you did wrong anyway though you can still score 100%.

About optimisation, easy puzzles aren’t focused on it and there’s not a lot to optimise as they’re more here for basic training (traversing arrays, comparisons, and so on) so you should probably not spend too much time on them. Also, on some harder puzzles you won’t have the choice to optimise at least a bit if you want to score 100% thus it’ll be more interesting because you’ll know if your code is too slow.

1 Like

That kind of answer satisfies me, so unless someone has an interesting objection to put forward, I’ll continue doing things this way.