How do you test codes and/or create new functions?

I am curious to know how experienced programmers code their projects.

For example:

When I am coding (I'm a CS student), I use an IDE (Xcode) and when I need to test something, I use Atom + terminal or, to be faster, I use cpp.sh website to try some codes.

I am not sure this is the best way to do it, but sometimes I feel that testing some code inside my project doesn't make sense because I need to comment out parts of it in order to see the specific output.

Or if I want to create a new member-function, but I just want to test this specific function, I don't like to run the entire project.

So my question is:
When you are programming and want to test a code or want to create a new function, do you code it inside your current project/class? or do you code it elsewhere and then include it in your project/class code?

I want to improve the way I code, so I need to know how experienced programmers do! :)


I wouldn't say I'm a pro ... I do Code Blocks with c/c++ and MinGW

For me I have tools on the side which I've made and some pre-made code I just copy and paste to get the core of my program started to test in, and then just play with it. This way I only have a compile time of 1 sec or less. Once the new code/feature is done, I know I can just add another #include"FileName.h" on my main program and start dropping in the new lines of functionality.

Now for me, my code is't too complex, I'm still building up my tool for plug and play, but it's what I do, and I hope this helps.
Last edited on
Hi,

I think what you are after is a thing called "Unit Testing".

https://en.wikipedia.org/wiki/Unit_testing

I have recently migrated to Eclipse IDE, it has a plugin called Cute:

http://cute-test.com/

Not that I have used it, just trying to point in the right direction :+)


Edit: Unit Testing with XCode:
http://www.techrepublic.com/blog/software-engineer/better-and-cleaner-code-unit-testing-with-xcode/
Last edited on
Thank you DTM256 and TheIdeasMan,

@DTM256, I will create a template on the side to test some codes. I liked your idea. I am spending too much time testing on the side, maybe if I create a template it will save me time. While I am learning how to unit test, this can help.

@TheIdeasMan, thank you, maybe it is time to learn how to unit test my code. I saw that Catch is an easy framework for testing, so I will try Cute and Catch.

Thanks!

I've been a professional software engineer for 3 years now.
How I test code... Badly :)

One really useful thing to do when creating a program is first create a detailed specification of the requirements it has to meet. I try to test as much as possible as I go along because the more work that is done before testing the more undesired effect will be present, and it's much harder to find the root cause.

The basic idea to how I test is write some code, test the ideal conditions and fix any errors and bugs, then test the non-ideal conditions.

Non-ideal conditions include things like
- The user types a letter when you've asked for a number.
- A file that you need doesn't exist on the system.
- The OS can't communicate to a hardware device.

I am entirely self taught and I don't have any practice in using standard testing methods such as unit testing, but if you have a very detailed spec of how things should work and you keep testing regularly then bugs are much easier to find.

To answer the original question, I tend to just write the functions into the existing code and test when I have finished adding a function or feature. For some complicated things like data analysis that has to be carried out within a larger program I'll create a project just for testing this and displaying results, once I've found the algorithms I want to use then I'll implement them into the source code for the main project.
At first may sound a little dangerous to add or modify code directly in your main project but that's where source control is incredibly useful... If something new you try doesn't work and you need to get back to what you had before just revert to a previous version of your source code.
Last edited on
Hello SatsumaBenji,

I agree that a detailed specification before starting is useful, however, for my simple personal projects, I don't see myself doing sequence diagrams and defining the architecture before coding... maybe I should from now on. :)

Yes, you are right, I will improve my source control usage. Sometimes, I only do a save file, but being able to discard all changes is really helpful!

Thanks and congrats for being a self taught software engineer!

Hi,

With the version (source) control, Git is a popular package for this. A lot of IDE's have GUI support for it, so it is easy to use.

Good Luck and Regards :+)
Yes, I use the source control included in Xcode and it is compatible with Git. At least it must be, because I can push and pull my commits to my github account.

Thanks! :)
One of the most successful things we do at work for unit testing (and developer support) is the "ctl program." For most of your subsystems, we create a command line program that lets you call the subsystem API directly. This proves very handy for unit test.

For individual libraries, sometimes we create test programs that exercise the library.

One handy trick when writing a test program: if the code tries to link in tons of other libraries, just create stub functions for the libraries. Got a function that calls the billing system with 10 parameters and returns the price of a widget? Write a version that always return $1 and link your unit test program with that version.

Finally, there's the fact that the more experience you gain, the fewer mistakes you make. Obviously this makes testing a lot easier.
To add another point, I think it's incredibly useful if you can design your code in almost a modular fashion. That meaning that you can isolate chunks of code reasonably well from other areas of code. For object oriented code try to avoid tying your classes too tightly together.
Good isolation let's you get a clear view of what parts need retesting when you change any existing code.
Topic archived. No new replies allowed.