I'm working on a new project (C++), and the setup is fairly simple:
I've got 5 implementation files, a utilities header file, plus my main file. Of those implementation files, 2 of them consist only of utility functions I've previously written.
In my utilities header, I've got 3 basic structures, plus an enum. 2 of the structures contain 1 very basic function defined within them.
Now that you have the details, let's get on to the issue...
I needed to add a function to determine the intersection (if any) between 2 lines in 2d space. Naturally, already having this utils.h file, I decided to throw it in there. I probably shouldn't have, as it is relatively long and complicated enough that the compiler wasn't going to be able to inline it, but at the time I just threw it in there and tried to do a quick compile:
/tmp/ccnsMHTQ.o: In function `checkIntersection(Point2d&, Point2d&, Point2d&, Point2&)':
VerticalPattern.cpp:(.text+0x2e1): multiple definition of `checkIntersection(Point2d&, Point2d&, Point2d&, Point2d&)'
/tmp/ccqHjPfS.o:HorizontalPattern.cpp:(.text+0x20b): first defined here
/tmp/cc1AN3z7.o: In function `checkIntersection(Point2d&, Point2d&, Point2d&, Point2d&)':
Enemy.cpp:(.text+0x8e): multiple definition of `checkIntersection(Point2d&, Point2d&, Point2d&, Point2d&)'
/tmp/ccqHjPfS.o:HorizontalPattern.cpp:(.text+0x20b): first defined here
/tmp/cc6 _16 _11ouOwL.o: In function `checkIntersection(Point2d&, Point2d&, Point2d&, Point2d&)':
main.cpp:(.text+0x1ba): multiple definition of `checkIntersection(Point2d&, Point2d&, Point2d&, Point2d&)'
/tmp/ccqHjPfS.o:HorizontalPattern.cpp:(.text+0x20b): first defined here
collect2: ld returned 1 exit status
Huh? That function certainly isn't defined anywhere other than utils.h. So, what gives? My first thought was that I screwed up a header guard somewhere and that coupled with me throwing the definition of the function into the .h file may be causing some issues. So, I checked...
All header guards were fine. Then I started to wonder if it could be a circular dependency of sorts. But, why then is the newly added function the only one causing the linker error? Wouldn't everything else in utils.h also be causing problems? I decide to conduct a test to find out:
I added a very simple function definition to utils.h and placed it above the definition of checkIntersection():
void foo() { int x = 2; }
A quick re-compile, and...
I got the same error as before, except now also with respect to foo(). Looking back over utils.h, it becomes clear that the compiler is inlining the functions declared within my structures. This makes sense, because member functions defined inside of a class definition are implicitly inlined. Inlining means that the functions are replaced by inserting the function bodies at the locations in which they are called. This means the linker never sees them, thus has no reason to complain about them in my situation.
At this point, I have enough information to construct a simple test case to illustrate the issue and better debug it. I need the following setup:
* A utils.h header file that contains a structure which is composed of a trivial data member as well as a function.
* Also in that utils.h file, I need to define a free standing function.
* Lastly, I need to have 2 additional source files, both of which include utils.h, one of which can be the file that holds my main method.
Once constructed, I'm left with the following:
O.K., time to try to build it and see what we get:
/tmp/ccwuqYve.o: In function `utilsFoo()':
foo.cpp:(.text+0x0): multiple definition of `utilsFoo()'
/tmp/ccCIDNQe.o:main.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
Good. The test case is valid. main.cpp includes both foo.h and utils.h, and foo.h also includes utils.h, thus multiple definitions. Evidently, I'm making a false assumption about what I'm doing, what the header guards are doing, or both... and you know what they say about assuming. Time to stop assuming and starting knowing:
Header guards only prevent multiple definitions within a single translation unit. Take this situation for example:
foo.h - includes utils.h, bar.h
bar.h - includes utils.h
Header guards prevent the contents of utils.h from being included more than once within foo.h (a single translation unit): Once from directly including utils.h, and again when it includes bar.h which in turn includes utils.h.
So, isn't that what I was doing in my example? No, not at all. I was trying to rely on header guards to prevent multiple translation units from defining the same function. After compilation, main.o and foo.o both contain code for utilsFoo() because both main.cpp and foo.cpp include utils.h, which is where utilsFoo() is defined. It isn't being defined more than once per-file, but rather multiple times across all files (translation units). Thus, the linker has multiple object files that all have code for utilsFoo().
So, why does sticking the definition of utilsFoo() into an implementation file as opposed to a header file fix the issue? Because in that case, that implementation file is the only object file that has code for utilsFoo(). When other translation units include utils.h, they are simply making the declaration of utilsFoo() visible to themselves. That is to say, they are making themselves aware that the function exists and can be called, but they aren't pulling in the code for the function.
I feel much better now. Sure, I knew how to fix this from the outset, but it was more important to me to know why I needed to fix it.
So, you get a new idea for a project. It could be a game, a helpful desktop application, the next big iPhone application, etc... Your brain is working in overdrive with great ideas and you can't wait to start coding. This is normal, and it's great. No better feeling than fleshing out a brand new piece of software that you've come up with.
Chances are, you'll hop right into your IDE and start fleshing up some quick and dirty code to get the thing off the ground. You're probably too excited to bother with design documents and all of that boring stuff. This is fine to an extent, but before you get too deep into your quick proof of concept, you are going to have to make some formal outlines for your project if it is to be of any modest size.
Now that the boring stuff is done, (and unless you are working for a company or making a very large application, you shouldn't have had to spend too much time doing that boring stuff) you can get back to the exciting stuff: programming! At first, things are great. The excitement level is just as high, if not higher than it was at the outset of the idea. But sooner or later, something terrible happens. You lose that excitement and in turn you lose your motivation to work on the project anymore. So, what happened?
It is hard to say for certain, but it seems to be human nature, more specifically our tendency to prefer instant gratification, and who can blame us? We just came up with a new killer app, and we want to take the vision in our head and make it come to life. Such is the attraction to this field to begin with: making dreams and ideas into reality using our skills. However, this comes at a price.
Once you start getting deeper and deeper into your application, that buzz subsides and you are brought back down to Earth a bit when you have to go through all of the mundane tasks to convert your dream into snippets of source code. Of course, the scope of your project as well as the tools and methods you chose to use to create it will have a huge effect on these mundane tasks (converting between data types, proper design patterns, reusable code, GUI, etc...), but at the heart of the matter it is still the same issue: How do you get back that initial excitement after you drop off to the other side of the hump?
I'm not sure I have a good answer for that as I suffer from this situation myself in nearly every project that I start. I find that unless I complete something within a few days, this is almost certain to happen. One issue that plagues me is that I tend to have 3 or 4 of these "killer" ideas floating around at any single time, so once the initial buzz fades during the development of one idea, I go craving my excitement "fix" by starting one of the other ideas, and so the cycle continues. It is an easy trap to fall into for sure.
Income could certainly mitigate these effects. Professional developers encounter these same issues, and maybe more so because they are most likely developing someone else's idea, so that initial rush of excitement may never be there. However, it is their job, so they have no choice but to keep plugging away. On the other hand, if you are working on your own personal project in your free time and you have no financial stake in the application's completion, it can be much harder to get out of the rut. Sometimes just wanting that dream realized isn't enough, as I'm sure we've all come to find out all too many times.
I think the best solution I've found is balance. Although, there is a very fine line to walk. Initial surges are great, but we must take advantage of them and get as much done in those first few days or weeks as possible. Lay the ground work while your motivation is at its highest. Document your code extremely well, otherwise you'll lose time later trying to figure out what the hell you were doing when you revisit the project. That's where the balance comes in. Take breaks. Work on other projects for a few days, but always keep your main project fresh in your head. Ideally, your side projects should be very small pieces of software. Maybe proofs of concepts or something along those lines. It shouldn't be another full-fledged application. During these breaks though, keep your main project fresh in your head. Look over your source code once a day, revisit your design documents, add TODOs to the code if you happen to notice something, but don't add any actual code.
I believe these breaks will allow your mind to rest a bit and will prevent you from getting tired of a project. The side projects will keep your skills sharp as well as give you that initial buzz of excitement from working on something new, and hopefully this excitement can carry back over to your main project.
If anyone has something that works for them, please share. These are just some things I've found that help me.
For a while, I've wanted to develop a small application that I can use to sync my mp3 player with my computer, or vice versa. It tends to be a fairly common thing, as I'm always adding/removing songs from one or the other. It also comes up a lot if I do a new install of an operating system and I'd like to have some music on there temporarily; it's just easier to transfer it from my mp3 player rather than another file system on a hard drive somewhere.
Yesterday, I started thinking about this application again, and today I began fleshing out some things on paper. I was going over things such as how you determine if two files are identical (despite file name, file size, etc... which don't really tell you anything about uniqueness), what sort of sync behavior should be used (mutual-add, mutual-add/delete, etc...), and how to handle the more real-life situation where you've got a number of folders with sub-directories as opposed to just a list of files. I had all of these great ideas of how to solve these problems, and my brain was working overtime for the problems I didn't yet have an answer to. I was looking at source code for some common core UNIX file utilities like diff, cmp, cp, etc... looking for pointers...
And then it happened. I realized that I didn't need to make any sort of application. I realized my "problem" was non-existent. Why, you ask?
With the use of the built-in UNIX 'cp' command (file copy), and a few command line switches, I could achieve my sync behavior.
cp --recursive --update
Done.
--recursive handles the issue with multiple levels of directories, and --update handles the issue with determining if files are identical or not, as well as handling the mutual-add syncing.
The curious part comes in with my reaction to this discovery: I was totally bummed! As an engineer, you tend to want to have these problems to solve. Then, you are excited once you solve them. However, realizing there was never a problem to solve in the first place doesn't bring that same excitement, it in fact has the opposite effect.
It does illustrate a few important concepts of engineering though.
1. Don't re-invent the wheel. 2. Keep it simple, stupid!
Some engineers have a tendency to want to use complex or advanced solutions to problems which don't need them. I myself was bummed that such a simple solution would solve this problem, as I had envisioned a much more "sexy" method. This only leads to trouble down the road, and if at all possible, it's better to realize there's a better way to do something before you've finished doing it the wrong way.
So, yesterday was the end of my first full week trying out my new routine of math, programming, and guitar each day. I felt it was a good introductory week to help me get used to the effort and time required to do such a thing, and I learned a lot about what changes I'll need to make in order to be successful with this process.
I stuck to my plan about 4 days out of the 7. It was a bit of a bad week to start this, as it fell over a holiday weekend, and 2 of the days in the 7 I had softball games (one of which I was injured in). Nonetheless, it was simply an introduction into what it would be like, and successful in that regard.
My guitar playing has improved dramatically. Not to say that I was bad, but I'm able to play some things now after only 7 days than I really ever imagined I'd be able to. As for programming, I found myself solving some complex problems of which recently I found rather stubborn. The Math was probably the hardest thing to sit down and do, and I failed in this department more than any other. However, when I did sit down and work some problems, each day they became easier.
I guess the main things I learned are:
It's hard work to do a routine like this with 3 different things. Usually people have a focus on one thing, and that's easy.
Tracking progress is essential. I didn't do any sort of that this past week and I don't feel like I can measure what I've accomplished in much of a concrete way. With guitar it's easier because I can set a goal to learn a song or a part of a song, and then do it. The other areas will require some structure.
It works.
I'll update this again at some point, at the longest I'll wait until a full month, but I may update it before then. I encourage you to try this out, whether it's with 1 thing, 3 things, or as many as you think you can handle, although I wouldn't push it much past 3 or the point of diminishing returns will surely kick in unless you don't have any other daily responsibilities. I work 2 jobs, so it's a time management exercise as well to fit all this stuff in!
This encompasses more than just programming, but it's the best place for it to go and I didn't feel like creating a new blog section...
I have been pretty busy lately, or that is to say "I've always got a lot to do, but usually don't accomplish what I should due to lack of structure and being worn out."
So, in that light, I came up with a plan to improve my skills in the areas of which I require them: Programming, Math, and Music.
Every day I will do the following:
Write code - I tend to do this anyways, but it's easy to do a lot of work for a 3 days and then take a day off. Nope, not anymore.
Do Math - I'm not great at Math, but my shortcoming in it is only due to lack of general effort toward the subject. Each day I will work some Math problems, as I need to be ready for a placement test anyways, I also need to sharpen my skills and keep them sharp for my future career.
Practice guitar - Practicing guitar is one of the most horrible things ever. Note I said "practice" and not "play". It's hard to force yourself to do exercises, but unless you do, you get into a rut and you never progress.
After one week of doing this, I'll post an update and let you know how it's worked out. All 3 of these areas are very closely related in terms of brain function, so while only doing one of them each day would greatly improve that area, doing all 3 should have a greater effect. The idea behind this is that you can't simply work on these things for 8 hours in one day, and then not do anything for the next 3. It's about the constant repetition of the activity day after day that will bring results. It improves memory, coordination, muscle memory, etc... I'll also make it a point to sit down and read something (other than e-mail...) each day. I don't particularly need help in this area, but reading often improves coordination (line skipping and such), concentration, vocabulary, and all sorts of other things worthy of improving.
I think not only will this routine improve the areas of which I'm targeting, but that it will also improve my overall productivity in life as I'll be on a more driven and focused path throughout my day, and I'll be more inclined to get something done rather than just chill out.
Netbeans is an excellent IDE from Sun Microsystems. It is written in Java and runs on all major platforms. With the latest 6.1 release, Netbeans has really stepped up its game and can now compete with some of the market leaders for integrated development environments. For more information about Netbeans, visit http://www.netbeans.org
Below is a list of 10 really helpful (...and cool) features in Netbeans 6.1. My examples are illustrated using a C++ project, but almost all of them apply across the board to the other languages that Netbeans 6.1 supports: http://www.netbeans.org/features/index.html
1. Code Bookmarks - This feature allows you to bookmark individual lines of code for easy browsing later.
2. Build Platform - When working with a C/C++ project, you can select which platform you'd like to build the executable for.
3. Code Completion - If you've used Visual Studio, then you've seen this type of thing before. While coding, you can pause (or use Ctrl+Space) and a code completion dialog will pop-up. This works for standard library items as well as items specific to user-created classes.
4. Code Formatting - Netbeans 6.1 gives you the ability to customize every possible code formatting option you can think of. Each case is separated into a group, and then each item in that group is a check-box. Changes are reflected in real-time via a preview pane to the right which is populated with example code. Once you make changes to these settings, Netbeans automatically accounts for them while you're coding. Very cool.
5. Single File Compile - Tired of building your entire project when all you want to do is make sure that the file you've just changed compiles cleanly? Netbeans 6.1 offers the "Compile File" option to do just that.
6. Diff - Diff is a very powerful tool for tracking changes between 2 different versions of a file. It isn't source control, it's simply a tool to contrast 2 versions of the same file, and it's commonly used when writing software patches. Netbeans 6.1 gives you the ability to run a Diff on a file right inside of the IDE.
7. Favorites - Netbeans 6.1 allows you to add files from your project to a list of Favorites. This is extremely helpful in large code bases.
8. Symbol Tracking - While viewing a source file, simply right-click on a symbol and you have the option to track various things such as the definition/declaration of the symbol, its source/header file, all occurrences of the symbol in the current file/entire project, and more. Again, very helpful in large code bases.
9. Include Directories - When developing a C/C++ project, Netbeans 6.1 makes it easy to add additional include directories to it's search path.
10. Local History - This is one of the coolest features I've seen in a while. Just think source control, except it's stored internally and it's graphical.
Hopefully this list has gotten you excited about Netbeans 6.1. If not, read it again! :)
The topic comes up all the time: "Which IDE to you use?"
It doesn't matter which programming language is being discussed or the scope of the project, developers always want to know what the "other guy" is using. I tend to do all of my development in C/C++ and it probably comes as no surprise to others using Netbeans for C/C++ the shock and resistance I receive when I tell people this.
I'm a Linux guy. Up until last year, I had never written a lick of code under any platform other than Linux. When I was eventually "forced" to write some C++ under Windows, I felt like a foreign exchange student with no sponsor. I had absolutely no idea what was going on. I almost started to believe I didn't know how to program anymore. Where was gcc? How do I run this thing? How in the hell do I use Visual Studio? In walks Netbeans, the one thing I can always count on in my software development. That is, aside from the knowledge I've already garnered.
The fact that Netbeans offers a full-fledged IDE that runs on multiple platforms is enough in itself. The fact that it has support for every language you could ever want (or need) to write code in is nearly unprecedented. The fact that it does all of this better than any IDE I've ever seen is teetering on goofy. The fact that it's open source is down right nuts.
I, like many developers, am forced to write code in a number of different languages for a number of different platforms. It's frustrating enough having to remember the nuances of all of these different languages. You do something one way in Java, another in C++, a new way in Ruby... but that's not the worst part. Chances are, unless you are really just using an editor as opposed to an IDE, you have to write this code with a different development tool. Isn't the idea behind an IDE to make the whole development process easier? What's easy about juggling different programming languages along with different development environments? In walks Netbeans.
Netbeans provides developers with a consistent look and feel across all of its supported languages. This allows developers to use Netbeans for all of their tasks without missing a beat. There is no learning curve. If you've written Java code in Netbeans, congratulations, you can now use Netbeans to develop software in any other language. I think people overlook the benefits of this, mainly because it's not a very common luxury. The old school of thought between developers, namely C/C++ developers, is that you use a text editor on Linux, and Visual Studio on Windows. For a lot of people, that's a deal breaker. I can't say I blame them: Visual Studio is usually a nightmare to use for anyone coming from the purist environment that is Linux/UNIX C/C++ development. In walks Netbeans. You're a Linux C/C++ developer coming over to Windows for a project? No sweat, just install Netbeans, install the C/C++ development tools (compiler, make system, etc...), and get right back to work. The effect this has on productivity is astounding. Not only can you get right to work, but all the experience you gain while working on this project under Windows can be directly translated back to Linux, or whatever your native environment is because the IDE's interface is the same, because the IDE is the same!
I talk a lot about C/C++. Guilty. Every other language is on a "need to use" basis with me. However, I've done a lot of work in Java, some work in Ruby, and recently my first ever experience with JavaScript. Let's focus on JavaScript since that's a pretty old school language. The JavaScript project I was working on involved some proprietary Microsoft technologies. Given that, I was basically forced to use Visual Studio. Luckily the new version isn't so bad, but I still wasn't pleased. I was out of my element, and I had to worry about more than just the code I was writing. That's a bad thing. In walks Netbeans. In the 6.1 Beta, JavaScript support was added. Eurkea! I said. The nature of the project was such that I could write the JavaScript code separate from those other proprietary technologies I mentioned, so I installed the beta and got to work. Not only was the learning curve involved with Visual Studio cut out of the mix, but the fact that I had the reference documentation built in to Netbeans was instrumental in my rapid development iterations with this project having never written any JavaScript in my life!
The IDE war will never be settled. Maybe that's for the best, to each his own. However, the bottom line is Netbeans makes you a better developer. It helps you do your job, and it helps you do your job well. The fact that it covers the major elements of all of the languages it supports is second to none. Break your code down by classes? Done. Attach an in-depth profiler? Cake. Debugger? You betcha'. Source control? Look ma', no hands.
If you've never tried Netbeans, I strongly encourage you to do so. It's alright, you don't have to tell anyone that you're using Netbeans. You can lock yourself away, keep all the lights off, cover your face if you must, but just try it. Once you put all the hype aside, you'll realize just how invaluable a tool like Netbeans is. And then you'll wonder why you weren't using it years ago.