Archive for the ‘Java/C++’ Category
For every one who comes from the world of dangling pointers and manual resource management of C++, Java deceptively looks very similar, unless you look under the hood. (I’m pretty much a newbie to Java, who is forced to learn its quirks, primarily due to Hadoop). Most of these tips are written for grad students who’s life cycle can be succinctly paraphrased as “program, collect data, evaluate, rinse, repeat”.
Tip 1. Get a good IDE (read Eclipse)
Unlike the world of C++, where the standard library is essentially small and can for the most part be held in your head, Java has a massive standard library which includes features for everything under the sun. Keeping that in your head is not for the light of heart. Thats where a good IDE like Eclipse comes in, especially the content assist features can make your life a lot easier. Also, the IDE can take a lot of pain out of managing your projects and libraries (because Java has a preferred conventions for writing and using libraries). One more amazing thing with Eclipse is that its got a nifty Java debugger which lets you inspect among other things – the threads currently running in your program.
If you think eclipse is massive bloat or don’t have access to a powerful system for doing your development, (some times I do, where I have to ssh into some machine and change, recompile, rinse, repeat if I am running experiments) then get JDEE (an Emacs plug in).
Tip 2. Get Ant for Building
Ant is a simple Java build system based on XML (yeah.. yeah, you’ve to write XML, stop complaining). In fact, you can use their tutorial build file and start from there. It works with very little modifications, unless you have a special project needs (In which case, Internet is your friend). Say good bye to make files and man up and write some XML.
Tip 3. When you have multiple cores, use Java threads.
Java has threading built in, even though there are some pains with the thread synchronization, Java makes things a bit easier and the best part is on any Linux distribution with NPTL (which is almost every distribution out there), the Java threading model is essentially 1-1, which means you can use all those cores to do the hard work. This is great if you are running experiments under different conditions, in which case, each run of the experiment is essentially independent of others. This usually gives you a performance boost of more that 20-25% (well, but then, I have access to a machine with 8cores and 8gigs of RAM, so YMMV).
Here is a really nice tutorial to get started on Java threads
Tip 4. Learn to use Scanner and Console classes.
For those who mostly work with input output formats from text files Scanner is one of your best friends. It essentially encapsulates the work of reading a line, splitting it into various parts and converting them into your favorite data types. And for all your input requirements learn to use the Console class, which provides a nice interactive console. If you the simple readLine method does not satisfy your needs, Java has a complete regex library, and again, the internet is your friend.
Tip 5. Use jconsole
And for the last tip of the day, learn to use jconsole a monitoring tool for java applications which lets you inspect the amount of memory usage and active threads. If you are using this tool locally, then for the most part there is no configuration required – type jconsole at a command prompt and you should see a window with the list of process that are open and their PIDs. connect to one that you want to monitor and you are ready to track your java process.
Things get a bit tricky with remote process and security issues – so my advice is setup a VPN or use ssh tunneling. More information on using jconsole is available here.
So, thats all for now.
Emacs, for those who know me, I am an big fan of, almost to the point of being religious. And and recently I’ve found another one – Eclipse. Emacs, as most would know is the ultimate editor that is written in a dialect of lisp called elisp (which predates attempts to standardize lisp and common lisp) – was the result of a time and a place where almost every programmer wrote lisp, AI was a buzzword and Symbolics was a household name.
Thus, emacs, naturally was written in the language of its time – lisp. With over 3o years behind its belt, emacs is now a mature multipurpose software application that most people go to the extent of calling it an operating system. The things that made emacs such a huge success story was not only was it written in lisp, the language of the day, it was also extensible in lisp, the language that most programmers who first used emacs knew. Thus, every pet-peeve of almost every programmer was solvable with just a few lines of elisp. Extensibility – Thats what made emacs a huge success. With packages for everything from terminal emulation, remote editing, newsreaders and even a web browser – Emacs is one multipurpose software application.
With, the coming of the AI winter, lisp lost ground and eventually gave way to Java. Java, being severely used in the past 10-20 years has become the lingua franca of the time. And, with Java we have another emacs incarnate, something that’s not only written in Java, also extensible in Java – eclipse. It has the same extensibility as emacs has , though not as mature in terms of extensions as emacs. So, Is Eclipse the next emacs?
Today, I had an interesting conversation with a friend of mine, a self-professed Java geek and some one who is a hard sell for dynamic languages like python. And our conversation came back to the same topic, again and again – type safety. From what I know of Java, it seems more or less highly type safe unless there is some sort of ugly reinterpret_cast like construct. But, coming back to my home ground of C++, thinking about it, C++ is not really type-safe, In more cases than others, type-safety is nothing more than an illusion. C++, being a multi-paradigm language has a whole host of powerful and extremely useful features, which also makes shooting oneself in their foot extremely easy.
Here is my top three really useful features, which also are the big type-safety pitfalls.
- Variadics : Variadics is a really handy feature in C++, but its current syntax is a dark abyss for getting into type-safety hell. The way currently one handles variadics in C++ is this unholy mess of macros all starting with “va_”. These macros not only manipulate the stack directly, but return a memory image of the object. So, unless you are very sure of what type you are getting your hands dirty with, you are going to come up with serious bugs. The case here is even worse than python, where when you do the wrong operation on the wrong type, you get a run time exception. Which in my opinion is more type safe than the current scenario in C++.
- reinterpret_cast : reinterpret_cast is another potential pitfall that I’ve come across, especially when I am prototyping in C++. Sometimes, its just easy to do a reinterpret_cast and forget it. Its really handy when you are trying to develop with some one else’s code. That damn singleton class which needs one tiny extra bit of functionality that you absolutely definitely require, but you can’t extend it. So, what does one do? An ugly reinterpret_cast. And you’ve given yourself a golden pass to type-safety hell.
- void * pointers : These are the worst offenders. I am sure, everyone who has programmed in C++ have had enough pains with knowing how bad these little bastards can make your life. I won’t add to the woes, but just let me mourn along in silence here.
While I was at CAIR (Center for Artificial Intelligence and Robotics) I was amazed at the irreverance towards hardware. There were computers, mini-towers, powersupplies, office copiers almost everywhere and they were using it for all sorts of purposes. From quick paper-stands to the kitchen sink.
But when ever you see a nice spick and span datacenters, with airconditioned looks on prime realestate, You wonder what they are housing there – a utility or endangered animal species. Computers and primarily datacenters are utilities and they should be treated with the same kind of irreverance that you treat your diesel generator or air-conditioning condensor with. Put them in some hidden corner, devoid of human intervention. There were netware systems that could do this for years, to think modern systems couldn’t is foolishness.
And finally sun has realised this and is trying to create a market niche in precisely this segment with their blackbox servers. They are built out of a standardised shipping container. It already reeks of “Made In China“. This is a step in the right direction, lets see where this takes Sun.
Warning: If you are new to programming, DO NOT READ THIS POST. GO BACK.
I’ve been going through the most amazing period in my life now. I’ve actually started to work for CAIR, DRDO this year and I am waiting to return there once my exams get over. I am enjoying every moment of it. So, now what does loosing one’s zeal have to do with enjoying oneself in a job that you really like? Nothing. But Its the lesson that I learnt, that has made me lose my zeal. So here goes the story…
I started my journey into this wonderful world of Lisp some time back, and it literally put my brain through a mixer. If you haven’t had the fun of putting your brain on a roller-coaster and watching it from afar, then you should try lisp. I promise you will enjoy it. Infact, Lisp is such an amazing language that if you don’t turn into a zealot, then you never got it in the first place, or so I thought. Now, moving a bit back in time, say a couple of years back. This was the time I learnt this funny language called python. I had the same “brain in the roller coaster experience”, dynamic typing, garbage collection, containers as part of the language, what not. This was like taking a kid who has never seen anything more than his own backyard playpen and giving him an “All rides free” pass to disney land. Thats exactly how I felt coming from programming in C/C++ for as long as I can remember. If you are a C/C++ hippie, or you are one of the many unfortunate souls who have been put through the torture of going through anna university thinking that all code is written inside of braces, take a look at python. I promise its great fun. Now all this talk about C++ brings about older memories… far older, when I was still in 8th. I had discovered the C programming language. I could let my computer dance to my tunes, or atleast dance at the tunes it was producing. I was amazed at all those silly vector graphics that I could produce with good old borland’s graphics library. I was amazed at my own skill, I endeavoured to write small games which no one but I would play (all night long). The fact that the computer could actually do “anything” I wanted excited me to an infinite extent.
See a pattern? Yes, I’ve been attached to each and every programming language I’ve ever learnt, thinking I could conquer the world with my new knowledge. How so naive of me. If you are under the same illusions that I was suffering from, I suggest go and write “production” code. Why? because production code is a wholly different deal from your hobby programming. if you are programming because there are no more good blogs to read, then its ok if you don’t put a tricky loop doing network i/o inside a try – catch/except (unwind-protect) kind of construct. But thats a big NO with production code. Thats downright stupid in production code. And you thought your pet language could do anything faster, better and sometimes even in supernatural ways than those of the “other” lesser languages? “Ha Ha Ha”, you self-delusional lemon-eater. In production code, when the demands are high everything that’s available tends to hit a brick wall. Wether its the global interpreter lock in python (one thats biting me big time right now) or the lack of good threading in lisp or the fact that working with C is like building a sky-scraper with lego blocks.
The fact that the available tools don’t really fit your needs is the ground reality, no matter who says what on which news group, trade show or promotional video. The reason you got hired was precisely because of this reason. I am here to retrofit it so everyone else can have a gala time. I am here to give these people the tools, the frameworks so that they can write their code without them worrying about things like “deep-copy vs. shallow-copy”. They can create threads without the worry that their pricey “dual-core” system doesn’t see it as a huge process which is using too much cpu time and asks the scheduler to put it to sleep. Its these kind of things that I am building, that has brought upon this realisation.
That no language, no matter how fun, mind-blowing, roller-coaster loopin a language it is, it ain’t perfect
This has also sucked the zeal that I had whenever I had embarked on learning a new language. Strangely it has made me cynical of these languages. Whenever I start seeing a new language, I can hear myself saying, “Hmm.. thats where I would run into trouble..”, Its made me turn pessimistic. Probably it gave me a little more maturity in language design, but thats debatable.
I am constantly mumbling three words in my sleep, consciously when I am trying to debug code, or think about problems of/in design. I am chanting them to keep me going, to stop my pessimistic “inner demons” from taking over, I am mumbling those three words as I type right now. I am mumbling “Half glass full.“