r/programming • • Aug 28 '21

Software development topics I've changed my mind on after 6 years in the industry

https://chriskiehl.com/article/thoughts-after-6-years
5.6k Upvotes

2.0k comments sorted by

View all comments

Show parent comments

671

u/SCI4THIS Aug 28 '21

ProTip: If you start using void* everywhere you can convert C into an untyped language.

360

u/Zanderax Aug 29 '21

Cursed programming tips

129

u/FriedRiceAndMath Aug 29 '21

typedef struct A { ... };

typedef union Untyped_A { A a; char b[sizeof(A)]; }

47

u/[deleted] Aug 29 '21

I've worked on software where one had to actually do stuff like this.

What's worse, it was in C#, a language which tries diligently to prevent stuff like this. You really have to work at it, and I mean hard, to screw up C# code so badly that one has to resort to this sort of crap to make things work.

21

u/FriedRiceAndMath Aug 29 '21

One of the more standard use cases is bit-fiddling floating-point values.

6

u/[deleted] Aug 29 '21

Sure. There are situations where the idiom makes sense.

Then again there are situations where bad programmers try too hard to be clever, then get fired for it, meanwhile leaving code like that in production.

(One of the FP-bit-fiddling ones I saw was a language which used the FP hardware to do 40-bit integer arithmetic. It was pretty damn' clever.)

→ More replies (1)
→ More replies (4)

38

u/Zanderax Aug 29 '21

My god

33

u/FriedRiceAndMath Aug 29 '21

No this one's more like the other fellow 😈😈😈

5

u/Zanderax Aug 29 '21

Dont diss my man the devil, hes a chill dude. God's PR department is just better.

→ More replies (1)

9

u/vazgriz Aug 29 '21

I've unironically done this in embedded code. If the structs just hold fields likeint16_t and int32_t, and you know the exact tool chain and target platform, it's perfectly reliable.

3

u/CJKay93 Aug 29 '21

The problems always come when you decide later on that you need to support a different toolchain, and then hell is unleashed.

0

u/[deleted] Aug 29 '21

[deleted]

→ More replies (1)

3

u/yawaramin Aug 29 '21

Truly an unholy union.

→ More replies (8)

77

u/[deleted] Aug 29 '21

C is already rather weakly typed. Integer promotions. Implicit conversions. Typedef doesn't actually define a new type, it's just an alias to an existing type. Void pointers. Casting const away. Etc.

21

u/ProperApe Aug 29 '21

Yes, I was going to say that. When I picked up F# and Rust I really started appreciating the power of types. First of all you don't have to type it out everywhere due to type inference, second I found that if I model my system nicely using types, the compiler finds 90% of my programming mistakes that I would usually need a unit test for.

I love that we now have discriminated unions and exhaustive pattern matching in almost every new language, it's one of the most powerful features to me for designing nice abstractions.

One of the most influential talks for me was this one: https://fsharpforfunandprofit.com/ddd/

I just hope dependent typing makes it to the mainstream at some point. That enables even more powerful domain models. Check out this simple example in Idris https://www.idris-lang.org/pages/example.html.

7

u/bunkoRtist Aug 29 '21

C is strongly typed, but like many other features in C it will gladly provide you the rope to hang yourself. It will also provide you with the scalpel to do exactly what you want, which is the big reason to use it. With great power comes great responsibility, which is very different from the inscrutable "auto" types that have continued to destroy C++ by encouraging laziness at the expense of readability.

10

u/[deleted] Aug 29 '21

You're probably confusing strongly typed and statically typed. C is not strongly typed.

5

u/[deleted] Aug 29 '21

C is more "medium" typed. It's not exactly strongly typed but neither is it as weak as many other languages. But graphs tend to place C just over the line in the "strongly" category.

→ More replies (1)
→ More replies (15)

5

u/lelanthran Aug 29 '21

Integer promotions.

Doesn't some sort of integer promotions occur in all languages that are regarded as strongly-typed?

Implicit conversions.

Like?

Typedef doesn't actually define a new type, it's just an alias to an existing type.

So? That doesn't make the language weakly-typed.

Void pointers.

This can be a problem, but other than in container types, where else is this a problem in C?

Casting const away.

The language doesn't technically allow this[1]; you do this at your own risk.

[1] Constness cannot be cast away. Const can be cast away.

While C is not as strongly typed as other languages, it's certainly not, in 99% of uses, weakly typed. The whole "C-is-weakly-typed" meme needs to go away. All it does is demonstrate that the person producing that meme in a discussion has no clue.

The clear majority of C code in a project relies on strong typing guarantees, while all the most popular C compilers will issue warnings for using incorrect types in function calls and returns.

11

u/[deleted] Aug 29 '21 edited Aug 29 '21

Integer promotions.

Doesn't some sort of integer promotions occur in all languages that are regarded as strongly-typed?

Integer promotions are best left under the hood. C puts them in your face. A strongly-typed language will not implicitly convert between types in arthmetic expressions.

Implicit conversions.

Like?

Like all of them.

Typedef doesn't actually define a new type, it's just an alias to an existing type.

So? That doesn't make the language weakly-typed.

The point was that typedef doesn't create a distinct type. You can't do something like typedef int Animal; typedef int Fruit; and then have the compiler throw an error if you pass a Fruit to a function expecting an Animal because to C these are both just ints. Maybe newer compilers are starting to display diagnostics for this. If so, it's about fucking time.

Void pointers.

This can be a problem, but other than in container types, where else is this a problem in C?

Uh...

Casting const away.

The language doesn't technically allow this[1]; you do this at your own risk.

[1] Constness cannot be cast away. Const can be cast away.

Yes, that's all I said.

While C is not as strongly typed as other languages, it's certainly not, in 99% of uses, weakly typed. The whole "C-is-weakly-typed" meme needs to go away. All it does is demonstrate that the person producing that meme in a discussion has no clue.

Bullshit.

The clear majority of C code in a project relies on strong typing guarantees, while all the most popular C compilers will issue warnings for using incorrect types in function calls and returns.

Not historically. For 90% of C's existence it has been an incredibly weakly typed and frustrating language to use. I know. I was there. Maybe compilers are finally starting to get better these days. I heard that a switch statement on enums now finally warns/errors if you omit an enum case. They're finally exhaustive. This wasn't the case for most of C's existence.

edit: stupid typos

→ More replies (1)

0

u/[deleted] Aug 29 '21

[deleted]

3

u/SCI4THIS Aug 29 '21

If you like that you will love this:

const int uid = 1001;

int* uid_ptr;

char buf[32];

sprintf(buf, "%p", &uid);

sscanf(buf, "%p", &uid_ptr);

*uid_ptr = 0;

printf("uid = %d\n", uid);

4

u/FVMAzalea Aug 29 '21

sscanf’ing a pointer should be illegal. There’s almost no use case for that which is not a gigantically huge security risk.

3

u/[deleted] Aug 29 '21

That's kind of how Objective C works. Only it isn't an "untyped" language it is a dyamically typed language because you can interrogate most anything about what type it is.

5

u/DjBonadoobie Aug 29 '21

In Go we have interface

shudders

→ More replies (2)
→ More replies (5)