DEV Community

Discussion on: What's Your Coding Quirk?

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

In JS, to add +1 value I don't usually use the ++ operand, instead I use += 1...

let conter = 0;

// Drake meme: rejecting
counter ++;

// Drake meme: approving
counter += 1;
Enter fullscreen mode Exit fullscreen mode

It just feels more semantic to me, haha.

Collapse
 
sjmulder profile image
Sijmen J. Mulder

I'd be fine with both of those but I also relish in exploiting post-increment. *dst++ = *src++ yesss

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

What it does??

Thread Thread
 
sjmulder profile image
Sijmen J. Mulder • Edited

It's a string-copying idom in C. Full context:

/* copy string into buffer */
void strcpy(char *dst, char *src) {
        while (*src)
                *dst++ = *src++;
}
Enter fullscreen mode Exit fullscreen mode

It's equivalent to:

void strcpy(char *dst, char *src) {
        while (*src) {       /* while src isn't pointing to nul character (terminator) */
                *dst = *src; /* copy the char at 'src' to char at 'dst' (both pointers) */ 
                src += 1;    /* increment src pointer to next char */
                dst += 1;    /* increment dst pointer to next char */
        }
}
Enter fullscreen mode Exit fullscreen mode

It's really not something to make a habit of but in this specific context, C string copying, it's a common idiom.

See also sjmulder.nl/2023/casey-interview-2...

Thread Thread
 
schemetastic profile image
Schemetastic (Rodrigo)

Haha, I don't think I'll be learning C in this life, but I'm surprised by people who have the strength to learn it. Kudos.

Thread Thread
 
sjmulder profile image
Sijmen J. Mulder

Fair enough! I learnt it years ago downporting a little C++ game for practice and finding that it forced me to write simpler and more to-the-point code. Essentially: data-oriented programming. Those concepts are useful in other languages too!

Thread Thread
 
schemetastic profile image
Schemetastic (Rodrigo)

Great! If you know C I would bet that you know a few other languages.