anup
Do you really know C and C++
by
, 18-12-2011 at 11:40 AM (9622 Views)
The idea of writing this blog entry came up when we started talking to students encouraging them to take up our C and C++ offerings. In the evolved world (as in terms of age and work experience) it was quite easy for folks to say that they do not know a particular lanugage especially C and C++, however, that was not quite the case with students. Most of them felt that they know these languages fairly well to not take up a module (or course) on them. Delving further, it came up that this was a mix of a genuine belief that they knew this language combined with an urgency to do something cool in a new language. While we cannot help much with the second part, this post is to clarify when you can safely say that you know these languages.
When to say you know a programming language
Simply put, programming is an art. Experienced programmers appreciate elegant code. What makes for an elegant code is language dependent. When you are able to write a large project, say, which is several tens of thousands of lines long, from scratch in a programming language, in an elegant fashion, you can safely say that you know that programming language. Again, the length of a project is not the only yardstick with which to measure complexity. In languages like Ruby you can be making use of intricate features to shorten your code length. This is something which Ruby excels at. However, the code length is a simple measure with which the students and novices can gauge the complexity of a project.
Elegant code
Elegant code will have the following qualities, some of which follow from what is known as negative programming:
- Will be easy to understand
- Will be easy to maintain
- Will be easily extended to introduce new features, without losing any of the above qualities
- Will follow a consistent style
- Will make use of language features well
Negative programming is a style in which no piece of the code is a duplicate of another whether in part of in whole. Once this is done the code becomes easy to understand, maintain and extend. The main advantage with negative programming is that it localizes changes.
However, this is easier said than done. Consider that you are working on a small piece of very large software and let us call it ‘X’. ‘X’ is used throughout this project and some of the usages you may not even be aware of. There is a requirement to update the behavior of ‘X’ for some usages. The first step which a novice programmer would do is to copy-n-paste ‘X’, may be encapsulate it in a function, modify it and call this from the parts where the new behavior is required. This is very easy for him, localizes his changes and allows him to finish his part fast. All the traits which will motivate him in the direction of taking up this approach.
However, this is the first blunder which has been committed and will subsequently lead to introduction of many bugs in the software. Why is that the case? As this software goes through other modifications a time may come when a bug has been found in ‘X’ and needs to be fixed. The person fixing the bug may not even be aware of the fact that now ‘X’ exists in two separate parts. He fixes ‘X’ in the original part, runs a test suite and is happy that the work is done. The bug might show up much later and will be difficult to localize as everyone believes that ‘X’ is fixed. The correct approach to fix this is to keep the common code in one place and move the differences to another place may be using functions or other language constructs. Run regression (tests) for ‘X’, create new tests for the updated behavior and save everything in the repository (software database).
Making efficient use of language features
Another aspect which you must master before claiming that you know a particular language is to be able to make efficient use of the features which that language supports. Some languages such as Java force you to follow a particular programming style. Java is a language in which the Object Oriented Programming (OOP) model is deeply embedded. Languages such as C++ allow you to easily use the OOP model, but have bypasses where in you are not forced to follow it. It is very important to make use of language features well. If you only do ‘C’ programming in ‘C++’, then you do not know ‘C++’ even though you might be using the a ‘C++’ compiler.
If you are programming in ‘C’, you must know and use the following well:
- Conditional expressions, loops and control structures such as switch-case
- Functions
- Structures and unions
- Pointers and arrays
- Dynamic memory allocation and de-allocation
- IO routines and command-line processing
- Memory checking tools such as valgrind and ensure that there are no memory issues
If you are programming in ‘C++’, then you must know well all the features mentioned for ‘C’ above along with the following:
- Classes and objects
- Inheritances and namespaces
- Overloaded operators and polymorphic functions
- Constants, strings and streams
- STL data structures and algorithms
- Design patterns
- Be aware of what is available in other libraries such as Boost
How to build expertise in a programming language
There are several stages to building expertise in a programming language. How long one spends in a particular stage depends entirely on his/her efforts and exposure. These stages are enumerated below, with the last stage being one of lifelong learning:
- In the first stage, you learn the language one feature at a time. You cannot move to the next stage until and unless you know well each of the features, their strengths and limitations in isolation.
- In the second stage you work on a large project making efficient use of language features. However, like any other form of art, you learn by copying. You look at other parts of a large project and mimic the style used in the piece which you are modifying.
- In the last stage, you are ready to architect projects from scratch. You are able to make intelligent choices about the features and patterns to use along with the language. Define the style of the project and create mechanisms where in other novices can come in and start updating. You are able to foresee the impact of the changes and how long something will take to implement.
How can VirtuQTM help?
VirtuQTM helps in all the above mentioned stages and tries to bring you closer to (3) quickly. Our programming modules contain a mix of (1) and (2) for all the features of ‘C’ and ‘C++’. Each feature is introduced and two sets of exercises are created for you to work on. The first set of exercises focus on the individual features which you just learned. The second set of exercises let you go through the experience of making use of the features which you just learned as part of a large project. The good thing about our exercises is that we build them one piece at a time and make use of the features which you previously developed in an integrated fashion. Good coding styles, making use of software engineering and development on the Linux platform are some of the other goodies which you get for free. So, what are you waiting for? Sign up now and get started immediately.