Talks

Ruby and Go

Ruby and Go

by Lional Barrow

In this presentation titled "Ruby and Go," Lionel Barrow, a developer at Braintree, discusses the relevance of the Go programming language for web developers who primarily use Ruby. He emphasizes that understanding Go can enhance a Rubyist's approach to web development, owing to Go's distinct programming style which prioritizes small, composable packages and upfront error handling.

Key points discussed in the talk include:

- Overview of Go: Go is a general-purpose language developed by Google known for its concurrency support and clean toolchain. Barrow clarifies that Go is not merely a systems programming language but a versatile one.
- Go vs. Ruby: The presentation illustrates how Go encourages a structured programming approach as opposed to Ruby's more flexible style.
- Go Agenda: Barrow introduces the "Go agenda," which focuses on:

- Writing small, generic packages.

- Emphasizing composition over inheritance.

- Implementing upfront error handling, treating errors as data rather than exceptions.

- Examples of Go's Package System: Barrow provides a practical example from the Braintree package that exemplifies Go's approach to defining types and public functions, contrasting it with Ruby's method of code execution involving requires.
- Interface-Oriented Type System: He explains how interfaces in Go enable type safety and explicitness, contrasting it with Ruby's duck typing approach, which is less clear about method requirements.
- Error Handling: Barrow discusses Go's method of error handling, where functions return both a result and an error value, promoting cleaner code without convoluted exception mechanisms typical in Ruby.

The presentation concludes with several takeaways:

- Learning and experimenting with Go can provide a better perspective on software development and problem-solving.

- Both Go and Ruby have unique trade-offs concerning flexibility and verbosity, influencing how developers can structure their applications.

- Barrow encourages web developers to explore different programming paradigms to broaden their skill sets.

For those interested in diving deeper into Go, Barrow suggests checking out various informative talks available on YouTube. Overall, this talk serves as an insightful comparison between Ruby and Go, highlighting the reasons for Ruby developers to consider learning Go.

00:00:30.560 Hey guys, I get to talk about Ruby and Go. My name is Lionel Barrow, and I'm a developer at Braintree.
00:00:36.840 You guys might have read that Braintree was acquired yesterday. If you want to talk about that, come and see me at lunch.
00:00:43.320 One other thing is that I've had a little bit of a cold, so if I start coughing into the mic, I'm sorry.
00:00:49.440 Let's talk about Go. Here's the plan: we're going to give a brief overview of Go, discuss what I call the Go agenda, and compare how it works versus Ruby.
00:01:00.440 Then we'll conclude with some takeaway thoughts. So, what is Go? Go is a relatively new language, about five years old, although the V1 specification has only been frozen for about 24 months.
00:01:08.799 It's a general-purpose language developed primarily at Google, and though it is entirely open source, its core team comprises luminaries like Rob Pike and Ken Thompson.
00:01:22.439 Originally marketed as a systems programming language similar to C or C++, many have realized this was a mistake; it should be regarded as a general-purpose language.
00:01:35.759 When looking at the categories of programming languages on Wikipedia, you'll find Go listed as compiled, garbage collected, statically typed, and object-oriented.
00:01:40.880 While it may sound reminiscent of Java, you'll likely find Go quite different. Go is noted for its first-class concurrency support and a strong emphasis on toolchain.
00:01:53.040 It includes features like automatic code formatting and more. While we won't delve into those today, I will note that when Go first came out, it was criticized by many.
00:02:05.520 From a programming language research perspective, Go was deemed extraordinarily boring, as all the significant ideas about its semantics and syntax have existed for at least 30 years.
00:02:18.080 However, the new features that Go introduces mix together concepts like CSP-style concurrency and an interface-oriented type system, blending them in a very pleasant way.
00:02:30.239 If you try it yourself, you'll notice that all the different parts fit together very well. Go is designed by software engineers for software engineering.
00:02:43.080 Significant thought has been put into aspects like dependency management, which, while boring from a programming language research perspective, makes a major impact on our daily lives.
00:02:49.599 Go is gaining traction—this is the most important way to track how popular a programming language is, but of course, hashtags can be biased.
00:03:01.519 Nevertheless, if you logarithmically plot this trend, you can see it's on a steady incline, indicating that Go is doing well.
00:03:08.440 The Go language has a clear agenda. It's a small language with a limited number of keywords and ways to accomplish tasks, driving developers down a specific path when writing code.
00:03:22.640 You might find that if you approach Go with an open mind, you'll be fine, but it's crucial to remember that it follows a top-down style in its design.
00:03:29.360 Unlike Ruby, which has evolved in a bottom-up manner—Ruby has loops that people seldom use—Go aims to push its agenda, encouraging you to embrace its principles.
00:03:41.799 So, what is the Go agenda? It focuses on writing code that centers around small, generic packages and emphasizes composing rather than inheritance.
00:03:47.840 Notably, Go emphasizes upfront error handling and treats errors as regular data rather than control flow mechanisms like exceptions.
00:03:56.400 In this talk, I'll discuss each of these items and compare how Ruby approaches these challenges.
00:04:12.000 Let's begin by discussing small generic packages. A package in Go is somewhat like a namespace or module, but differs in that it can hold internal state and execute computations at initialization.
00:04:25.440 Though it's not exactly the same as a Ruby module, all Go source code is contained within a package, fundamentally acting as a collection of source files grouped by an identifier.
00:04:31.600 Packages are your building blocks in Go, and they require you to think critically about the package API, especially since a name's case dictates whether it can be imported.
00:04:45.039 For example, when viewing code, you can immediately recognize if you're dealing with a public or private method simply by its capitalization, a convention enforced by the compiler.
00:04:56.479 Here’s what Go code looks like in practice. In the Braintree package, we have two top-level public functions: a charge function that takes a credit card type and an amount string, returning a Boolean.
00:05:10.560 We can imagine this Boolean indicates whether the charge was successful, and we also have a credit card struct. Declaring a type like 'credit card struct' is akin to defining a class in Ruby, but here we only set up the attributes.
00:05:29.680 Functions or methods associated with those types are defined separately. In this case, both charge and the credit card type are public, allowing anybody importing this package to utilize them.
00:05:45.400 However, internal details, such as the card number, remain private, and as a developer, you immediately recognize the nuances when browsing the source code.
00:06:01.639 In Go, the main package executes similarly to Java. When looking at our program in the main package, you will notice it imports the Braintree package alongside others.
00:06:17.080 Here, the main function retrieves card number and amount from the command line, creates a new credit card instance, attempts a charge, and then generates output based on success or failure.
00:06:30.720 Importantly, once we've referenced the type once, we don't have to redeclare it—a feature that reduces verbosity and enhances readability.
00:06:36.880 This contrasts sharply with Ruby's 'require' method, which executes a lot of code and can lead to unregistered methods entering the global namespace. Go maintains clarity in where imported code originates.
00:06:54.920 While Ruby's flexibility can introduce ambiguity and complexity, Go enforces a more straightforward import structure, avoiding issues like circular dependencies that would otherwise lead to compilation errors.
00:07:13.279 Next on the Go agenda is prioritizing composition over inheritance. In Go, composition entails classes coordinating with others, while inheritance relies on a defined mechanism.
00:07:29.000 Go's interface-oriented type system plays a key role here; an interface acts as a collection of required methods. Any struct that fulfills the interface's method requirements can be passed into a function that accepts the interface type.
00:07:42.560 This encourages packages to remain generic, as they can simply define interfaces to interact with. However, an example showing repetitive interface methods also highlights an area for improvement.
00:07:56.320 A better interface exists in the standard library’s io package, allowing for much greater utility, and ensuring that types conforming to it can function seamlessly throughout the library.
00:08:05.440 By allowing files and other structures to implement the writer interface, Go facilitates high-level operations while enforcing type safety.
00:08:16.680 Like Ruby's duck typing, Go employs a form of compile-time duck typing with explicit interface declarations, ensuring you know at compile time if you’re using an appropriate type.
00:08:29.880 In Ruby, interface requirements are implicit, mandating developers to decipher them by reading the code, whereas Go makes its interface requirements explicit.
00:08:42.960 Consequently, you can easily reason about what characteristics an interface demands before implementing it, minimizing unexpected behavior.
00:08:56.240 Moving on to the agenda item of upfront error handling, this point is often contentious. By treating errors as ordinary data, Go emphasizes the minimization of exceptions for control flow.
00:09:10.960 Typically, when calling a function in Go, you receive both a result and an error. You check if the error is NIL; if it is, proceed with the result.
00:09:24.320 This practice promotes a straightforward and readable error handling approach, as evidenced by a simple Boolean casting function from the standard library, which wraps potential errors.
00:09:33.680 When utilizing this function in a greater context, you may need to focus on either handling the error directly or proceeding with normal function flow based on the error status.
00:09:50.560 The result is that Go code remains clean and simple, eliminating complex control flow mechanisms that frequently complicate Ruby codebases.
00:10:03.520 Although one downside is the possibility of introducing error checks everywhere, creating surface noise in the code. Developers often end up writing code that looks cluttered with error checks.
00:10:19.200 Additionally, it can be difficult to handle errors locally, leading to leaky abstractions. Passing errors up the call stack could mimic exception handling in some respects.
00:10:34.640 Despite having different operational meanings, facilities for error handling in Go through Panic exist, albeit their usage is not idiomatic.
00:10:47.800 It's critical to acknowledge that programming languages make trade-offs, and learning multiple languages grants insight into the underlying trade-offs.
00:10:59.160 Building familiarity with different languages allows developers to see beyond Ruby, enhancing their ability to leverage the appropriate tooling for given projects.
00:11:14.480 To sum up, while Go promotes certain ideologies—like simplicity, explicitness in interfaces, and structured error handling—it also introduces verbosity at times.
00:11:30.760 Each programming language necessitates a consideration of trade-offs regarding flexibility and verbosity.
00:11:44.240 Ultimately, don't shy away from experimenting with new languages. Engaging with other programming paradigms can lead to richer perspectives and improved coding practices.
00:11:54.320 As we wrap up, if you want to learn more about Go, I recommend checking out a list of talks on YouTube that cover the essentials and advancements in the language.
00:12:02.480 These talks provide insights into Go's design philosophy, its concurrency constructs, and practical applications in building web apps.
00:12:17.720 Before I conclude, are there any questions regarding Go or its applications?
00:12:32.680 I appreciate your attendance and participation today—thank you!