So this question is going to be a little "different" but it's true and I am asking for advice.
I've noticed especially when I program in C and I'm trying to tackle a medium to larger sized problem/application (lots of funcs I have to write), I tend to have what I will call "code confidence" issues. I think it's because I do software security at work, I've become paranoid when I am coding myself and it slows me way down. I've come to this realization because I often find myself questioning how to do things "Maybe that's not the best way... Maybe there's a gaping security or performance hole here... Is this loosely coupled enough???" etc....
It gets to the point where I sometimes search for someone else's implementation on Google but when I do, I often find that the other person either did exactly what I did, something else that I knew but was afraid to try, or something even uglier actually.
For example, I was writing a demo module of a program recently and I needed to convert a struct to a string to hash the data. Well, I was like "uh oh, haven't had to do something like that in a while..." etc etc... And just overall was overthinking it.
I then found another guy's example and he simply wrote something like these few lines and was done with it:
While of course I haven't tested it enough to see if there are any nasty bugs, the point is this guy took 30 seconds to get a basic toString going whereas I was sitting there overthinking it and that code is something I definitely could have thrown together easily. It's not so much I don't know how as it is I don't trust my choice.Code:char* to_string(BLOCK * block) { char *string_data = malloc(sizeof(BLOCK)); // Using a char* instead of a struct BLOCK * makes the data into a "string" if(!string_data) return NULL; memcpy(string_data, block, sizeof(block)); return string_data; }
Has anyone else experienced this and do you have tips on dealing with it? I feel like I could bang out code a lot faster if I didn't do this. By the way, I'm not necessarily talking about production code here, but this program I'm working on is sort of a "rough draft" anyway.
It probably comes from the fact that I'm in the software security field and also that I've not been a 100% full-time professional C coder but rather I have to constantly changes languages at work and I'm generally doing smaller things with each one rather than a large-scale C program, etc...