Archive for the ‘IDEs’ Category.

A new style guide or a new way to format your code in C++ or C# or any language: "Code like you speak"

Code Like You Speak

Ok, so I wrote a post about why I use long variable names, where I discussed how it is very easy to use long filenames with today’s IDEs. As I code more and more I realize how I want to write like I speak.

As I try to read other people’s code, I have to wonder if they could even understand it themselves if they went six months without touching it.

For example, lets say I am working with a database that has phone numbers and for each phone number I want to do check if it is valid:

Example 1

PhoneNumber p = new PhoneNumber("555-555-5555");
if (p.isValid())
{
    // do something
} else
{
    // Do something different
}

There is nothing wrong with the above code. It even makes sense, mostly because the first line where p is created is right next to the if statement. But of course that is not always the case.

Maybe I have dozens of objects for different database attributes including multiple ojects that start with ‘p’: PhoneNumber, PartList, PersonID, and they all have an isValid() method and those objects are created elsewhere in the code, who knows where. Also, your isValid() function in PhoneNumber checks if the PhoneNumber is formatted correctly, but the isValid() function in PartList checks if it is a valid part in the database and the isValid() function in PersonID does something different, now the above code would be more confusing, especially to some one else reading it (or to yourself six months after writing it).

if (p.isValid())
{
    // do something
} else
{
    // Do something different
}

So if we don’t have the declaration right next to the code, it is hard to understand…

You might say, “Load it in a debugger and just look…why is it so hard?”

My response is this. “What about your Team Leads for you Tech Support team? What about your documentation team? They may have that has access to read the code but they don’t have the tools or know how to compile it. But they may need to see your code to get their job done. And I don’t want to offend your project managers, but there sure are a lot of Project Managers that either never had the ability or have lost the skill to compile code.”

So someone else reading your code is lost. What does p represent? PhoneNumber, PartList, PersonId? What is the object? What does isValid() check for on this object?

So lets code like we speak. First you should say what we really want to do in your language. The sentence below is a valid English sentence and anybody who speaks English can understand it.

If the current phone number is formatted correctly, do something, else do something different.

Well, what if the code were written like this:

// This could be anywhere in code
PhoneNumber theCurrentPhoneNumber = new PhoneNumber("555-555-5555");

// A code like you speak if statement
if (theCurrentPhoneNumber.isFormattedCorrectly())
{
    // do something
} else
{
    // do something different
}

See how it is in “code like you speak” format?

So you may here different theories about writing code and whether to document with comments or not document and have clear code. I am for the theory that when you write your code you should assume that the person reading your code:
1. Doesn’t understand code.
2. Has no technical skills
3. Has an average IQ (in the 90s).
4. Is the worst technical ever who has to document your code…
etc, etc, etc…

So that is why “Code like you speak” is a great way to code. If your technical writer reads the first example, he is confused, but if he read the second, he fully understands the idea what it going one.

Now, it is important that you don’t over do it. You don’t need to have perfect English. Broken English is fine. As you practice you will get better at it. As you get better at it, your code becomes more readable and others can work on it easier. This is especially usefully when working in teams, or one a community-developed open source application.

Don’t go overboard! I mean, there is not reason to define the word “othewise” to replace “else”. If you did that, you may actually confuse veteran developers.

// In a header somewhere...
#define otherwise else

// This could be anywhere in code
PhoneNumber theCurrentPhoneNumber = new PhoneNumber("555-555-5555");

// A code like you speak if statement
if (theCurrentPhoneNumber.isFormattedCorrectly())
{
    // do something
}
otherwise
{
    // do something different
}

However, if the “Code like you speak” development style takes off, the above may not be so overboard, and may become a standard practice to define many English synonyms to help one code in a more clear and understandable method.

One might argue that because not every one speaks english, a language barrier would be more likely, and I would have to disagree. If you code like you speak, someone who doesn’t speak your language could still figure it out a lot faster using something like a language translation tool (such as google’s or babblefish’s) than they could by trying to figure out what a random object is.

As for other style guide information, there are lots of style guides. If you need one and don’t have one, start with this one:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Tool to create keyboard shortcuts that paste common data because code snippets don't work for C++ in Visual Studio 2008

So I was quite annoyed that code snippets doesn’t work for C++ in Visual Studio 2008.

So I have used a download of a program called KeyText for years. Since probably 2001. However, they no longer have a free version and so I set off to find a new program that was Open Source or FreeWare.

So far it is a no go. I think that such a tool is indispensable so I will probably have to purchase such a tool. Here is a list of tools I have found.

KeyText – $29.95 – http://www.keytext.com

Keyboard Express – $24.99 – http://www.keyboardexpress.com/download.htm

QuickPhrase – $14.90 (download only) – http://www.typingmaster.com/individuals/quickphrase

Hot Keyboard – $29.95 – http://www.hot-keyboard.com

Template Manager – $9.95 – http://www.comfort-software.com/template-manager.html

Flash Paste – $29.95 – http://flashpaste.com

SourceForge.net let me down…either that or I just did a poor job searching. While I still haven’t found an open source keyboard macro text pasting tool, there are some quality options above that are all low priced.

How to find the file and line number of memory leaks using C++ with wxWidgets in Visual Studio 2008?

Ok, so I am coding with C++ and wxWidgets using Visual Studio 2008 as the IDE.

I got the following output when my my program was launched in debug mode and it exited.

Detected memory leaks!
Dumping objects ->
{1535} normal block at 0x005C1920, 18 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CD CD

I can’t have memory leaks and while they aren’t a big deal and are with objects I create once so they really aren’t that bad, my obsessive compulsiveness (I’m just a little OC but not OCD) wouldn’t let me move on with the program or do anything else until I had solved these memory leaks.

I did some researching and tried a lot of things before I finally found this website:
http://www.litwindow.com/Knowhow/wxHowto/wxhowto.html#debug_alloc

So I gave the steps a try. I had a little bit of a problem but I got them to work, so I am re-writing the steps so that I remember how to do it again and don’t run into the same problem.

Steps for Finding Memory Leaks in C++ and wxWidgets using Visual Studio 2008

  1. Create a new header (.h) file called: stdwx.h 
    // wxWidgets precompiled / standard headers
    #include "wx/wxprec.h"
    
    // When debugging changes all calls to "new" to be calls to "DEBUG_NEW" allowing for memory leaks to
    // give you the file name and line number where it occurred.
    #ifdef _DEBUG
    	#include <crtdbg.h>
    	#define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)
    	#define new DEBUG_NEW
    #else
    	#define DEBUG_NEW new
    #endif
    

    Note: The site I linked to had much more in the header file, but I like to know the minimal requirements for the task at hand and so I commented out the lines that I thought didn’t matter and tested by recompiling and running in debug and sure enough, only the above lines are needed. However, that shouldn’t stop you from adding #includes you always use to your header file. Notice the use of #ifdef _DEBUG which means that this code will only be used when debugging and so your release code will not contain this debugging code (which is useless for release builds).

  2. Create a new .cpp file called: stdwx.cpp. Add A single line including stdwx.h.Yes, it is really only supposed to be one #include line as shown:
    #include "stdwx.h"
    
  3. Now add that same #include line to every .cpp file in your project:
    // Include the stdwx.h in every .cpp file
    #include "stdwx.h"
    
  4. Now in Visual Studio 2008, under Solution Explorer, right-click on the Project (my test project happens to be Dice at the moment) and choose Properties.
  5. Expand Configuration Properties | C/C++ and select Precompiled headers.
  6. Use the drop down for Create/Use Precompiled Header to select Create Precompiled Header (/Yc).
  7. Under Create/Use PCH Through File, type in stdwx.h.Note: The Precompiled Header File setting should auto-popluate with the correct value of $(IntDir)\$(TargetName).pch.
  8. Click OK to save the project properties.

A screen shot is included to provide further help on these settings:

Precompiled Headers Settings

Precompiled Headers Settings

You should now be ready to recompile your program and now instead of seeing just vague memory locations of memory leaks, you should see the exact file and line number, which is key in detecting and deleting the allocated memory.

Detected memory leaks!
Dumping objects ->
{1535} normal block at 0x005C1920, 18 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CD CD
c:\users\jbarneck\documents\visual studio 2008\projects\dice\dice\die.cpp(183) : {1529} normal block at 0x005C18D0, 20 bytes long.
 Data: <          \ . \ > 00 00 00 00 CD CD CD CD 20 19 5C 00 2E 19 5C 00

Copyright ® Rhyous.com – Linking to this article is allowed without permission and as many as ten lines of this article can be used along with this link. Any other use of this article is allowed only by permission of Rhyous.com.

Visual Studio 2008 editor colors set to use a black background and how to add these settings yourself or keep your color settings on re-install?

So I recently installed Windows 7 64 bit on my laptop. Before I was using XP Pro SP3 32 bit. I had visual studio 2008 installed and had the editor using a black background exactly how I like it.

So really, all I wanted was my colors in my editor. So it turns out you can export them.

  1. On you current install, go to Tools | Import and Export Settings.
  2. Choose Export selected environment settings and click next.
  3. Click the top box to remove the check box from everything.
  4. Expand All Settings | Options | Environment.
  5. Click to check the box next to Fonts and Colors
  6. Click next and save your file

I won’t walk you through importing it because you should be competent enough to do that on your own having now exported it.

Want it in a download?

Visual Studio Black Theme

Want just the XML? For those of you who also want a black background and just want the XML, here is my Environment_FontsAndColors section and a screen shot of it.

Visual Studio 2008 Text Editor with black background

Visual Studio 2008 Text Editor with black background

Here is the xml code, you can copy and paste:








      2














































































































Copyright ® Rhyous.com – Linking to this article is allowed without permission and as many as ten lines of this article can be used along with this link. Any other use of this article is allowed only by permission of Rhyous.com.