3 ms·
> Since Go doesn't even have inheritance or getters and setters Does Go have objects with functions? If so, it has getters and setters, Go programmers just ch
by Sohcahtoa82 7y ago
> Since Go doesn't even have inheritance or getters and setters
Does Go have objects with functions? If so, it has getters and setters, Go programmers just choose not to use them. I could absolutly write Python code like this:
class MyObject(object):
def set_foo(this, foo):
this.foo = foo
def get_foo(this):
return this.foo
The thing is...Java doesn't have getters and setters. There's nothing about the language that requires them. This is perfectly legal Java code:
class MyObject {
public int foo;
}
Look at it! It's a public member! You can read/write to it without a getter/setter! You know, something every language has, but for some reason, it's considered verboten in Java.
Java is a perfectly fine language. It's Java programmers that are awful for sticking to these unnecessary paradigms that necessitate ridiculous amounts of boilerplate.
- deleted 7y ago[deleted]
- throwaway894345 7y agoI agree with your point, but the snark here is unwarranted (pretty sure the parent isn't even contesting your point) and very likely to derail the otherwise interesting conversation (IMHO).
- Sohcahtoa82 7y agoRe-reading the other comment and my response, yeah, you're probably right. Java programmers are a source of frustration for me. I don't write Java professionally, but I do application security which involves reading a lot of Java code. And because the developers are sticking so strictly to the typical Java paradigms, the code is very difficult to read. Testing things locally is a nightmare, since stack traces are a ridiculous stacks of .invoke, as if Java programmers are afraid of directly calling functions. I let my frustration get the better of me and I apologize.
- hota_mazi 7y ago> for some reason, it's considered verboten in Java. It's not just "for some reason". There are plenty of reasons why getters are a superior approach to naked fields, and these have been documented to death for the past twenty years. If your field is not final, you should hide it behind a getter/setter.
- Sohcahtoa82 7y agoI've read a lot of documentation, and I've never been convinced. Maybe it's because I've just never been involved in writing a large Java project, and so never seen the advantages? Many other languages do just fine without them. What makes Java so different?
- ptx 7y agoOther languages (e.g. Python, C#, Kotlin, even old-school Visual Basic) have actual language support for getters/setters, usually called properties. What makes Java different is that they have properties as a concept (in the Java beans specification) but not as an actual language feature, so you have to implement them manually yourself for every field and every class. (Or let the IDE generate the code, but all that boilerplate is code that wouldn't need to exist in other languages.)
- thu2111 7y agoTwo things: 1. Many other languages let you redefine field assignment syntax to be a function call, which is basically what a 'property' is in Kotlin. Java doesn't. 2. The Java ecosystem distributes software in binary bytecode form and cares about binary/API compatibility. The combination of these two mean that even if you don't want to run any code on field assignment today, if there's any chance you might want it tomorrow you have to plan for it now by using functions instead of naked fields. Changing the latter to the former means updating all the use sites. The big win of language integrated properties is you can go from (in Kotlin): class Thing(val veryLongText: String) to class Thing(text: String) { val veryLongText by lazy { text.toLowerCase() } } without any of the places that use Thing.veryLongText noticing the difference. The first code just stores the string as an ordinary field. In the second, we've decided the text should actually be lower case and that lower-casing should be done on demand then memoized for efficiency. In Java, if you want to do that, you'd have needed to write a getVeryLongText() method from the start, as otherwise you'd have nowhere to add the extra code.
- ickyforce 7y agoThe funny thing is that if you want to create an immutable type in Java - no problem, just create a constructor and mark fields as final[1]. In Go - you can't do it like that. You need those getters, preferably an interface (because exported struct allows anybody to create a zero-value) and a factory function. [1] - assuming their types are also immutable
- throwaway894345 7y agoImmutability is neat, but you can get a long way by passing by copy and/or documenting whether a value shouldn't be mutated. I would like to see immutability added to Go, but it would break my heart if it was inspired by Java (i.e., final) instead of Rust's notion of immutability.