News for hackers. Stuff that matters.
Re:Porting 4.5K lines of C to Go by tigershark on Friday August 04, 2017 @06:58PM (#14933060)
This is what really struck me: "The API is still a bit awkward by Go standards. Too many methods, not enough direct access to variables" Does he really mean that proper go code has direct access to fields rather than encapsulating them???
Re:Porting 4.5K lines of C to Go by kjksf on Friday August 04, 2017 @07:13PM (#14933140)
I would phrase it like that: most languages (C++/Java/.NET) have unhealthy obsession with creating unnecessary setters/getters, paying for it with increased compile time, code bloat, slower code.

In Java/C# land refactoring tools even give one-click access to "create me some setters/getters".

This is not to say that abstracting access with functions has no place, but if all they do is get/set the value, they are unnecessary.

Re:Porting 4.5K lines of C to Go by surye on Friday August 04, 2017 @08:16PM (#14933444)
C# and Python (for examples) don't use getters and setters, they use properties which provide the same interface as direct access variables when the need arises for more complexity, client code doesn't need to know about it. Languages without this feature needs to make everything getter/setter if they don't want to break the API when something needs additional logic.
Re:Porting 4.5K lines of C to Go by TazeTSchnitzel on Saturday August 05, 2017 @08:55AM (#14935467)
And indeed Go code has its fair share of (otherwise) unnecessary getters because of this.
Re:Porting 4.5K lines of C to Go by fancy_pantser on Friday August 04, 2017 @08:52PM (#14933608)
Python does have properties, which work via decorators @property (which decorates a getter) and @foo.setter.
Re:Porting 4.5K lines of C to Go by typon on Friday August 04, 2017 @09:04PM (#14933648)
He literally just said that
Re:Porting 4.5K lines of C to Go by vkou on Saturday August 05, 2017 @02:27AM (#14934479)
So, let's suppose you don't create getters or setters, because all that function does is get/set the value.

Six months later, you discover that you, in fact, do need to do something besides getting/setting the value.

If you created a getter/setter, you could just change its code. If you, instead, had raw access to the field, you now need to update every single instance of client code that called your getter/setter.

Most of the time, your compiler/VM can optimize the getter/setter call away, and a bunch of trivial get/set logic isn't exactly the worst source of cognitive overhead.

Re:Porting 4.5K lines of C to Go by kjksf on Saturday August 05, 2017 @03:46AM (#14934655)
There are infinite possible futures but only one now.

Write the best possible code needed today. Anything else is premature abstraction.

I do get the "what if" temptation, I just think it should be actively resisted.

Re:Porting 4.5K lines of C to Go by osteele on Saturday August 05, 2017 @04:19AM (#14934733)
See [YAGNI](http://wiki.c2.com/?YouArentGonnaNeedIt).

A helpful (to me) metaphor is that you're paying for an option. (In the finance sense, not the type system sense.) Sometimes it's worth buying an option, sometimes it's not, but don't think it's free.

Re:Porting 4.5K lines of C to Go by astrobe_ on Saturday August 05, 2017 @04:40AM (#14934801)
A more simple analogy: it's an insurance.

It's certainly wise to subscribe an insurance for your house, but for every single drinking glass you own...?

If it's a tea set you inherited from your great-grand-mother, when you break it the insurance won't un-break it. If your property suddenly needs to be more than a property, chances are that this change will break the LSP or trigger a larger redesign anyway.

Re:Porting 4.5K lines of C to Go by MrBuddyCasino on Saturday August 05, 2017 @04:51AM (#14934842)
This not one of those instances though. The cost of having getters/setters is essentially zero, the potential downside can be big.
Re:Porting 4.5K lines of C to Go by rakoo on Saturday August 05, 2017 @06:21AM (#14935074)
Ok, reality time: out of all the getters/setters you've ever written, how many of them have you ever needed to modify? And for those modifications, how long would it have been to refactor your code base to go from public field access to publi method access ?
Re:Porting 4.5K lines of C to Go by taspeotis on Friday August 04, 2017 @09:19PM (#14933715)
> paying for it with increased compile time, code bloat, slower code.

Auto-properties in C# are one line of code, same as fields, and the getter/setter methods should always be inlined which turn them into field accesses anyway. So I'm not sure how that would affect compile times or lead to slower code in anything but some pathological use case. Or DEBUG, but at that point all performance bets are off.

(Technically Microsoft can get worse performance on properties vs. fields when using NGen - that's why they have TargetedPatchingOptOutAttribute - but that doesn't apply to regular, non-Framework assemblies.)

As for code bloat `public int SomeValue { get; private set; } = 6;` encapsulates a publically read-only/privately mutable property with a default value quite concisely...

Re:Porting 4.5K lines of C to Go by zaroth on Friday August 04, 2017 @11:07PM (#14934017)
(smacks forehead)

Wait, wait, since when can you put the default assignment right there on the same line? Go ahead, say since always... I dare you.

Edit: Apparently this came in C# 6.0 (July, 2015). Ok, now I don't feel so bad. :-)

On a separate note, C# might not sexy or even that popular, but I really enjoy coding in it, and the continuing evolution of the syntax is a joy.

Re:Porting 4.5K lines of C to Go by shpongled on Saturday August 05, 2017 @04:03AM (#14934693)
It really is a joy to program in. I fought it off for the longest time (I like regular ole C, python, dabbled in Java long ago), but finally dipped my toes in and tried C# a month ago... While the language as a whole is a bit verbose, LINQ is incredible, and the breadth of the standard library is awesome.
Re:Porting 4.5K lines of C to Go by MichaelGG on Saturday August 05, 2017 @12:03AM (#14934157)
Instead of a field, you get a field and two methods. It's more IL, more metadata, etc. Maybe when the JIT is all done it's eliminated, but it'll cost more to JIT.

Though I doubt this makes a ton of real world impact, performance wise. Stripping names would do more to shrink the .Net code.

Re:Porting 4.5K lines of C to Go by taspeotis on Saturday August 05, 2017 @12:11AM (#14934178)
> Instead of a field, you get a field and two methods

And if you wanted a publically read-only/privately mutable value without properties you'd have to write ... a field and one method. Possibly two methods if you wanted some shared validation code everywhere you assign the field.

Like I said, in practice, it's the same. If you have some case where three vs. two members makes a difference you're in pathological case territory.

Re:Porting 4.5K lines of C to Go by DelightOne on Saturday August 05, 2017 @05:33AM (#14934950)
Souns like get/set in swift for me. Does it have computed properties too?
Re:Porting 4.5K lines of C to Go by taspeotis on Saturday August 05, 2017 @06:17AM (#14935060)

    public double Area
    {
        get { return Width * Height; }

        set
        {
            Width = Math.Sqrt(value);
            Height = Width;
        }
    }
You can also use expression bodied members (that's the => syntax) with get/set. There's a shorthand if you only need to define the getter (i.e. read-only):

    public double Area => Width * Height;
Re:Porting 4.5K lines of C to Go by DelightOne on Saturday August 05, 2017 @08:28AM (#14935389)
Swift has the same syntax too except the last line.

Have they introducted willSet and didSet too?

Neat stuff, i may try the language out in a future project on windows.

Re:Porting 4.5K lines of C to Go by taspeotis on Saturday August 05, 2017 @09:26AM (#14935554)
There's no direct equivalent to willSet or didSet. The closest equivalent (aside from adding code before/after your own get/set code) might be INotifyPropertyChanged [1]/INotifyPropertyChanging [2]? But they are more for external observers and add a lot of boilerplate to your code. It's possible to manage the boilerplate, however [3].

[1] https://msdn.microsoft.com/en-us/library/system.componentmod...

[2] https://msdn.microsoft.com/en-us/library/system.componentmod...

[3] https://github.com/Fody/PropertyChanged

Re:Porting 4.5K lines of C to Go by Scaevolus on Friday August 04, 2017 @07:01PM (#14933074)
Yes. If you have a struct, getters/setters are somewhat pointless, since member visibility control is only package level (protected/public depending on variable name capitalization).

For interfaces, you can only invoke methods, so getters/setters are required.

Re:Porting 4.5K lines of C to Go by vvanders on Friday August 04, 2017 @07:38PM (#14933265)
Yup, getters/setters in POJOs drive me up the wall. So much simpler in languages that have proper const/mutability qualifiers.
Re:Porting 4.5K lines of C to Go by krylon on Friday August 04, 2017 @08:15PM (#14933438)
> proper const/mutability qualifiers

I really like Go, but on the const/mutability front, it does no better than C, unfortunately.

Re:Porting 4.5K lines of C to Go by fleshweasel on Friday August 04, 2017 @08:10PM (#14933418)
so..not Go :P
Re:Porting 4.5K lines of C to Go by vvanders on Friday August 04, 2017 @09:28PM (#14933742)
Oh yeah, definitely not :)
Re:Porting 4.5K lines of C to Go by valarauca1 on Friday August 04, 2017 @07:49PM (#14933319)
Encapsulation isn't _that_ useful.

In Go it has real runtime cost as unlike C/C++ (you have an optimizing compiler) [1] , and in Java/C# you have the JIT which will optimize out your method calls over enough time.

A lot of FP languages are perfectly fine building Abstractions without getters/setters. But in OOP land people believe this is a requirement. 2/4 OOP _principles_ encapsulation and inheritance have aged like dirty sweat socks in a hot gym locker.

[1] https://klaig.blogspot.be/2012/09/the-go-language-is-faster-...

Re:Porting 4.5K lines of C to Go by burntsushi on Friday August 04, 2017 @08:18PM (#14933453)
Take a look at the `time.Time` struct. It has a couple fields. What are the trade offs of making those fields public? (I contend that encapsulation is supremely useful, and the time package is a good exemplar.)

Note that Go's compiler does do function inlining, so it might be possible to encapsulate your data without runtime costs. With that said, you certainly don't have the flexibility that you have in C/C++/Rust.

Of course I agree that blindly adding getters/setters everywhere is bad juju. But to say encapsulation isn't useful is a step too far. Instead of thinking in terms of getters/setters, I'd rather think in terms of invariants on your data. If you have invariants you want to protect, then encapsulation is one possible tool you can use to protect them.

Re:Porting 4.5K lines of C to Go by kartickv on Saturday August 05, 2017 @01:30AM (#14934368)
He didn't say encapsulation is not useful. He said it's not that useful. In other words, blindly encapsulating everything because that's supposed to be the right way is overkill.

Unless you're building an API.

Re:Porting 4.5K lines of C to Go by burntsushi on Saturday August 05, 2017 @10:39AM (#14935928)
> He didn't say encapsulation is not useful. He said it's not that useful.

So look at my comment as a refinement on that idea?

Re:Porting 4.5K lines of C to Go by nine_k on Friday August 04, 2017 @08:25PM (#14933496)
This is because most FP languages embrace immutability, so setters are absent by design, and getters are not needed. Any computation goes to proper functions, not "getters". Absence of virtual methods also helps: there is no point in having a getter (or a setter) if nobody would override it.
Re:Porting 4.5K lines of C to Go by coldtea on Friday August 04, 2017 @07:29PM (#14933231)
>Does he really mean that proper go code has direct access to fields rather than encapsulating them???

Yeah, so?