OOP in Golang vs C++
Before I started to learn Go, every online opinion I would read about the language complained about the lack of generics and how OOP was dumbed down and so on.
It made me put off learning it for quite some time, more than I would like to admit. Coming from a C++ background, OOP, generics and meta-programming was my daily bread.
It wasn't until I had to actually learn Go that I saw what it offered me in terms of OOP and it was just enough. As such, I wanted to put a side-by-side comparison of typical C++ code that deals with classes, and it's corresponding implementation in Go that does more or less the same thing.
This is by no means an exhaustive list of examples, but I thought it might prove useful for someone trying to figure out Go.
To run all Go examples, copy them to a file and run go run filename.go .
To run all C++ examples, copy them to a file and run g++ -o filename filename.cpp -std=c++14 && ./filename .
Class declaration
In C++:
#include <iostream> #include <memory> #include <string> class MyClass { private: std::string property1; void Method1(std::string param1, int param2); public: std::string property2; MyClass(std::string constructor_argument); int Method2(int param); }; MyClass::MyClass(std::string constructor_argument) { this->property1 = constructor_argument; } void MyClass::Method1(std::string param1, int param2) { std::cout << param1 << std::endl << param2 << std::endl; std::cout << this->property2 << std::endl; } int MyClass::Method2(int param) { this->Method1(this->property1, param); return param + 1; } int main(int argc, char *argv[]) { auto obj = std::make_unique<MyClass>("property 1 value"); obj->property2 = "property 2 value"; std::cout << obj->Method2(4) << std::endl; return 0; }
Go equivalent:
package main import "fmt" type MyClass struct { // properties that start with a lowercase character are private property1 string // properties that start with an uppercase character are public Property2 string /* Keep in mind that public and private in Golang actually means exported by package. Code from the same package can access a structure's private properties and methods. */ } func NewMyClass(constructor_argument string) *MyClass { return &MyClass{property1: constructor_argument} } func (mc *MyClass) method1(param1 string, param2 int) { fmt.Printf("%s\n%d\n", param1, param2) fmt.Printf("%s\n", mc.property1) } func (mc *MyClass) Method2(param int) int { mc.method1(mc.property1, param) return param + 1 } func main() { obj := NewMyClass("property 1 value") obj.Property2 = "property 2 value" fmt.Printf("%d\n", obj.Method2(4)) // No return needed }
Inheritance (sort of)
In C++:
#include <iostream> #include <memory> #include <string> class BaseClass { public: std::string property1; void method1(); }; void BaseClass::method1() { std::cout << this->property1 << std::endl; } class DerivedClass : public BaseClass { public: std::string property2; void method2(); }; void DerivedClass::method2() { std::cout << this->property2 << std::endl; } int main(int argc, char *argv[]) { auto obj = std::make_unique<DerivedClass>(); obj->property1 = "property 1 value"; obj->property2 = "property 2 value"; obj->method1(); obj->method2(); return 0; }
Go equivalent:
package main import "fmt" type BaseClass struct { Property1 string // no need for method declaration here } func (bc *BaseClass) Method1() { fmt.Printf("%s\n", bc.Property1) } type DerivedClass struct { BaseClass // this is actually composition Property2 string } func (dc *DerivedClass) Method2() { fmt.Printf("%s\n", dc.Property2) } func main() { obj := &DerivedClass{} obj.Property1 = "property 1 value" obj.Property2 = "property 2 value" obj.Method1() obj.Method2() // no need to return }
Interfaces
In C++:
#include <iostream> #include <memory> #include <string> class MyInterface { public: virtual void method() = 0; }; class Class1 : public MyInterface { public: void method() override; }; void Class1::method() { std::cout << "Class 1" << std::endl; } class Class2 : public MyInterface { public: void method() override; }; void Class2::method() { std::cout << "Class 2" << std::endl; } std::shared_ptr<MyInterface> NewClass1() { return std::make_shared<Class1>(); } std::shared_ptr<MyInterface> NewClass2() { return std::make_shared<Class2>(); } int main(int argc, char *argv[]) { auto obj1 = NewClass1(); auto obj2 = NewClass2(); obj1->method(); obj2->method(); return 0; }
Go equivalent:
package main import "fmt" type MyInterface interface { Method() } type Class1 struct { } func (c1 *Class1) Method() { fmt.Println("Class 1") } type Class2 struct { } func (c2 *Class2) Method() { fmt.Println("Class 2") } func NewClass1() MyInterface { return &Class1{} } func NewClass2() MyInterface { return &Class2{} } func main() { obj1 := NewClass1() obj2 := NewClass2() obj1.Method() obj2.Method() }
Conclusion
There are basic equivalences between traditional OOP languages like C++ and the syntax and functionality that Golang provides.
In it's own simple way, Golang provides ways to implement encapsulation, inheritance and polymorphism. In my humble opinion, these mechanisms are enough for most object-oriented projects.
8 Tips On How To Handle Angry Customers Without Losing Your Cool
Hi there, reader! :) This article presents some tips on how you can handle those customers who are a little more difficult to handle, based on my own experience.
Whether you're planning to chat or write emails, below are my top suggestions on how to create the best interaction with your customers, even if they are not so nice.
1. Don't take it personally
One of the most difficult tasks in maintaining your calm when dealing with angry customers is to not take their frustration personally. You shouldn't do that as they know that you did not cause the problem. Listening to what they have to say without interrupting and then finding a way to help them out will cool down the situation.
Also, as a professional customer service specialist, you shouldn't argue back. If they become abusive, let them know that you understand their situation and you are there to help.
2. Empathize
This would be the most important rule which I often associate with the law of attraction - if you act with patience and empathize, you'll receive gratitude even though the problem is not solved.
Some customers keep coming back. Asking questions as "How are you today?" or "How is it going?" will help in creating that special bond between you and your customer which will lead you to know them as people and not just simple buyers.
3. Be patient
Patience is a virtue especially when dealing with angry customers. Some of them may get angrier as the live chat continues, but if you stay in control and redirect the conversation to a happy resolution, things may move in the right direction. Each customer is different and being patient with each of them will improve the quality of conversation.
4. Inform the customer when he needs to wait while you're investigating
Often times and usually when I chat with customers, the investigation requires time and they have to wait. Phrases such as "I appreciate your patience while I'm investigating your case." or " Just a moment please while I am looking for more details" will make it clear to the customer when he needs to wait and also avoid the repeated "Are you there?". Asking questions regarding their problem will also help you take back control and extract the information you need to get to the bottom of their situation.
Also, if I'm caught in multiple investigations, I usually tell them that I'm helping other customers as well and my reply can be delayed. However, always assure them that they will receive feedback from your side.
5. Knowing when and how to apologize
I know that it is difficult being truly sincere when you are trying to keep your cool, but an apology at the right moment can calm down the customer. Depending on situation, there are a few different ways you can apologize to your customers such as:
"I'm sorry you are so frustrated. I understand your situation and I will do my best to help you"
"I am really sorry that I misunderstood your problem."
"I apologize for the inconvenience this may cause you and your team."
6. Solve the problem
If you applied the rules from above, your customer should be finally relaxed. This means that you can work with him in order to find a resolution that satisfies both sides.
7. End the conversation with a happy note
It is very important to end a chat session on a high note. Instead of giving an "abrupt goodbye", you can ask your customer if there's anything else you can do for them and reminding that you are right there for them if they require any assistance. Also, wishing them a wonderful day and a sunny weather should give the "goodbye" a "talk to you soon" meaning, which is great.
8. Relax, take it easy
Let the anger go away with your customer. Always remember that a focused mind requires a relaxed state, so don't forget to take a break when you cannot concentrate. Take a walk to the kitchen, drink a cup of tea while enjoying the view, play a game with your colleague or listen to Mika’s song (Relax, take it easy) as it will help you to relieve the stress. Also, it is scientifically proven that taking a deep breath multiple times will help you concentrate even more.
I believe that these tips aren't only for customer support. You can practice them every day of your life, as communication is the key to good relations between the people. Knowing how to communicate will allow building connections and a great success.
...and at the end, no matter what, don't forget to smile! :)
Key to negative feedback: be constructive
Feedback is one of the words I use and hear a lot. It looks like everybody knows what it means, how important it is. Then, why is it so hard sometimes to give it and accept it? I don't know if you like by the book definitions, but I'll give you one:
In an organizational context, feedback is the information sent to an entity (individual or a group) about its prior behavior so that the entity may adjust its current and future behavior to achieve the desired result.
(a definition from www.businessdictionary.com)
Key to negative feedback: be constructive
There are two types of feedback, positive and negative, both valuable and effective. We have a constant need of knowing where we stand, how do we do our job and how can we maximize our potential.
While it is more convenient to give and receive positive feedback, we might avoid giving the negative one. But this is one of the most important tools to develop employees.
No matter if we talk about positive or negative feedback, it has to be constructive so that it brings an improvement to the receiver's behavior. Feedback should be:
- specific
- realistic
- well intended
- timely
- shouldn't be judgmental
When it is expressed as I mentioned above, it will motivate and should lead to performance improvement.
If what's communicated sounds as criticism, it's too general and doesn't refer to a specific situation or example, it will demotivate us. It is much easier or at hand to 'attack' with words than it is to deliver the message in a diplomatic, non-aggressive manner.
What's the context where a employee can give or receive feedback? We encourage our colleagues to have One on One Meetings with their managers.
1:1s are a chance to give and receive regular feedback, to develop a stronger connection between the two and a way to have in control the problems which may occur in a working environment. We see them as meetings that should be scheduled by the employee, but a manager can do it do. It should happen once a month (still working on improving their frequency in our company). All kind of subjects can be on the agenda: performance, career development, other professional or personal issues.
Of course feedback doesn't happen only in a formal meeting, it is a daily thing even though we might not be aware of it.
I'll leave now you with an intriguing affirmation I read. Feedback's role is to make employees do the job better, it is not to make them feel better. Do you agree with this statement?
Automatically Restart Your Node.js Application
Many of us write code on Node.js, click [CTRL+S] to save the file but forget to restart the application.
If we don't forget to restart the application we will go in console and hit [CTRL+C] to stop the application and then restart it by pressing the [UP] arrow and [Enter].
Nodemon
You can automate this repetitive task and make your development process easier by using `nodemon`.
`nodemon` will watch the files and if any files were changed, the `nodemon` will automatically restart your node application.
First, you need to install `nodemon`:
npm install nodemon -g
Now, you should swap the `node` command for the `nodemon` command:
$ nodemon app.js [nodemon] 1.11.0 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node app.js` [nodemon] restarting due to changes... [nodemon] starting `node app.js`
Read about: How to set up a Node.Js development environment
If you need to manually restart your application, instead of stopping and restart `nodemon`, you can simply type `rs` on your console.
[nodemon] starting `node app.js` rs [nodemon] starting `node app.js`
Happy Coding!
How to set up a Node.js development environment
Context:
Here at IntelligentBee we wanted to establish a few ground rules that all of our developers should follow when developing an app in Node.js. In that way all of us would be familiar with a particular set of tools, something that would make us deliver better software, fast. Here is how we set up a Node.js development environment for a particular project:
- Installing NPM and Node.js
To get things started you need to install npm, and Node.js. You can easily do that by downloading them from here. - Create a project folder, and navigate to it using your favorite terminal. Then create a package.json file in that folder, a file that will save all the libraries you use. Using the terminal, run the command $ npm init . Write a few details about your project, and that’s it.
- The web framework
This should be project based, but we recommend Express.js as it the most mature and widely used framework for Node.js. From the Terminal, navigate to the folder where you will build your project, and run $ npm install express --save - The testing framework
To deliver robust code, you need to write tests. Node.js has a lot of testing frameworks available, but Mocha.js is by far the most used and proven test framework out there. To install, run $ npm install --save-dev mocha .Here we also need to mention the assertion library that can make life even easier. We recommend Chai.js, as it exposes all interfaces of asset, should and expect. Just run $ npm install --save-dev chai in your terminal to install. - The IDE
We’ve chosen Visual Studio Code over the more widely preferred Sublime Text, as it has a native support for Node.js debugging. Also, it is pretty easy for newer, less experienced developers to adopt it. You can get this free piece of software from here. - The linter
We decided to go with ESLint, as it is the most popular linter for Javascript and has a plugin for linting Node.js code. It is recommended that you install ESLint in the project folder that you work in, but you could also install it globally. To install it for your project, run $ npm install eslint --save-dev in your terminal. To make it aware of Node.js and Mocha you need to install these Eslint plugins as well:
- Eslint-node: $ npm install --save-dev eslint-plugin-node
- Eslint-mocha: $ npm install --save-dev eslint-plugin-mocha
The coding standards:
The Airbnb coding standard is one of the most popular out there, for JavaScript and Node.js. That would help you write clean code, that will be easier to understand later on, or by other people. In your terminal, run $ ./node_modules/.bin/eslint --init and choose Airbnb as your coding standard. After you do that, you need to open the newly .eslintrc.json file that was created, and add "mocha" and "node" in the file. Basically that file needs to look like this, if you saved the file using JSON format:
{ "extends": "airbnb-base", "plugins": { "import", "mocha", "node" } }
- Install the ESLint plugin in VS Code
Go into the VS Code app, and navigate to its Extensions page. Search after "eslint", and install the first result from the list. Restart VS Code.
Now you should be all set up and start coding. Enjoy!
How is your Monday?
We all know the Monday jokes. And I bet we all complained at least once about the fact that the weekend is over and a new work week is about to start. Well, I’ll admit, it happened to me more than once. And every time I asked myself what is it that I don’t really like. Is it the fact I have to get up early? Is it the fact I don’t like working? I don’t think I ever felt sorry that Monday comes during vacation time. Or could that mean that I don’t like my job, the company and the team I work with?
The Monday Blues
Maybe you heard about the ‘Monday Blues’ defined by Alexander Kjerulf, an expert on happiness at work:
“the negative emotions that many people get at the beginning of the workweek if they're not happy at work”
I gave this definition a thought.
I started by making a list with what’s important for me in order to be happy at work. It looks like this:
- Having ownership, ‘my job is mine’. Researches talk now about a new kind of ownership, the psychological ownership. This makes an employee feel that the company he works for is his/hers.
- Having autonomy, being able to take decisions.
- Being able to express ideas, concerns, opinions that contradict what others say.
- Mistakes should be permitted; and haven’t I done like a million of them. Making mistakes is a sign that you had the courage to try something. They are a way of learning and experiencing new things.
- Flexibility regarding my working schedule and the way I organize my tasks, activity.
- The team I’m part of. I need to say that my colleagues offered me so much support, professionally and personally. I have all the reasons to be grateful and happy to work and spend time with them.
Do I have all these above at my current job? I’m lucky enough to respond with a firm yes, I do. Then what could be the problem when I’m not happy to hear the alarm clock especially in the first morning after Sunday? I’m still looking for this answer :).
My Mondays are great
Until I find it, I’ll keep in mind what someone told me once:
“Monday is the perfect day to correct the last week’s mistakes”
This is a great perspective and opportunity to bring a smile on our faces when we feel a little bit down on a Monday morning or on any other working day.
Happy Monday everybody!
How to Save a File in Vim After Forgetting to Use Sudo
Many of you probably experienced this. You edit a file in Linux with Vim or Nano or whatever else text editor you might be using and when you try to save, you realise you forgot to launch the editor with sudo privileges, and file can't be written. Exiting without saving and editing again with sudo privileges is not always an option, especially if you lose a lot of work.
Solutions to save a file in vim
There are some workarounds. You can open a new terminal and grant the right permissions on the file with:
sudo chmod 666 <filename>
Now you can go back to your editor and saving will be possible. Don't forget to change back the right permissions for your file.
Also, you could save in a new file and then copy the newly created file over the old one.
But these are workarounds. Vim editor actually offers a solution.
In normal(command line) mode of Vim you can issue:
:w !sudo tee %
And that works like magic. For the geeks, here is how the "magic" works:
- :w - writes the contents of the buffer
- !sudo - pipes it to the stdin of sudo
- tee % - sudo executes tee that writes to "%" file
- % - Vim expands "%" to current file name
So Vim actually makes magic happen.
Emulate a Redis Failover with Docker
Reading the Redis documentation can be a bit confusing without the hands-on experience. You could in theory create multiple processes of the Redis Server on your machine and configure each of them in part, but what if you could do it in a few commands? Not only that but emulate the network they’re connected to as well.
I’ve been looking into this and there’s a few examples out there on Web, the best one I could find was this one: https://github.com/AliyunContainerService/redis-cluster
So, starting from that example, I’ve tried to do the next best thing, which is to create a single docker-compose.yml file for all of it. Removing the need to build each image, just to do a docker-compose up and scale as needed.
Here’s what I got:
master: image: redis slave: image: redis command: redis-server --slaveof master 6379 links: - master sentinel: image: redis command: > bash -c "echo 'port 26379' > sentinel.conf && echo 'dir /tmp' >> sentinel.conf && echo 'sentinel monitor master master 6379 2' >> sentinel.conf && echo 'sentinel down-after-milliseconds master 5000' >> sentinel.conf && echo 'sentinel parallel-syncs master 1' >> sentinel.conf && echo 'sentinel failover-timeout master 5000' >> sentinel.conf && cat sentinel.conf && redis-server sentinel.conf --sentinel" links: - master - slave
Basically, after saving this into a docker-compose.yml file and running docker-compose up in that folder you’ll get this:
You can now scale as needed. For example, by running:
docker-compose scale slave=2 sentinel=3
You’ll end up with:
To initiale a failover, you’ll need to take the master out of the picture, you can do that with:
docker-compose pause master
You can now observe the communication between the sentinels and slaves. After the down-after-milliseconds and failover timeout passes, one of the slaves will be selected for promotion.
After the sentinels agree on the selection, the slave will become the new master.
You can now unpause the old master by doing this:
docker-compose unpause master
The old master will now become a slave of the new master and perform a sync.
That’s about it. As an exercise you could try setting up a cluster starting from this and observe failovers there.
Why Should Your Online Business Offer API
There are several ways to extend a business model but API is a hot topic right now as the online world is expanding very fast. If you’re a developer or at least interacted with APIs before, you probably know why public APIs are so important, but there’s a big chance you didn’t hear or care about them before and now you’re wondering why everyone talks about them.
What is an API
In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application software. In general terms, it's a set of clearly defined methods of communication between various software components. (Wikipedia)
There is a simple way of saying this: an API is like a contract between two computer software agreeing to share information in a more or less standardised way.
Now it’s pretty clear what are we talking about, but why are them so important? How can APIs help us? In the following rows I will try to argument some good reasons.
Getting Started with Building APIs in Symfony2
Grow your business
You can grow your online business by integrating with additional tools or apps and engaging with others. This can be done using public APIs.
Let’s take Uber and Google Maps: everytime you search directions in Google Maps (from home to work, i.e.) you can automatically request an Uber, see your driver on the map or even contact him, all without having to leave Maps app thanks to Uber’s API.
Or if you have an online store, you might wanna offer public APIs so other apps can request price offers and display your products on their platforms.
Get ready for scaling
It’s all sweet and fun to start a new business and you probably want to do it faster and cost effective. Usually this means a monolithic application.
Success means scaling and this can be done by breaking the app into microservices. This will enable multiple options for you.
Let’s say you have a microservice that is being used very often and affects your server. That microservice can be moved on a new server with dedicated resources only for it and it will be accessible for the rest of the app via an API.
Or there is the case when you want to rewrite a single microservice (some languages are more efficient than others). This is the beauty of standardised API - you only have to make sure you accept the same API call as before and return the answer in the same format, so other dependent services won’t be affected.
Time saving
UX/UI is very important and we strongly advise you to continue to invest in that area, but there are cases when having to explore an UI for some actions is time consuming for some (more technical) users.
Let’s take SendGrid and their Marketing Campaigns app. You can create a new campaign by going through the UI process or you can simply make a call to their API. Second option is usually faster (but for more technical people or at least you need to develop an integration first) and the flow will always be the same, while UI can suffer some modifications over the time.
Mobile app
At some point you will probably want to add a dedicated mobile app to your business. Having APIs makes it possible. You are free to develop a new design or a new template without any changes on the API side.
Providing APIs must be a concern for every company, whether they focus on internal operations, partner integrations or public access. Join the revolution! Add API support to your product! Get a free quote now.
Better is possible and real
We all wish, hope and want for good things to happen as often as possible in our life. Even from the beginning, we are all trying to be good kids, then, as teenagers to get good grades, as young people to graduate a good college, as adults to have a good job, to build a good family and live a good life.
Good is enough, is normal...is perfect. But let me ask you something: isn’t it interesting the fact that we are so used with the presence of good things in our life and we take them for granted because they are ordinary, but when something extraordinary, unexpected happens, we are so amazed and exalted and overwhelmed and we start wondering: how is this possible? How can be something good, better? What is this better thing that just happened? Why didn’t this happen before? When will it happen again? And so on. We have definitely heard that better is real and we have seen it in others life, but we have never experienced it in our lives.
Well, this is where I stand up and say: BETTER is real! Something extraordinary, unexpected, something better really happened to me and I want to share my experience with you.
Better is possible and real
I’ve always dreamed about a good job. I’ve tried to study as much as I could to gain all the necessary information I needed to be sure that I will get the job. It wasn’t easy… but time flew fast and I have graduated the college. Frankly being, due to educational system and society, I wasn’t 100% sure that I will get the job I’ve dreamed about. Anyway, I hoped and I have started looking for the desired job. The reality was cruel, but, in the end, the job offer was signed. Yeeeeey! I felt so good! I have a good job! Now I am satisfied! Once I got the job, I did my best to be a good employee and to fit in this new and nice environment. But after a while, I have realized that this job was just a good job and nothing more. The company I’ve worked for and the teammates I’ve been working with were good, the view from my desk’s window was good, everything was just good. I’ve started feeling that good was no longer enough. Therefore, when the window of opportunity was opened, I quickly flew like a shy but courageous bee outside of the good zone. Once I've escaped from the good zone, I’ve heard rumors about the land of the better.
Have you ever felt the need of better? Have you ever realised that good is no longer good enough and you want something better, you need something better even if it is sometimes impossible to get it? Well, I felt the same. Also, let me tell you that the impossible is achievable. How? With support from people who are ready to believe in you and help you to grow professionally and gain priceless experience within their team. This is how it happened to me. I have found a better job and a better company for me, for my needs (yes, I’m aware of the fact that good and better are subjective beliefs, we have different perceptions about what they mean). I have met here the most amazing team I’ve ever had the chance to work with (if you don’t believe me, just come and find out for yourself). From the CEO to the newest colleague, everyone is BETTER. I am telling you, they've welcomed me with such love and kindness. A better job is defined, among other things, by a better team you work with.
And did I tell you about the view from my desk’s window? You and I will probably never find a better view than the one I enjoy everyday. From an unbeliever in better, I am now a strong believer and I will remain the same. Why? Because I have now a better job and I work with better people for a better company. I thought it is impossible, but now I have it and I love it and I know, feel and believe that it will last a very long time from now on.
I’m now part of a “beehive" filled with highly intelligent, strong, hardworking, funny and talented “bees” which are ready to provide the best “honey” in order to uniquely identify the company from the rest of the world through the highest quality of friendship, projects and delivered products.
Better is possible and real!