4 ms·
My fingers are getting real tired from typing if err != nil all the damn time. I do like it for small things - compiling to one (albeit huge) binary and its re
by pydry 6y ago
My fingers are getting real tired from typing if err != nil all the damn time.
I do like it for small things - compiling to one (albeit huge) binary and its relative speediness is nice. I wouldn't use it for large systems though. The amount of code really balloons over time, relative to, say, python and that SLOC correlates to bugs and maintenance cost.
- politician 6y agoHave you considered using a snippet or macro to type the 13 characters for you? Snippets or macros are available in most editors.
- baq 6y agoif everybody could use a macro in their editor or IDE, it's a strong hint that the language could do better. (looking at you, java)
- curryst 6y agoI have debated this. I was actually going to formulate mine into a pre-processor so that if I can change a macro it will retro-actively update the other ones. I thought it would be useful for this, and for structs that I want to replicate for various types. At what point are we just creating a new language that compiles to Go, though?
- andreygrehov 6y agoCurious, what's your definition of a large system?
- apta 6y agoI'm not the person you're replying to, but I've had similar experiences with larger programs in golang. It's quite terrible to deal with, and overly verbose. I'd say anything more than 2000-3000 lines and golang becomes tedious to manage (and that isn't much given how verbose golang is).
- andreygrehov 6y agoInteresting. I currently maintain 2 repositories, 20k and 60k LOC, and I consider both applications fairly small as it's super easy to cruise its modules/files. In my books, a large system is probably around 500k LOC and I'm curious how that'd work with Go. It's all about project structure and organization of packages. A single file with 3k LOC is most likely unmanageable. 20 files with 150 LOC each is also not much helpful. However, 5 files with 600 LOC each is a much more balanced split. You are not getting lost in files and each file is of reasonable size. Obviously, it's not always possible and highly depends on application specifics, but I'm just trying to make a point. One should always strive for a project structure that doesn't require too much of a brain juice to comprehend the structure. Otherwise it's possible to get lost in 2-3k LOC regardless of a programming language.
- apta 6y agoI work on a program that is 100+ KLOC. It's quite atrocious honestly. I can't help but think that if it were written in Java, it would have been much shorter, probably at least 50% if not even more.
- philosopher1234 6y agoCan you go into more detail about how java would make the program shorter?
- apta 6y agoSome examples: * Exceptions for error handling * Streams (map/filter/etc.) * Generics * Superior collections library (Set, ConcurrentHashMap, LinkedHashMap, etc.) * Records * Enums * Upcoming feature: pattern matching
- philosopher1234 6y agoThis is not really what I had in mind. I know java has these features, but how would they make your program shorter? Could you be more specific? Maybe share an example of a real problem that is made easier by these features? Often people will give made up examples, but the trouble with made up examples is you can't tell if they actually matter in practice or not. Sure, if we invented a way to turn popcorn into gum, itd be easier to turn popcorn into gum, but no one wants to do that in reality. I'm after real examples.
- pydry 6y agoMy tolerance would be about the 2000 line level. That isn't the point where I'd consider it non maintainable or anything, it's just the point where I'd probably consider the trade offs less worthwhile.
- dantillberg 6y agoWhen writing quick-and-dirty Go programs, I define a function called "bailif" (pun intended) that turns non-nil errors into panics. i.e. `func bailif() { if err != nil { panic(err) } }`. Each error can be "handled" at first by just `bailif(err)`.
- euroderf 6y agoThis works best in main(), while library functions can just return errors upwards to it.
- akmittal 6y agoI did not like it at first but now I kind of got used to it. It makes errors very explicit. Other language alternative is wrapping in try catch block. Wrapping everything statement which can produce error is more tedious.
- ColonelPhantom 6y agoThere are other alternatives as well. For example, Rust uses a Result tagged union. You can also shorthand it using the ? operator, which does the same as the if err != nil { return nil, err; } but without all the boilerplate. There are some inconveniences, like needing to specify the error type by hand, but overall the amount of code is far smaller, it is a lot harder to mess up return types, yet it remains equally explicit in forcing you to handle every error, which is something that Go doesn't do.