### nishuz's blog

By nishuz, 2 months ago,

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?

• +333

 » 2 months ago, # |   +365 Just do int n = (a.size() + b.size())/2; :)
•  » » 2 months ago, # ^ |   +241 int n = vector({a.size(), b.size()})[rand()%2]; 
•  » » 2 months ago, # ^ |   +42 Why to go $a$ first!
•  » » » 2 months ago, # ^ |   +11 well, it's first in the input as well, so it makes sense
•  » » 2 months ago, # ^ |   +3 Why not >> 1?
•  » » » 2 months ago, # ^ | ← Rev. 2 →   0 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, # ^ |   +16 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, # |   +43 It bugs me sometimes but it's better to just neglect it
 » 2 months ago, # |   +72 At least it's not just me
 » 2 months ago, # |   -23 Yes, It is just you.
 » 2 months ago, # |   +16 It's just not you.It's me too.We have OCD
•  » » 2 months ago, # ^ |   +3 Don't you think that's a bit extreme?
•  » » » 2 months ago, # ^ | ← Rev. 2 →   0 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, # |   +128 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, # |   +25 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, # |   +26 you can use int n = (a.size(), b.size());, at least, it looks fair :)
 » 2 months ago, # |   +8 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 →   +32 First-world problems!
 » 2 months ago, # |   0 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 #include #include #include #include #include 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, # |   0 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, # |   -10 Sometimes I do too.Just make sure it's equivalent, and then skip the detail.
 » 2 months ago, # | ← Rev. 2 →   +2 You can prevent the thoughts by using Schrödinger's cat: Spoilervector arr[2] = {a,b}; int n = arr[rand()%2].size(); 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, # ^ |   0 This is brilliant.
 » 2 months ago, # | ← Rev. 2 →   0 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, # |   +3 Do both int n = (a.size() & b.size()); :)
 » 4 weeks ago, # |   -8 Oh, me too
 » 4 weeks ago, # |   0 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, # |   0 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.