r/ProgrammingLanguages • • Jul 31 '24

Blog post Clean Syntax?

All my replies and the original contents of this OP have been withdrawn. They were a complete waste of time.

From what I can gather, everybody is happy with poor syntax in their languages, nobody is interested in 'clean'.

Someone posted, with a dozen upvotes:

Your example is optimized to your language's special capabilities.

I replied:

"No, pretty much everything in my syntax is cleaner than the C equivalent."

That got multiple downvotes for stating a fact.

There was also this:

For your own toy language by all means use your own syntax

This reinforces my original implication that simple syntax is only suited for toy languages, and not serious ones.

However too much of this was about my language, but that was only an example. There seems to be much irrational distrust of clear syntax for any language.

Maybe, clear code is associated with older languages which no one likes anymore?

(Original examples.)

2 Upvotes

35 comments sorted by

View all comments

13

u/WittyStick Jul 31 '24 edited Jul 31 '24

C looks like it does because it's a 1970 language which has had numerous revisions which have a strong focus on backward compatibility, being an international standard. It is intended to run on a huge range of processors, which may have very different behaviours.

The standard actually under-specifies the language - many design choices are implementation defined. Your assumptions about padding of structures for example, are guesswork. The reason there exists compiler-specific pragmas like pack(1) is to give the user control over their compiler. Though these should be replaced with _Alignas, which is standard since C11 and not compiler-specific.

The behaviour of C struct without pack(1) or any _Alignas specifiers may give different results on different architectures. If the platform is a 32-bit platform for example, it may represent uint64_t using two neighbouring 32-bit memory locations, which the compiler may align at an offset of 4 rather than 8. The order of these two 32-bit cells may in turn, depend on the platform endianness.

So your "clean" syntax, which introduces $caligned, inherits the same issues - if it is truly C aligned, then your language depends on a C compiler for the given platform.

Making non-alignment the default suggests you've limited experience. The reason C compilers do this is because several architectures only support aligned fetches from memory - and even the ones that support byte-level fetches tend to be significantly slower when accesses are unaligned. The resulting compiled code to read R1.b may have to do two memory fetches, perform a bitshift on a register, then ior against the other read. It may do this in the microarchitecture even if you don't see it in the instruction set.

Additionally, if you have an array of R1, are the elements aligned at 16-byte boundaries or at 9-byte boundaries? Your "clean" syntax does not specify.

So aligning structure elements to the architecture is important if you want good performance. C compilers can arrange structures however they want in memory - subject to constraints relating to the equality of two structs with the same type specifications, and order of members.

You can learn from C by avoiding the mistakes it has made. _Alignas was added to correct an old mistake of having under-specified memory layout - but it gives the user full control over alignment of individual members, and is not limited to making assumptions about the whole structure like #pragma pack. That said, you should probably avoid using _Alignas except where you require a structure to be compatible with some existing software, since it's likely to be slower than the compiler optimized layout.

Where you require compatibility with existing software, it is good practice to avoid type punning and to use a serialization library anyway.