nishuz's blog

By nishuz, 2 months ago, In English

While programming, I waste a lot of time thinking about small and irrelevant details in my code.

For example, if you're given two strings $$$a$$$ and $$$b$$$ of the same length (which is unknown), it's more natural to do int n = a.size(); instead of int n = b.size();, right?

I prefer the first method. However, sometimes I feel bad for not using the second one. I think it's unfair to keep using the first one, so I alternate between the two. But when I use the second one, it looks weird (as I said, the first one is more natural), but I can't change it because I already used it.

It's very distracting. I can't think about the problem because these thoughts run through my mind. I end up wasting a lot of time and getting unnecessary penalties in contests.

Is anyone else troubled by this? What do you do to get rid of these thoughts?

 
 
 
 
  • Vote: I like it
  • +333
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it +365 Vote: I do not like it

Just do int n = (a.size() + b.size())/2; :)

  • »
    »
    2 months ago, # ^ |
      Vote: I like it +241 Vote: I do not like it
    int n = vector<size_t>({a.size(), b.size()})[rand()%2];
    
  • »
    »
    2 months ago, # ^ |
      Vote: I like it +42 Vote: I do not like it

    Why to go $$$a$$$ first!

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      well, it's first in the input as well, so it makes sense

  • »
    »
    2 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Why not >> 1?

    • »
      »
      »
      2 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Because in most cases a decent compiler (GCC & Clang surely are) would optimize / 2 that way. No need to type extra symbols. :)

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    then you will have to alternate between int n = (a.size() + b.size())/2; and int n = (b.size() + a.size())/2;

»
2 months ago, # |
  Vote: I like it +43 Vote: I do not like it

It bugs me sometimes but it's better to just neglect it

»
2 months ago, # |
  Vote: I like it +72 Vote: I do not like it

At least it's not just me

»
2 months ago, # |
  Vote: I like it -23 Vote: I do not like it

Yes, It is just you.

»
2 months ago, # |
  Vote: I like it +16 Vote: I do not like it

It's just not you.It's me too.We have OCD

  • »
    »
    2 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Don't you think that's a bit extreme?

    • »
      »
      »
      2 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      There are different types of OCD...Some OCDs are with very minor symptoms which Don't affect much and some affect a lot.Interesting fact is I Didn’t know I had OCD for all these years until my psychologist pointed it out a month ago

»
2 months ago, # |
  Vote: I like it +128 Vote: I do not like it

Also, when I copy-paste some logic block, sometimes I struggle when I need to modify the "original" instead of the "clone", because it is also unfair: why should I murder the one who gave birth to the other block? In these cases I usually undo the paste and re-paste it before the original instead of placing it after, and then I am free to modify the clone (which is now located before the original in the code)

»
2 months ago, # |
  Vote: I like it +25 Vote: I do not like it

I don't think this is a direct answer to your problem, but one thing you could do is

int n = a.size();
assert(n == b.size());

It still isn't symmetric between a and b but it at least helps you avoid potential mistakes and make clear the fact that a and b are equal length.

»
2 months ago, # |
  Vote: I like it +26 Vote: I do not like it

you can use int n = (a.size(), b.size());, at least, it looks fair :)

»
2 months ago, # |
  Vote: I like it +8 Vote: I do not like it

I don't know if I should use memcpy(a,b,sizeof(a)) or memcpy(a,b,sizeof(b)), so I use memcpy(a,b,sizeof(a,b)) instead :)

»
2 months ago, # |
Rev. 3   Vote: I like it +32 Vote: I do not like it

First-world problems!

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I also have the problem when I feel stuffs are not well aligned that I spent some more time, even if those were the last seconds of the contest, just to fix them align and satisfy my eyes, like this:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <deque>

And sometimes during the contest I even tried to change the variable name to somethings that align perfectly. Like in your problem, I will always try to use more variables just for alignment though it is exactly the same.

int n = s.size();
int m = t.size();
»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I usually avoid if else because of the same reason and end up writing two opposing of conditions instead. I feel that we are neglecting a condtion by wrting else to the opposing if statement.

»
2 months ago, # |
  Vote: I like it -10 Vote: I do not like it

Sometimes I do too.

Just make sure it's equivalent, and then skip the detail.

»
2 months ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

You can prevent the thoughts by using Schrödinger's cat:

Spoiler



Unless you print the random generated you will never know if the cat is alive or not (or in this case A was used or B was used) :D

»
2 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

me too so

int n=a.size();
n=b.size();

I am trying to hold myself back , because I think that is " obsessive-compulsive disorder " for many programers ( include too many libraries , 200 lines for define , the names of variables )

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

Do both int n = (a.size() & b.size()); :)

»
4 weeks ago, # |
  Vote: I like it -8 Vote: I do not like it

Oh, me too

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I once had this with my fingers. When I moved my left hand more I'll try to move my right hand to make them move the same distance, and vice versa. I keep doing this and realized it's a kind of OCD. So I asked my self what's the meaning of this and found no answer. I intensionally prevented me from doing this for like a month or two, and it disappeared.

Try to force yourself stop thinking about which way to choose, or follow a principle like always use the lexicographical smaller variable name, and it should help.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

To be honest, I've suffered from this a few times and it was clearly time consuming. I realized that maybe I don't have a correct approach to the problem so that while I was coding, I needed to consider whether this approach could bring the result. In fact, most of the time on contests I used to think about how to code faster and easier to debug, which isn't so effective in my view. So I decided to solve more problems, that gave me a sense of thinking and observation and it enabled me to save much time. On the other hand, if I can't escape from these thoughts, maybe rereading the text is the good way to follow.