Sunday, March 16, 2008

Coding better C++

I've prepared a short guideline for writing better codes in C++. I've prepared the list from my C++ experiences and studies. Each item of the list needs good amount of explanation which is outside of the scope of this post. I'll try to explain them in the future posts in my blog. For now, if you need more information on an suggested item, you can look up on the internet or in the books.

Here it goes:
  1. C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.
  2. Have a C++ coding standard defined or follow the coding standard defined for your project or organization.
  3. Design from outside, design the interfaces first.
  4. Avoid using assignments in constructors, use initializer list to initialize members.
  5. Know the need of copy constructor and define when needed.
  6. Use virtual destructor when your class is used as a base class.
  7. Never call a destructor (unless you're using a placement new). If you do, you may need to revise your software design.
  8. Avoid using placement new.
  9. Avoid using Macros for functions. Use Inline functions instead.
  10. Avoid using Array, unless you have to. Use standard library containers like Vector, etc.
  11. Avoid C style cast, use the C++ casts.
  12. Use references when you can and pointers when you have to.
  13. Use composition when you can and private inheritance when you have to.
  14. Use const for function parameters when you can.
  15. Be careful about static initializations, you might get unexpected behavior if the static variables/objects has dependencies.
  16. Be careful about multiple inheritance. Understand the 'dreaded diamond'.
  17. Be careful about floats and doubles. They may give you unexpected behaviors.
  18. Avoid constant literals in the code. Separate them in static inline functions or macros.
Each suggestion, mentioned above, will take time to get used to (if you're not already). For example, item 1 says "C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.". Now if you were a C programmer, it is not easy to stop using C, for example: it may become difficult for you to stop using stdio functions and using iostream instead. It'll take time and practice. (Please note, stdio functions might have advantages over iostream but that's not the point here).

Once you get used to all of the suggested points, you'll find that your code is much much better than many others and of course, more optimized and less error prone. Let me emphasize, 'these are golden suggestions, try to follow them'.

Happy coding :).

How to begin web programming with PHP

I am often asked, 'how to begin web programming with PHP?'. Following is a response that I had given few weeks ago. Hope this will be useful for anyone willing to start learning PHP.

Things you need to know to begin web programming with PHP:

  • Good knowledge of HTML
  • Some knowledge of CSS
  • Some knowledge of JavaScript
  • Good knowledge of MySQL Database, SQL (as most php applications are database driven)
  • Good knowledge of PHP
  • Using and configuring Apache
Tutorial Links:

Here are the links to tutorials that may help you to accelerate your learning process.

Recommended Setup:

Recommendations:

  • Keep the references handy/near to you.
  • Search in Google for more tutorials
  • Look into well known PHP frameworks (search for: top php frameworks).
  • Look into open source projects to learn how real life applications are done.
  • Practice well :)

Thursday, January 10, 2008

C# never made me anymore happier than today!

I wasn't anymore happier than today in ... hmm... in the last few months!

I have been working with a project where I need to convert a VB.Net project into C# one. The whole project contains 40 other C++ projects which are mostly libraries and plug-ins. The full project is huge.. only the source files (.cpp, .h, .vb) are probably 50+ MB.

Now there are freeware tools, both web based and desktop ones, to convert a chunk of VB.Net code to C# ones. There are some good articles on that as well. SharpDevelop, the open source C# development tool has a built in feature to convert VB.Net codes/solutions to C#. Some web based free conversion services are based on SharpDevelop. SharpDevelop does a average job in real life applications (like the one I'm working with) but it's far from being 'good'. It produces numerous amount of errors which of course we need to fix manually. It's really poor for converting VB's "implicit type conversion", "globals" and many other common stuff!

As our client suggested, I used a commercial conversion tool, it's from vbconversions.net. This one was not as fool as the open source or freeware ones but it yielded a lot of errors too. One funny thing with this tool is that it horribly masses up name spaces and some function parameters (which are not of intrinsic type or has name space), and of course it fails handling VB's implicit conversions too.

After fixing thousands of errors for almost a week we finally got our project compiling! Then the final moment arrived, we wanted to run it! We did.. and (as to my expectation).. it crashed horribly!

After one more week of debugging and tracing down the almost untraceable, horrible and ugly crashes.. I almost began to loose patients!

But today, some horrible thread related bugs were solved dramatically.. and guess what.. finally .. we got what we wanted... we got it running today! The project loads and shows the 3D models the way it should do.. well, even though there are a lot of bugs we need smash!

When the project ran and gave me the same output of the VB.Net project, it was a beautiful thing to see and a very interesting moment. It made me really happy.. :)

Now, it gives a lot of inspiration and stamina to fix the rest of bugs. I'll have a formal QA first on this stage of the project and then start fixing. So rest of the bugs, be aware.. here I come.. :)

By the way, the client of the project I am working for is one of the leading game development companies in the world, everyone knows their name and it's interesting to work for them.

Monday, December 17, 2007

Upcoming Google Knol - a Wikipedia killer?

Google has just announced their latest upcoming application called 'Google Knol'. It's a tool for posting and sharing information in the form of articles. Much like Wikipedia, Google Knol just seems to be more social and focusing more on the author's name, and of course its 'Google' branded. 'Google branded' means Knol will have more higher rank results than the Wikipedia, answers.com or other sites in search results on general topics.

Users can share, rate, suggest edit, add contents to articles. There can be more than one article on a topic from different authors, unlike Wikipedia. In Wikipedia, all aurthors edit/enhance one article per topic. Wikipedia is 'topic centric' where as Google Knol will be 'author centric'.

Here's the intro at Google's official blog:
"The web contains an enormous amount of information, and Google has helped to make that information more easily accessible by providing pretty good search facilities. But not everything is written nor is everything well organized to make it easily discoverable. There are millions of people who possess useful knowledge that they would love to share, and there are billions of people who can benefit from it. We believe that many do not share that knowledge today simply because it is not easy enough to do that. The challenge posed to us by Larry, Sergey and Eric was to find a way to help people share their knowledge. This is our main goal."

So is this a Wikipedia killer? In my point of view, it's not gonna be an easy task for Google to takeover the popularity of Wikipedia anytime soon. They need something more jaw breaking (a feature, functionality or an idea) to do so. Well only time can tell the 'actual' story.. :).

More on Google's Blog:
http://googleblog.blogspot.com/2007/12/encouraging-people-to-contribute.html

Constructive comments by Roger Ehrenberg, who's not so optimistic about the success of Knol, is here:
http://seekingalpha.com/article/57457-google-knol-not-setting-the-world-on-fire.

Tuesday, December 11, 2007

Visual Studio.Net for free

Yes, many of us do not know this fact but there are free editions of the mighty Visual Studio IDE, the languages: Visual C++, Visual C#, Visual Basic and SQL Server. It's called the 'Express Editions'.

Visual Studio Express Editions (2005, 2008, etc) are free versions of Visual Studio professional editions with limitations on some professional and enterprise features. Targets of Express Editions are mostly beginners, hobbyist and students.

This very good strategy of Microsoft is definitely targeted to reach the mass market and make more programmers divert to or become Visual Studio developers. Open source tools, languages and operating systems are causing Microsoft to loose ground at an alarming rate over the last few years. The free editions are one of few and good steps of Microsoft against it.

Whatever the reasons for the express editions are, it's a great news for beginners, students and hobbyists. I've used C# express edition (2005) for a moderate sized application earlier this year and hardly noticed any significant changes. You can easily switch between the Visual Studio professional and express editions too. So, if you begin developing an application in Express Edition and want to move to professional edition later, it's a piece of cake!

Web site & downloads: http://www.microsoft.com/express/.

Ext JS 2.0 final is out

As the title says, Ext JS 2.0 final has been released on December 4, 2007. You can check it out and download it at extjs.com.

Ext JS 1.1 rocked my heart. I had used it in a bio-informatics web 2.0 application (basically a DNA sequence/template modification application with lots of complex business rules, built with .Net 2 and Ext JS). Ext JS 1.1 dramatically improved the application GUI over another GUI framework we used earlier.

Ext JS is definitely one of the best choices if you're planning for a heavy weight GUI based web application but may not be a good choice if you're planning for a lightweight application. The main reason behind this is the 'bluky' size of the library. To use all features of the library, you can include the full library in your application, which is around 580 KB for Ext JS 1.1.1 and 614 KB for Ext JS 2.0. In that case, the only thing may bother you, the lightweight lovers, 'a lot' is the load time of your page/application. It's definitely very noticeable in the slower speed internet connection (like dial-ups).

With Ext JS 1.1, it was difficult to choose the specific library parts of the library for any specific features or widgets, for example the nice window message box dialogs. 'Packages' didn't help much, or I do not know how to use them :). If you have any expectations from Ext JS 2.0 about being it any more lightweight or choosing specific library parts easily, it may disappoint you. But you'll definitely like the more dramatics Ext 2.0 has to offer over its predecessor.

Ext 2.0 offers newer components, layouts, better examples and a better documentation. It's worth of upgrading to it for your current web 2.0 application, if time permits.

Wednesday, December 5, 2007

Animoto - A.I. creates your music videos!

"Animoto is a web application that automatically generates professionally produced videos using patent-pending Cinematic Artificial Intelligence technology and high-end motion design. Each video is a fully customized orchestration of user-selected images and music. Produced in widescreen format, Animoto videos have the visual energy of a music video and the emotional impact of a movie trailer.

The heart of Animoto is its newly developed Cinematic A.I. technology that thinks like an actual director and editor. It analyzes and combines user-selected images and music with the same sophisticated post-production skills and techniques that are used in television and film. The technology takes into account every nuance of a song: the genre, song structure, energy, rhythm, instrumentation, and vocals. Whether it's punk, pop, hip-hop or a classical Stravinsky piece, every Animoto video is totally customized. Even videos generated with an identical set of images and music will each have a completely distinct set of motion design. No two videos are the same. They can be emailed, and embedded in pages on websites including social network sites like Facebook and MySpace."

The above text is taken from the Animoto web site.

So A.I. creates music videos for you! Can things be anymore cool? :) I guess this application will get huge popularity in a few days. YouTube fans won't wait to publish thousands of their own music videos, created with this, right away.

You can create only short length, 30 seconds videos for free but you can create as many as you want.

Check it out at http://animoto.com/.

Saturday, December 1, 2007

Why Good Programmers Are Lazy and Dumb

Please check out this very interesting article, you'll surely be convinced that you're lazy and dumb and will realize why you've been coding so hard all these years :). I really liked this article. Here goes the first few lines from the article:

"I realized that, paradoxically enough, good programmers need to be both lazy and dumb.

Lazy, because only lazy programmers will want to write the kind of tools that might replace them in the end. Lazy, because only a lazy programmer will avoid writing monotonous, repetitive code – thus avoiding redundancy, the enemy of software maintenance and flexible refactoring. Mostly, the tools and processes that come out of this endeavor fired by laziness will speed up the production."

URL: http://blogoscoped.com/archive/2005-08-24-n14.html

Chao!

Friday, November 30, 2007

How to become a good programmer

At ReliSource, many of the times I am involved in the hiring process and need to take several interviews per week/month. Over the last two years, I’m observing one unfortunate truth which is: Bangladesh is lacking enough skilled programmers. Some people have talked about this issue previously and I think it’s time to take some real steps to improve the situation so that we do not face a disaster in the near future.

I’m putting some suggestions here for the fresh graduates and the college freshmen on how to become a good programmer.

In my own definition, a fresh graduate good/skilled programmer should have the followings:

  1. Strong skill of one or more good languages like C++, Java and C#.
    1. Must have strong skills with control structures. Don’t mess up if you’re asked to print out triangle or other shaped piles of ‘x’s with loops.
    2. Must have strong skills with recursion. You must know how to transform a looped task into a recursive one and vice versa, for example: multiplication using addition recursively.
    3. If your language is C/C++, you must know how to play with pointers and references.
    4. Understand pass by value and reference.
    5. Clearly understand scopes and memory allocation, de-allocation. Know when a object is destroyed and when to destroy.
    6. Know the usage of all operators including bit-wise ones.
  2. In-depth knowledge of OOP.
    1. Only being able to write classes and doing encapsulation and inheritance is not what you should call good OOP.
    2. Clearly understand how function overloading, overriding, polymorphism works.
    3. Clearly understand how constructor/destructor (if any) works with inheritance.
    4. Clearly know the difference and use of Interfaces and Abstract classes.
    5. Know how to overload operators. Why and how copy constructor is defined/used.
  3. Know common data structures
    1. At least know the common data structures like stack, queue, linked list, doubly linked list (know circular version of all of them) and trees.
    2. Be a skilled implementer of any of those, have clear concept of how push, pop, add, delete, peek etc method works on those data structures.
  4. Know most common algorithms well
    1. You don’t need to memorize pseudo codes line by line but you need to have clear concept of most common algorithms of sorting(bubble, quick, merge, heap, bucket, etc), searching (including DFS, BFS), etc.
    2. As a fresher you must know their time and space complexities, pitfalls and improvements (if any).
  5. General computing concepts:
    1. Know processes and threads, how are they related to each other, how to program them, etc.
    2. Understand TCP/IP: Don’t think it’s only the network administrator’s task to understand TCP/IP. All programmers ever doing any network or web programming should have clear TCP/IP concepts and understanding.
  6. Be skilled in debugging in IDEs:
    1. Be skilled in any of Visual Studio/Visual Studio.Net, Eclipse, Netbeans, KDevelop, etc.
    2. Know how to debug your code.
  7. Have basic knowledge of Software Engineering and SDLC.

Some advice for college freshmen:

  1. Start with C++ or Java, avoid starting with scripting languages:
    1. If you’re learning programming for the first time, avoid starting with scripting or loosely typed languages like: PHP, ASP, Perl, etc or Visual Basic. It may destroy your understanding of program execution, data types, memory allocation, etc.
    2. Start with C++ or Java. If you want to me to be specific, start with C++, you’ll love it for the rest of your life.. :) It’ll be easier for you to learn (almost) any other language (like: C#, PHP, ASP, etc).
    3. If you ask, do you need to know C to start with C++? Or should you learn C first and then C++? C definitely helps a lot for learning C++ but it's not mandatory to start with C.
  2. If you want to be a good programmer, keep on coding at least 20 hours a week for the next 4 years :).
  3. Never stop learning new technologies that are coming out everyday.
  4. Know some of the many languages/technologies but be master of one. Know at least one language very well.

Good luck!

An interesting interview with great programmers

Please check out this interesting interview.

The following persons were interviewed:

  • Bjarne Stroustrup - C++ creator
  • Linus Torvalds - The Linux kernel author
  • James Gosling - The Java language creator
  • Peter Norvig - Research Director at Google, a well known Lisper, author of famous (in some circles at least) books about AI.
  • Guido Van Rossum - The Python language creator
  • Tim Bray - One of the XML and Atom specifications author and a blogger too.
  • Dave Thomas - Author of the “Pragmmatic Programmer”, “Programming Ruby” and other great books about programming.
  • David Heinemeier Hansson - Author of the Rails Framework - the new hot web development framework.
  • Steve Yegge - Probably the least known from guys here, but also made one of the most interestings answers, has a popular weblog about programming. He is also the author of a game called “Wyvern”.

The following questions were asked:

  1. How did you learn programming? Were any schools of any use? Or maybe you didn’t even bother with ending any schools :) ?
  2. What do you think is the most important skill every programmer should posses?
  3. Do you think mathematics and/or physics are an important skill for a programmer? Why?
  4. What do you think will be the next big thing in computer programming? X-oriented programming, y language, quantum computers, what?
  5. If you had three months to learn one relatively new technology, which one would You choose?
  6. What do you think makes some programmers 10 or 100 times more productive than others?
  7. What are your favourite tools (operating system, programming/scripting language, text editor, version control system, shell, database engine, other tools you can’t live without) and why do you like them more than others?
  8. What is your favourite book related to computer programming?
  9. What is Your favourite book NOT related to computer programming?
  10. What are your favourite music bands/performers/compositors?

Url: http://www.stifflog.com/2006/10/16/stiff-asks-great-programmers-answer/

Chao!