Ten principles of economics.

Share Button
  1. People face tradeoffs.
    There is no such thing as a free lunch. To get one thing that you like, you usually have to give up another thing that you like. Society also faces tradeoffs such as one between efficiency and equity. Efficiency means that society is getting the most it can from its scarce resources. Equity means that the benefits of those resources are distributed fairly among society’s members. Efficiency and equity are commonly contradictory. For example, if income tax is raised by policy, this policy have benefit of achieving greater equity, they have a cost in terms of reduced efficiency. As a result, people work less and produce fewer goods because the government redistributes income from the rich to the poor, it reduces the reward for working hard.
  2. The cost of something is what you give up to get it.
    The opportunity cost of an item is what you give up to get that item.
  3. Rational people think at the margin.
    Marginal changes means small incremental adjustments to a plan of action. For example, plan tickets are sold at $500 for a flight, but the airline are willing to sell a ticket to a standby passenger right before it takes off. Because the marginal cost of this is merely the cost of the bag of peanuts and can of soda  that the extra passenger will consume.
  4. People respond to incentives.
    Because people make decisions by comparing costs and benefits, their behavior may change when the costs or benefits change.
  5. Trade can make everyone better off.
    Trade between two countries is not like a sports contest, where one side wins and the other side loses. It can make each country better off. It is also true between families or other parties.
  6. Markets are usually a good way to organize economic activity.
    Market economy means an economy that allocates resource through the decentralized decisions of many firms and households as they interact in markets for good and services. It is opposite to centrally planned economics in most communist counties. The invisible hand of markets will guide its activity.
  7. Governments can sometimes improve market outcomes.
    There is a situation in which a market left on its own fails to allocate resources efficiently, which is called market failure. One possible cause of market failure is an externality. Another possible cause is market power, for example, monopoly.
  8. A country’s standard of living depends on its ability to produce goods and services.
    Citizens of high-income counties have more TV sets, more cars, better nutrition, better health care, and longer life expectancy that citizens of low-income countries.
  9. Prices rise when the government prints too much money.
    If the growth in the quantity of money is too fast, the value of money falls, and an inflation comes in.
  10. Society faces a short-run tradeoff between inflation and unemployment.
    Prices are said to be sticky in the short run, which mean if government wants to control inflation and reduces the quantity of money in the economy, in the short run, prices will not adjust accordingly. Instead, prices are slow to adjust. Imagine you get less money and prices don’t decline, what will you do? Stop buying things. Then firms make less money so they will lay off peoples. Until prices fall and people buy things as they use to be, then firms will again hire more people.


Principles of Macroeconomics

Effective C++ item 8: Prevent exceptions from leaving destructors.

Share Button

Throw an exception in destructor is dangerous and you should never let the exception leave destructors. If there are two exceptions propagating, the program will terminate or yield undefined behavior. Consider the example:

The output will be like:

Notice that two exception will propagate in the code so that it terminates.

So what should we do if we have to throw an exception in a destructor?

Consider we have a class to handle database connection and has a close method to close the connection. It will throw an exception in close method if close fails.

To ensure that clients don’t forget to call close on DBConnection, we have an resource-managing class to handle that:

But here we allow exception propagate out destructor which is dangerous as we discussed previously. so what’s the solution? We can catch the exception throw in destructor in the same destructor so that it will not propagate out that destructor. But that leads to either terminate the program my ourselves or swallow that exception. That’s not good. What we should do is to provide a close method for clients so that they can handle the exception themselves. But in destructor, we still swallow the exception if clients don’t call close method themselves.

“Effective C++” Third Edition by Scott Meyers.

Effective C++ item 7: Declare destructors virtual in polymorphic base classes.

Share Button
  1. Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should have a virtual destructor.
  2. Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.

For the first point, if the base class doesn’t have a virtual destructor, there are great potential that you will have memory leak in your program. Here is an example.

Consider here get() method returns a pointer to a dynamically allocated object, and if we delete Base class pointer with a non-virtual destructor, results are undefined. What typically happens at runtime is that the derived part of the object is never destroyed. This is an excellent way to leak resources, corrupt data structures, and spend a lot of time with a debugger. So any class with virtual functions should almost certainly have a virtual destructor.

Sometimes, you have class that you’d like to be abstract, but you don’t have any pure virtual functions. What to do? The solution is simple: declare a pure virtual destructor in the class you want to be abstract. But you must provide a definition for the pure virtual destructor! Because the way destructors work is that the most derived class’s destructor is called first, then the destructor of each base class is called. The compilers will generate a call to the destructor of a base class from its derived classes’ destructors, so you have to be sure to provide a body for the function. If you don’t, the linker will complain.

For the second point, because virtual function needs extra space to store virtual table and other stuff, it will not be efficient to use virtual functions in classes are not designed to be polymorphic. In addition, it may cause problem when you transplant the code to other languages such as c and fortran.

“Effective C++” Third Edition by Scott Meyers.

Effective C++ item 6: Explicitly disallow the use of compiler-generated functions you do not want.

Share Button

We all know from previous posts that compiler will generate constructors, destructors  and assignment operator for you. What if you don’t need or you want to prevent compiler to generate them for you? The ways is to declare the corresponding member functions private and give no implementations. Here we will have a uncopyable class to demonstrate how to implement this.

If you do

The compiler will complain about assignment operator is private. and if you call it in a member or a friend function, the linker will complain about can’t find implementation of this function.

“Effective C++” Third Edition by Scott Meyers.

Setup git repository and start using git.

Share Button

Last week I tried to use git and here are some tips to set up git environment and a beginner tutorial.

  1. Basics
    The key concept of Git is branches. A branch is a separate code line with its own history. You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (normally named master). The user selects a branch and works in this selected branch, which is called the “working copy”. Selecting a branch is called “checkout a branch”.
  2. Installation
  3. Setup
    To list your git configuration: 

    To  configure your name and email:

    To enable highlighting:

    To set SVN-like alias:

    To add ignore rules:
    Firstly, create file ~/.gitignore and add these to the file:

    Then run
  4. Getting started with Git
    To create and add files to repository:

    To create a new branch:

    To switch to a branch:

    To delete a file from the existing branch:

    To push changes to branches/master of remote repository

    To pull changes from remote repository:

Get low price on hotels using priceline and hotwire.

Share Button
Probably you already know there is a service on priceline called “Name Your Price Negotiator”, which let you enter a price for a deal and if it’s rejected, then you enter a higher one. Hopefully you will get a reasonable price for that deal. But there is a better strategy to quickly get a good deal:
  1. Go to Hotwire.com.
  2. Enter your preferred hotel dates, star level, and geographic area in order to find out the price thatHotwire is offering the room you’re currently looking for.
  3. Next, go to Priceline’s Name Your Price Negotiator interface.
  4. Specify the same hotel dates, star level, and geographic area you input into Hotwire’s system.
  5. When it asks you to name your price, enter a price that is 70% of the price quoted by Hotwire(a 30% discount).
  6. Click enter to see if your offer is accepted.

Remember to give it a try next time you want to book a hotel or rent a car.