Math in Programming (again)

I’ve discussed this before but another thread popped up a couple of months ago and I had to give another explanation on the significance of mathematics in software development.

Excuse me?
I just want to ask if the computation in math is using for programming? Cause I hate Math :-|

Almost all programming deals with logic, and Logic is part of Math. So yeah, you shouldn’t be a programmer if you don’t want to do math.

But I’m getting ahead of myself… the real question here is: “Why do you hate math?”

If you hate math because you think it’s all memorization and weird symbols, you do not hate math – you simply hate standardized education from last century.

I love math, because for me it’s a fun way of exploring and discovering the mysteries of the reality we live in using only a handful of tools. I do not consider the memorization and weird symbols as the whole of math, I just think of them as a necessary (albeit annoying/painful) part of math – no different from a novice carpenter getting injuries from learning his tools.

The problem with most math classes is that they are too abstract to the point that learning the topics become almost faith-based. (A good example of faith-based teaching can be seen in Richard Feynman’s essay on Brazilian Physics eduction.) If students were introduced to calculus using this or polar coordinates using this, they might have a different opinion on the subject.

Back to your question, the other commenters have already given most of the things you need to know i.e. no, most of us programmers don’t do hardcore math from day to day; however, learning math practices your abstract reasoning and problem comprehension skills, both of which you will use everyday. (Think of soldiers doing push-ups. You won’t see them do those in the heat of battle, but it helps condition their bodies for the battlefield.)

Oh, and yeah, there’s one field in software development that everyone wants to get into but actually deals with a lot of hardcore math: Game Programming.

Aa. Okay i just want to ask if that math subjects is so hard to get? :)

They are hard, but they are doable as long as you have proper study habits.

Different maths require different skills, though. For example, Linear Algebra is more meticulous to detail, Calculus stresses your pattern recognition abilities, and Discrete Math needs both critical and out of the box thinking.

Programming Joke

A wife asks her husband, a programmer, “Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?”

A short time later the husband comes back with 6 cartons of milk and his wife asks, “Why did you buy 6 cartons of milk?”

He replies, “They had eggs.”

I find this to be the most unfunny programming joke in the dozens of programming jokes I’ve heard since I started programming. It’s even worse than the “Why is Halloween the same as Christmas?” joke – at least that one’s a cute coincidence for number systems.

At first glance, it looks like a straightforward joke: a direct translation of the statement make the husband buy six cartons of milk:

milk_to_be_bought = 1
if they_have_eggs
  milk_to_be_bought = 6
end
buy_cartons_of_milk(milk_to_be_bought)

But that’s not even a direct translation! A direct translation would be:

buy_one_carton_of_milk
if they_have_eggs
  buy_six_cartons_of_milk
end

This results in the husband buying 7 cartons of milk!

But no, the conversion from command to program is still incorrect. Here’s a much more direct translation:

buy_one_carton_of_milk
if they_have_eggs
  get_six
end

get_six what?

That’s the thing: no self respecting programmer would do what the husband did in the joke. A joke computer scientist would not do anything until the linguistic ambiguity is resolved, while a joke software engineer would also refuse to do anything until the unclear requirements are clarified, documented, and signed off.

Now if the joke went like “A wife asks her husband, who just started Codecademy last night…“, it would probably work.

Things To Do This New Year: Software Engineering

You know the drill.

Learn a new language to complement your programming skills.

It would be a typical New Year’s resolution for developers to learn a new programming language this year. But seriously, what’s the point of learning C# when you’re a Java developer (or vice versa)?

What you should be striving for are programming languages that are orthogonal to your current skill set. If you’re an enterprise developer used to statically typed OO programming languages, try dynamic languages like Python and Ruby. If you’re already using dynamic languages, try your hands on functional programming like Erlang and Scala. Same goes for platforms: web developers might want try programming in RIAs.

The point here isn’t to add bullet points to your resume, but to have different ways of looking at problems, like adding new tools to a toolbox. For example, had I not been aware of the basics of functional programming, I might have tried to force traditional Java-like synchronization techniques in my Google Wave gadgets instead of the more elegant FP approach.

Just a short plugging:

Rapid Development‘s Classic Mistakes (in software development) was a real eye-opener for me when I read it four years ago. Even though it was written almost a decade ago, a lot of the mistakes listed there were still present in my company.

To keep the list up to date, Construx (Steve McConnell’s company) is now holding the Classic Mistakes survey for 2010. Help update the study by taking the survey here.

Code Comments

Code Comments: novice programmers either use too much or too little, while so called “experienced programmers” don’t even know where they should be placed.

Actually, the proper use of code comments is one of the issues in software engineering with the least amount of debate. Overall, there are only two rules of thumb to keep in mind when using code comments.

Comments should explain the why instead of the how or what.

In other words, your comments should not repeat what the code is saying. Code Complete uses these two examples:

//set product to base
product = base;

//loop from 2 to "num"
for ( int i = 2; i <= num; i++ ) {
    //multiply "base" by "product"
    product = product * base;
}
System.out.println( "Product = " + product );
//compute the square root of Num using the Newton-Raphson approximation
r = num / 2;
while ( abs( r - (num/r) ) > TOLERANCE ) {
    r = 0.5 * ( r + (num/r) );
}

System.out.println( "r = " + r );

Both code snippets don’t look good (i.e. the variable names are bad). However, only the latter’s comments are useful, and the former would be better off with no comments at all.

If the code is so complicated that it needs to be explained, it’s nearly always better to improve the code than it is to add comments.

There are many ways to do this, ranging from using more descriptive variable names to inserting white space when needed. My personal favorite is to extract methods.

...

//get user input
...

//process user input
...

//format and display output
...

In this case, the programmer crammed all of the logic into one function. This usually happens when the developer reached his state of “flow” and just keeps on typing hundreds of lines of code into the system. The problem with functions longer than a couple of dozen lines is that not only is it hard to read, it’s also hard to debug.

By extracting the logic of the comments, we can have clearer code without even using comments, not to mention an easier to debug program:

public void run() {

	getUserInput();

	processUserInput();

	displayOutput();

}

private void getUserInput() {

	...

}

private void processUserInput() {

	...

}

private void displayOutput() {

	...

}

And yes, this is post is a lame excuse for trying out the SyntaxHighlighter plugin. :P

Quick Programming Challenges

As a guy with over a decade of programming experience and a good mathematical background, I occasionally want to throw all the other aspects of software engineering out of the window and just program stuff. Here are two sites I visit to get my quick programming fix.

Continue reading “Quick Programming Challenges”