So if we consider a struct that only has data, like MyStruct above, in which cases would it benefit from having a constructor? The advantage of NOT writing a constructor So technically, you can replace struct with class in all that follows, but this does not follow the convention of struct and class (which everyone should follow). In practice, the convention is that we use structs only to bundle data together, and a struct generally doesn’t have an interface with methods and everything. Technically, a struct is like a class, so technically a struct would naturally benefit from having constructors and methods, like a class does.īut this is only “technically” speaking. Would a struct benefit from having a constructor? Or are constructors not in the “spirit” of struct? Or would constructors even get in the way?Īll those questions can be answered by Yes or by No, depending on what a given struct represents.īefore delving into the “why”, the “when”, the “how” and even the “what else”, let me be more specific about what I mean by a struct. Learn more.Ĭ++ structs are little bundles that pack a few pieces of data together: struct MyStruct Curated by Pek and delivered every day, it is designed to be your morning reading list. It all came down like this.Ī Config struct was initialized with field:value syntax, and I added a new attribute without a possible "sensible default.I originally wrote this article for Morning Cup of Coding. Morning Cup of Coding is a newsletter for software engineers to be up to date with and learn something new from all fields of programming. Last month I created a bug due to a missing constructor. Public Structs and Libraries are even more sensitive to changes. Reason 3 - Exported Structs and Public Libraries Point being? It would be best if you optimized for flexibility and refactoring as a team in your project. I don't know about you, but the latest commit in my project was like 3h ago, lol. The last commit to Mutex source was in November 2019. Reason 2 - Your code changes weekly, RWMutex doesn't A wrong default value was supplied as the Go compiler can't pick it up given from a compiler perspective, the value is not missing it thinks you didn't pass it because you wanted the compiler to use a default value. However, I can link to 3 PRs in the last 6 months that caused a production bug because of a missing dedicated constructor. I have been writing Go for 4 years, and I can't pinpoint a single struct I designed using default values solely. ReaderWait int32 // number of departing readers ReaderCount int32 // number of pending readers ReaderSem uint32 // semaphore for readers to wait for completing writers WriterSem uint32 // semaphore for writers to wait for completing readers W Mutex // held if there are pending writers To be objective, one struct has default values that work like a charm, and I frequently use: sync.RWMutex. I needed to change only one place.Įvery struct deserves its constructor! Your future self will thank you during the next refactoring session :) I encapsulated this responsibility into a single function. What's your experience? Agree or do you think differently?įortunately, I can avoid all that mess, and a large PR for my teammates, because I have control over the struct creation. Generally, I don't see any value in the field:value constructors given any good IDE will graphically show you the names like my previous screenshot. Don't even try to fix it with an optional setter. MaxGasPrice will accidentally be 0 by default, turning the struct into an invalid state. However, if I would label the constructor elements "explicitly as field:value pairs, the initializers can appear in any order, with the missing ones left as their respective zero values", the refactoring could quickly end up with a production bug, as the compiler wouldn't point out all the places I forgot to pass a new value so that's even worse approach. That's a very realistic estimate as I have already been in several such situations on various projects. If I had to add another attribute to the Tx struct, MaxGasPrice, I would have to modify tens/hundreds of files (depending on the codebase size/implementation). Let's say I don't create a dedicated constructor NewBaseTx(). Reason 1 - Sensible default values are rare No constructor Let me walk you through my four reasons for doing this. Func NewBaseTx(from, to common.Address, value uint, nonce uint, data string) Tx
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |