Zeals TECH BLOG

チャットボットでネットにおもてなし革命を起こす、チャットコマース『Zeals』を開発する株式会社Zealsの技術やエンジニア文化について発信します。現在本ブログは更新されておりません。新ブログ: https://medium.com/zeals-tech-blog

Tried Programming Rust lang[Rustをやってみた!!]

f:id:zeals-engineer:20190209181754p:plain

フルスタックエンジニアのAaronさんがRust言語についての記事を書いてくれました!
(Aaronさんの社員インタビュー記事もよろしければご覧ください!)

www.wantedly.com

Aaronさんは新しい技術への感度がとても高くて、ZEALS内でいち早くRustにも取り組んでくれました。
Rustを触って感じた言語の特長をまとめてくれましたので、Rustに興味のある方はぜひ読んでみてください!

後半では、Aaronさんの書いてくれた文章を日本語訳していますので、英語が苦手な方はそちらをお読み頂けると嬉しいです!

Tried Programming Rust lang

Introduction

This blog post is about typing. Specifically, typing in programming languages and about the concept having (hopefully) a positive impact on my coding style.

Although I am primarily a JS/Ruby developer by trade, I do experiment with other languages and have been looking at the benefits/drawbacks of adopting another lower level language, or even doing something like Typescript on the frontend.

I make games as a hobby. Which led me to programming in Rust, a systems-programming language by Mozilla.

www.rust-lang.org

Yeah, I know you can make games in JS (and it's actually quite fun, check out Pixie JS), but I wanted to make something really fast without all the minor hinderances of programming for a browser.

What I've come back with so far is an appreciation for types.

So what I've noticed is:

1. Compilers are nice

I went through an entire code tutorial with about 300 lines of code and never ran the program once.

I followed along typing and understanding what was happening in the tutorial and before I knew it, the tutorial was over.
It's not necessarily that dangerous in JS, but I had some apprehension about the debugging process that awaited me.

doc.rust-lang.org

But to my surprise, after only a few spelling misses and missing or wrong function parameters the program booted up dutifully and it ran perfectly!
Although one can never be sure about what kind of bugs may lie in the dependencies I downloaded, my code seemed bug free.

It made me think about JS/Ruby, and how I might not see a lot of bugs until I actually test the program.

Yes, I know you can write tests for your code, but the types were like little annotations for all my functions without writing one test.

There were no questions about function arity, no doubts about me putting the wrong kinds of arguments somewhere, I just had a confidence that the program worked.
And that it would work even if another person had edited it.

2. Structs are nice

To me, JS is the ultimate free language, with a huge price that comes with that freedom.

Especially when working in big teams, where people come and go, you can always enforce every single good-practice.
And, to be honest, sometimes it all comes down to style. But having a ton of object literals in your codebase can make things messy, especially when the object literal really ought to be an enforceable structure with some kind of scheme.

By being able to make your own types, you can ensure that an object/hash is made the same way every time.
And new languages like Rust/Golang have these convenient structs that don't force you into making entire class to accomplish that job.

And because, by nature, these structs are like schema, I can be guaranteed that they're used the same way everywhere.
I don't have to consult documentation, or the programmer that wrote it.

Effect on my style

Although I couldn't just make everybody adopt Typescript at work tomorrow, nor do I think I'd want to, I can use some of what I've learned to get the benefits of what I have experienced in my time with Rust.

1. We don't have a JS/Ruby compiler, but we can still program defensively

I can't stop JS/Ruby from getting into the master with a bunch of runtime errors, but I can have my program let others/myself know when they're/I'm using my code incorrectly by making classes which will liberally throw errors when the wrong parameters are passed into the constructor.

They say that "prevention is the best cure" and we can prevent people from using our code outside of the way we expected them to use it, we also prevent them from using it in ways we didn't expect.

2. We don't have structs, but we can try our best when it really counts

I'm not advocating for every JS/Ruby function to manually check the parameters of every function, but when it really counts don't just expect the functions we're using to behave the way we want them to. Important functions that rely on important parameters must check their inputs.

Typed languages will let you know precisely when you're "doing it wrong", but JS will just smile at you nod. An extra if-statement will probably take you 10 seconds to write and save you a lot of headaches in the end.

Closing thought

As web developers, increasingly were sending receiving tons of JSON all the time.
And I can't count the amount of hours I've wasted just debugging payloads to or from servers that don't have the correct stuff in it.

Of course, I love the flexibility that JSON provides, but I think this is one of those parts of web-development that really counts and probably should have a typing/schema.

I would like to experiment more with something like protocol buffers in the future and see if that doesn't relieve some of the headaches, although who knows, by then the next big thing may come along :D

【和訳】Rust言語をやってみた!!

はじめに

今回はコーディングについての記事です!
コードライティングやコーディングスタイルの概念について詳しく書いています。

僕はjavascriptやRubyのエンジニアとしてプログラム開発を行っていますが、他にも色々なプログラミング言語を試していて、低レベル言語やフロントエンドにおけるTypescriptなどのメリットやデメリットを調べたりしています。

Mozillaが提供するプログラミング言語”Rust"を触るきっかけは、僕の趣味が「ゲームを作ること」だったからです!

www.rust-lang.org

普通のjavascriptでゲームを作ること(Pixi.JSとか!)ももちろんできますが、ブラウザ開発にありがちな障害(ファイルシステムにアクセスできなかったり...)がなく、高速でメモリをあまり食わないアプリを作りたかったので、今回Rustに挑戦することにしました。

僕は今回、 ”型”のありがたみ を改めて感じました!
それでは、さっそくご紹介していきます!

気づいたこと

1. コンパイラがすごくイケてます!

今回、僕はチュートリアルで300行くらいのRustのコードを書きました。 チュートリアルの間、途中では一度もプログラムを実行しませんでした。

doc.rust-lang.org

チュートリアルではRustのコードの特徴を理解することに集中しました。
僕はjavascriptではあまり感じないのですが、最後のデバッグに不安を抱いていました...。

しかしデバッグしてみると、驚いたことにタイポや引数を少し間違えていただけで、プログラム全体は正常に起動し問題なく実行されました!
パッケージやライブラリにバグがあるか​​もしれないですが、自分のコード自体にはバグがほとんどなかったのです。

javascriptやRubyを書いていると、プログラムを実際にテストするまで、どれだけ多くのバグがあるかわかりませんでした。

もちろん、テスト駆動開発的に、コードのためにテストを書くことだってできます。
しかしRustの”型”は、テストを書かなくても、すべての関数に小さな注釈があるかのように機能しました。

引数についても、引数の種類が間違っているかを心配することもなくプログラムを書けました。
他の人がRustを書いても、同じようにうまくいくのではないかと思います。

2. 構造体もすごく良い概念です!

javascriptはすごく自由な言語だと僕は考えています。
特に、大規模なチーム開発をするときは、チーム内で良い書き方を習慣づけることもできます。

それらがコーディングスタイルとして定着することもあります。
しかし、コードに大量のオブジェクトリテラルがあると、処理が面倒になることも多いです。

RustやGoには便利な構造体を持っていて、無理にクラスを作らなくても良くなります!
型を作ることで、オブジェクトやハッシュを毎回同じ方法で確実に作ることができます。

doc.rust-jp.rs

これらの構造体は本質的にはスキーマに近く、どこでも確実に同じように使われます。
ドキュメントやそれを書いたプログラマーにいちいち相談する必要はないのです。

コーディングスタイルへの影響

仕事でTypescriptを採用することはしなかったのですが、Rustのメリットの多くを仕事で活かすことができると思います!

1. javascriptやRubyコンパイラはないですが、エラーを防ぎながらコードを書くことができます

javascriptやRubyの実行時にmasterブランチでエラーが出るのを止められないですが、自分のコードが間違った使われ方をされていると、それを教えてくれます。具体的には、間違ったパラメータがコンストラクタに渡されるとエラーをスローしてくれます。

コード自身が「予防が最善の治療法である」ということを教えてくれます。
僕たちは、予想しない方法でコードが使われるのを防ぐことができるのです。

2. 構造体がなくても最善を尽くす

javascriptやRubyの関数でパラメータを手動でチェックすることはあまりオススメできませんが、使用している関数が思ったとおりに動作するとは限りません。
パラメータに依存する重要な機能は、入力された値を確認する必要があると感じました。

型付けされた言語は、間違った値が入力されればそれを教えてくれますが、javascriptならほくそ笑んで終わりです。笑 if文を多用してしまうと、頭を抱えながらコードをなんとか書くことしかできません。

さいごに

Webの開発者として、僕は今回、いつも以上に多くのJSONをサーバーに送り続けました。
そして、デバッグだけで多くの時間を費やしてしまいました。

僕はJSONの柔軟性がすごく好きですが、JSONはWeb開発の一部分でしかありません。
本当に 重要なのは、入力/スキーマを持つことだと思います。

将来的にはプロトコルバッファのようなものを試して、悩みを解決してくれないかどうかを確かめたいですが、その前に、次の大きな変化が起こるかもしれないですね!僕はとてもわくわくしています!

最後までお付き合いいただき、ありがとうございました!