DEV Community

Andrij Nitrogenesis
Andrij Nitrogenesis

Posted on

Zig experience of a C++ developer

Hello there. This is my first blog post on dev.to, and I've decided to make it about my personal experience with Zig from a perspective of a long-time C++ developer.

Introduction

So, let me quickly introduce myself. My name is Andriy, I am a 29 year old Ukrainian kid, that is really into tech and programming. My journey began a long time ago with C, then C++, and now I am here. In this blog post I would like to share my thoughts on Zig, and was it hard for me to get into it.

Lettuce begin.

What's Zig anyway?

For those who lived under the rock for the past few years, there's a brief explanation of what Zig is: Zig is a modern systems programming language, that was made by Andrew Kelley and is still in it's early development. Zig is trying to be not a C competitor, but an alternative to C. That's a pretty bold statement, yes. Peeking forward, Zig is almost achieving that goal in my opinion.

The Why

Why Zig? That's a good question. Zig is trying to be an alternative to C, and here's why:

1. C is unsafe

Yada-yada, common knowledge. Yes, and it's not just unsafe, it's the opposite of "safe". If you don't know what you're doing, then you're going to shoot yourself in the foot 100%. If you're not careful enough you can make your code vulnerable to any kind of attack, be it a buffer overflow, injection, etc. Zig is actively trying to mitigate that whilst not limiting you and letting you do whatever you want. That's a good thing for sure.

2. C has a weird syntax

Of course, this mostly is addressed to C++ rather than C, but still my point is clear. For example, who thought of making streams be read and written into using the bit shift operators (>>, <<)? That looks obvious at first glance, for sure, like "I am reading from the stream so I 'push' >> it's contents into something else", but it still doesn't make much sense. Take a peek at the Zig syntax:

const std = @import("std");
const log = std.log.scoped(.@"my_cool_app");

pub fn main() void {
    std.debug.print("My name is {s}", .{"Andrij"});
}
Enter fullscreen mode Exit fullscreen mode

As you might see, Zig doesn't have, e.g, the :: access operator, everything in anything can be instead accessed by the good ol' dot-access operator. Also, functions that are built into the code by compiler are marked with the @ symbol, which makes it possible to create functions with similar names. Types are also pretty consistent in Zig. E.g, take a look at the following C code:

struct MyStruct {
    int my_field;
    float my_other_field;
};
Enter fullscreen mode Exit fullscreen mode

And now look at the same code in Zig:

const MyStruct = struct {
    my_field: i32,
    my_other_field: i32,
};
Enter fullscreen mode Exit fullscreen mode

I think you can agree with me, that the Zig code is much easier to read and understand.

And that all gets me to my next point:

3. C is inconsistent

I think that this time this is addressed both to C and C++. For example, in C++ classes should be named in PascalCase, so it will be distinct. But I guess that rule doesn't affect the STL developers themselves, as all classes in STL are snake_case. So as functions. So as fields. Why? I have no idea, but that makes it so you can't actually distinguish a class from a function, and a function from a field, which makes it really hard to work with unless you a) Opened a third eye and learned the whole STL, and b) You have intellisense or online documentation. In Zig you have some kind of core guidelines (so does C and C++, actually) which tell you the following: ALL variables/constants/fields should be snake_case, functions should be camelCase, structs, enums, unions should be PascalCase, and types (Enum, Struct, etc.) should be in PascalCase as well at any circumstances (even the builtin functions working with types are PascalCase). So this really brings a good amount of consistency to Zig, as you can write understandable code and read code that was written by no matter who.

My experience

So, let's get to the actual story of my Zig journey. It all has started this spring. Of course, I knew about Zig's existence almost from it's start, but that's not the point. I was really sick of C++ and was looking for an alternative. My options were Go, Rust, C#, Java and Zig. Zig in particular stood out for me. You see, I was working with all 4 of the languages from the list before, and I'll be honest, it wasn't really a good experience for me. So, I started digging. I installed Zig, tried to make a some kind of a game-of-life simulation, but it didn't go well. So, then I stumbled upon ziglings, which is a set of exercises to help you learn Zig by fixing broken pieces of code. I found it very useful and, well, it might be the only reason I am working with Zig. You see, it's a really young language. There is so little documentation (although the official documentation helps out a lot, and there is almost no tutorials on how2do stuff, and those that you will find will be most likely either obsolete and/or vulnerable and/or inefficient.

What about my experience while writing code in Zig... It's complicated. As a long-term C/C++ dev I wasn't ready for modern syntax and features, so that threw me off the course pretty quickly. But I managed to recover. But now coding in Zig feels to me like a constant flow of thought, where nothing stops it and you don't have to do premature optimisation, much planning, etc. You just code, and nothing else bothers you.

The concepts of Zig are modern and in some way unique, so you will get how the comptime works, what is std.mem.Allocator and how to use it, etc., etc. For me it was a long journey, being honest. But you will get it eventually as you progress.

Zig has much more features that are actually useful, like named code blocks, inline loops, enum_literal type, and so on, but it'll be too much for this blog post.

I personally believe in the bright future of Zig and I really think that it might be one of if not the best C successors that are available at the moment. But, that's really all about it. That's all I wanted to say.

Thank you for the time you spent reading this, I spent 3 days working on this blog post and now I am pleased that I have shared my story with the community.

To learn Zig, you might want to use these online resources that might help you out:

To see the news about Zig and related stuff, check out Zig news.

My socials:

That's really about it. Cya.

Top comments (0)