• Advertisement
Sign in to follow this  

For Beginners: The Importance Of Object Oriented Design

This topic is 1922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
[size=5][b]Enumerated Types[/b][/size]
I am not sure what this is called, but I would like to add the concept of not using strings as enumerated types. For example

[CODE]
void foo(string gender)
{
if (gender == "male")
{
// do stuff
}
else if (gender == "female")
{
// do other stuff
}
}
[/CODE]

This is bad for a couple of reasons. First of all it allows gender to be any arbitrary string. Meaning "Male", "MALE", "m", and "darth vadar" are all valid string values, but may not be valid gender values. So as a programmer trying to figure out what valid values I can pass as a gender I may have to dig around in the source code how it is used. Also, passing around and comparing string values is slower than enumerated types.

This is how it should be done
[CODE]
typedef enum _Gender
{
Unspecified,
Female,
Male
} Gender;

void foo(Gender gender)
{
if (gender == Male)
{
// Do Stuff
}
else if (gender == Female)
{
// Do Other Stuff
}
}
[/CODE]


Now a programmer can know what valid values are just by looking at the enumerated type. The compiler can catch a typo if I accidentally typed one the enumerated types wrong and passing and comparing enumerated types is super fast as it is merely and integer.

[size=5][b]Assertions[/b][/size]

Assertions are great for catching bugs right when arise rather than when their effects have been propagated through your code. Whenever you write a function and you are making assumptions on the data you are going to operate on. Put this assumptions on as assertions. For example.

[CODE]
public void foo(int index)
{
assert index >= 0;
// do stuff
}

public bool binarySearch(Array data, int value)
{
assert isSorted(data);
// do search
}
[/CODE]

These assertions will take more processing time in a debug environment but once you release the final product they will be gone. Using assertions is very useful for tracking down bugs early rather than later. It is almost counter intuitive but making your program crash when something is wrong rather than silently ignoring it will result is code has less bugs as it will help you track down problems sooner. If you aren't familiar with assertions I would definitely look into it.

Share this post


Link to post
Share on other sites
[sub][size=5]We got good stuff here! Amazing how each time that I come to this website the people, information, and help causes my understanding to grow.[/size][/sub]

[sub][size=5]Thanks, so much! [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] [/size][/sub]


[sub][size=5]Clinton[/size][/sub]

Share this post


Link to post
Share on other sites
[quote name='HappyCoder' timestamp='1350158468' post='4989876']
Assertions

Assertions are great for catching bugs right when arise rather than when their effects have been propagated through your code. Whenever you write a function and you are making assumptions on the data you are going to operate on. Put this assumptions on as assertions. For example.

public void foo(int index)
{
assert index >= 0;
// do stuff
}

public bool binarySearch(Array data, int value)
{
assert isSorted(data);
// do search
}


These assertions will take more processing time in a debug environment but once you release the final product they will be gone. Using assertions is very useful for tracking down bugs early rather than later. It is almost counter intuitive but making your program crash when something is wrong rather than silently ignoring it will result is code has less bugs as it will help you track down problems sooner. If you aren't familiar with assertions I would definitely look into it.
[/quote]
I think when talking about assertiions there should be a discussion of when to use assertions and when to use exceptions. I'm not saying for you to do it. But the question has been raised before. And beginners tend to think that one can be used for the other. Which is not always correct.

Share this post


Link to post
Share on other sites
[quote name='Alpha_ProgDes' timestamp='1350160991' post='4989881']
I think when talking about assertiions there should be a discussion of when to use assertions and when to use exceptions. I'm not saying for you to do it. But the question has been raised before. And beginners tend to think that one can be used for the other. Which is not always correct.[/quote]This. Assertions are there to evaluate the results of a function/method/subrutine/etc, not to validate incoming data (in that case you validate by hand and/or use exceptions).

When you need to ask the program "Is this result any good?" then you use assertions to check up if the algorithm works. If you need to ask "Is this input any good?" then you use exceptions (and normal data consistency checks of course).

In the case of binary search, you'd use the assertion after the search was completed to check if the object that was returned is the correct one.

Now if the input of the binary search method is incorrect, you'd raise an exception. Or at most, use an assertion after trying to sort the collection to check if the sorting algorithm works.

That's why ideally you'd get rid of assertions when you finish up your software. Because they're not there to validate data but to validate the results of the things you do with the data, to know if the algorithm you implemented works as intended. When the algorithms works as intended, assertions aren't useful anymore, thus you get rid of them.

Share this post


Link to post
Share on other sites
[quote name='TheChubu' timestamp='1350165539' post='4989898']
Now if the input of the binary search method is incorrect, you'd raise an exception. Or at most, use an assertion after trying to sort the collection to check if the sorting algorithm works.
[/quote]
Interesting. I've always wondered about pre-conditions and post-conditions for a method. I thought that something that you'd use assertions for. Whereas exceptions are to handle "unexpected" errors. Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
This forum isn't a place for people to post resources for beginners, but rather one for beginners to ask questions and engage in discussion. It's not your blog either; this kind of thread is more appropriate for a journal post or for hosting on your own blog.

Share this post


Link to post
Share on other sites

This topic is 1922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement