Skip to main content

Intuitive Programming - Fragile Constructors

There is nothing that says that a constructor of a class can’t perform complex transactions, but most of the people do not expect them to be long running or fragile.  When someone is writing a constructor that connects to a database, makes a network call, parses files on disk or something that can error out, that is not intuitive.  In the words of Big Lebowski “you are not wrong, you are just an a-hole”



My overall mantra of intuitive programming and design is that you should create systems with the principle of least surprise to the caller and maintainer.  Most of the programmers assume that object constructors rarely fail and rarely take a long time.

Constructors are generally used to create object and assign default or provided values to the fields in the object.  The trouble with overloading the constructor with something super heavy is that while you can remember that this is a “special constructor” others won’t intuitively get that.  Often times when you do something strange you yourself don’t remember what you did.

What kind of bad things happen when you put fragile and long running operations into a constructor?  Someone (because it’s never you) can put your object in system initialization or a static module variable and then all of a sudden without a network resource or a database entry your system won’t start.  Your alarm triggers are going off, you want to get to the management interface or pull up a diagnostic page and it just doesn’t start - it’s a bad situation for engineers.

What’s a better way of dealing with complex initialization? Let’s say you need some data from the database, a network call or something else that can be fragile?  The easiest and most intuitive way is to just have a function that produces your object as a result.

def gimme(….) -> ComplexClass

When people call a function they have much fewer assumptions about what is going to happen. They generally understand that they might get an exception, they might get nothing in return, it might take a while or there might be a slew of things that happens within that function.  With functions people are more likely to read the docs or the source code.

Another pattern is to create an initialization method so you can create an object and then initialize it in a separate call.

While all of the above methods are acceptable, some of them are more intuitive than others.  Intuitive software design guards the consumers of your code from making making mistakes and that is good karma.


References:
https://blog.submain.com/c-constructor-usage-examples-best-practices/
https://stackoverflow.com/questions/938426/bad-practice-to-run-code-in-constructor-thats-likely-to-fail
http://www.parashift.com/c++-faq-lite/init-lists.html
https://www.amazon.com/dp/0321356683 

Comments

Popular posts from this blog

SDET / QA Engineer Interview Checklist

After interviewing and hiring hundreds of engineers over the past 12+  years I have come up with a few checklists.  I wanted to share one of those with you so you could conduct comprehensive interviews of QA Engineers for your team.

I use this checklist when I review incoming resumes and during the interview.  It keeps me from missing areas that ensure a good team and technology fit.  I hope you make good use of them.  If you think there are good questions or topics that I have missed - get in touch with me!


SDE/T or QA Engineer interview checklist from Mike Borozdin
If you like this checklist you might want to check out these posts:
Emotional Intelligence in Software Teams  and Good-bye manual tester, hello crowdsourcing!

Code versus Configuration

At Ethos we are building a distributed mortgage origination system and in mortgage there is a lot of
different user types with processes that vary depending on geography.  One of our ongoing discussions is about how much of the logic resides in code vs. being in a workflow system or configuration.  After researching this topic for a bit, I have arrived at a conclusion that the logic should live outside of code very infrequently, which might come as a surprise to a lot of enterprise software engineers.

Costs of configuration files and workflow engines First thing that I assume is true is that having any logic outside of the code has costs associated with it.  Debugging highly configurable system involves not only getting the appropriate branch from source control, you also need to make sure that the right configuration values or the database.  In most cases this is harder for programmers to deal with.  In many FinTech companies where the production data is not made readily accessible…

Should this be a microservice?

After having developed several distributed systems and been a part of dozens of architectural discussions I decided to put together a way to frame the microservices debate. Microservices have been fashionable for some time. A lot of it stemmed from the fact that big and successful cloud companies are using microservices.  It seems reasonable that to create a “serious system” one must be using serious tools and architecture, today it’s microservices.  No engineer wants to be called out for creating a solution that “doesn’t scale.”

The definition for a microservice varies, but overall it tends to be a piece of your system that can run somewhat independently (unless of course it depends on other microservices) and has a REST or queue processing interface.  Overall code encapsulation and separation of concerns have all been around for a long period of time.  Current evolution with containers, fast networks and REST API allows people to easily integrate pieces of their system using web se…