• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

And to give some visibility into what the world was like prior to thankfully now widespread UTF-8 Unicode, one friend explained the situation way back when as such:

When there is no common standard, people find alternatives. In Taiwan, developers decades ago created environments that trap system calls, interpret special two-byte combinations and draw ideographic characters stored as bitmaps in 24 by 24 points on floppies.

They got more than five different encodings running. Unicode sorted most of the problem, but there are still some documents left in the past. To correctly view the documents, people have to try one encoding, then check the contents, then try another… because encoding information is not stored with the data.

Japan still has two encoding schemes up till today.

The old ASCII encoding was largely made to accommodate American languages (the "a" in ASCII) and not much else. Prior to an attempt at a universal encoding, every country had several encodings, some loosely based on ASCII, that all tried to handle their respective languages. It's also a big part of why in the 50s through the mid 90s, it wasn't uncommon to see individual nations develop their own computer architectures.

Unicode also had its own pain points (see also: both kinds of UTF-16 and UTF-32), but UTF-8 largely solved those in a number of ways. See also; Rob Pike's history of UTF-8.
 

leroidys

Member
For you professionals, how productive are you on any given day? Is there like a programming equivalent to writer's block?

I wouldn't say so. I think of writers' block as literally not being able to proceed. I seem to always be forging ahead with code, even if I'm totally stuck there's always things to try out. The code I'm writing my be the wrong path and get completely scrapped, but I wouldn't say that it's analogous to writers block.
 
I thought of that but it gets tricky since you're allowed to move back and forth between states.
You could keep a stack of states traversed to handle going in reverse. An undo stack, essentially.

This seems more linear than that, I think just a FSM should be enough.

Don't get too carried away with "is it performant" first, that'll slow you down. Make sure you have a solution that works, then optimize. :)
 
For you professionals, how productive are you on any given day? Is there like a programming equivalent to writer's block?
For the first question, it's a combination of getting good sleep and being able to write down and reason about problems. Caffeine works alright for getting me through some days, but the crash is real, and the more caffeine I have, the more likely I am to just do things without really thinking them through. It's a balance of eating right and sleeping right.

For the second, I've run into a few problems where I was legit stuck and running into terrible, horrible problems building on someone else's work or (with adventures in graduate studies) absolutely clueless as to what to do next. And I think the solution's about the same. Take the time to be calm, think through options, think about who might help, write down what I've done so far and figure out what angles haven't been covered yet.

Much of programming is about staying healthy and being organized. Especially at the assembly level but this applies to all languages, you need to figure out a system to put things together that will make you WANT to go back to your old code. And that's the real challenge of programming, weighing that against deliverables!
 

Two Words

Member
I have a question about break in c++.

So I am in a spot where I might need to break out of a while loop, but the while loop is like this

while
{
..........
if
{
..............
if
break;
}

}


How far does that break break? Does it break through both ifs and the while?
 

injurai

Banned
I have a question about break in c++.

So I am in a spot where I might need to break out of a while loop, but the while loop is like this

while
{
..........
if
{
..............
if
break;
}

}


How far does that break break? Does it break through both ifs and the while?

It breaks you out of the nearest loop. It is unrelated to if logic. It does however break out of switch statements.
 

Two Words

Member
It breaks you out of the nearest loop. It is unrelated to if logic. It does however break out of switch statements.

Thanks, that confirmed my issue. Breaking out of nested loops sucks. Is there a more elegant way than setting off a boolean flag if you do break which breaks the outer loop if the flag is true? It's a little annoying to do that when you're nested while loop is pretty large.
 

Two Words

Member
Also, is there a somewhat cook book answer on how to avoid null pointer errors when dealing with linked lists? As it is now, I've tackled linked lists like this in just about any way I deal with them.

1. Check if the list is empty.
2. Check if I'm at the beginning.
3. Check if I'm at the end.
4. Check as if I'm in the middle until I reach the end.

Is there some basic part of linked lists that I am missing here? Throughout this project, I kept hitting null pointer crashes with every function I made and I had to keep tweaking my logic until I accounted for all the ways that can occur. I'm not entirely sure I've properly ensured I cannot get any null pointer errors. I feel like I only know that I'll have one if I happen to draw up the case which causes it. Any tips?
 

injurai

Banned
Thanks, that confirmed my issue. Breaking out of nested loops sucks. Is there a more elegant way than setting off a boolean flag if you do break which breaks the outer loop if the flag is true? It's a little annoying to do that when you're nested while loop is pretty large.

In Java you can name scopes. This way you can break out of a particular scope, not just the innermost loop.

Code:
   Scope1 : loop 1 {
      loop2 {
         break Scope1;
      }
   }
 

injurai

Banned
Anything like that in C++?

You would use boolean flags to cascade a series of breaks, or you can refactor the whole block of nested loops to be a function. Then use return if you want to break out of all of them. You can also use goto statements, but please don't.
 
You would use boolean flags to cascade a series of breaks, or you can refactor the whole block of nested loops to be a function. Then use return if you want to break out of all of them. You can also use goto statements, but please don't.

Eh, depending on the situation, I actually think gotos are fine. I mean yea, be careful and know what you're doing. But if you do, then knock yourself out.
 
Also, is there a somewhat cook book answer on how to avoid null pointer errors when dealing with linked lists? As it is now, I've tackled linked lists like this in just about any way I deal with them.

1. Check if the list is empty.
2. Check if I'm at the beginning.
3. Check if I'm at the end.
4. Check as if I'm in the middle until I reach the end.

Is there some basic part of linked lists that I am missing here? Throughout this project, I kept hitting null pointer crashes with every function I made and I had to keep tweaking my logic until I accounted for all the ways that can occur. I'm not entirely sure I've properly ensured I cannot get any null pointer errors. I feel like I only know that I'll have one if I happen to draw up the case which causes it. Any tips?

C++ classes. Or the equivalent C, which is to write functions like

Code:
node* ll_next(node*)
node* ll_prev(node*)
node* ll_insert(node*, node*)
node* ll_delete(node*)

The nice thing about this is that most of the null pointer checks can be wrapped up in these functions, and by separating out the logic into functions, you can write unit tests for them.

Once each function is properly unit tested, you're good to go.

Of course you still need to check the return values for null (actually insert shouldn't ever need to return null, but the others might)
 

alatif113

Member
Also, is there a somewhat cook book answer on how to avoid null pointer errors when dealing with linked lists? As it is now, I've tackled linked lists like this in just about any way I deal with them.

1. Check if the list is empty.
2. Check if I'm at the beginning.
3. Check if I'm at the end.
4. Check as if I'm in the middle until I reach the end.

Is there some basic part of linked lists that I am missing here? Throughout this project, I kept hitting null pointer crashes with every function I made and I had to keep tweaking my logic until I accounted for all the ways that can occur. I'm not entirely sure I've properly ensured I cannot get any null pointer errors. I feel like I only know that I'll have one if I happen to draw up the case which causes it. Any tips?

I like to draw out the problem to visually help me know what I'm doing and also make sure there arent any pointers left hanging. Like actually make squares representing the list and arrows for the pointers connecting them. Do that for the front, back, and middle. Unless you are inserting, yes you should always make sure the list isn't empty. You'll get more/less complicated results depending on the type of list (circular/non circular, double/single).

You could keep a stack of states traversed to handle going in reverse. An undo stack, essentially.

This seems more linear than that, I think just a FSM should be enough.

Don't get too carried away with "is it performant" first, that'll slow you down. Make sure you have a solution that works, then optimize. :)

Here's the solution I got if anyone's interested:

So I made the truth tables for the FSM and figured out that the current state, s1s2, depends on the previous state p1p2 by the following:

For "clockwise" traversal: s1 = p2; s2 = !p1;
For "counter - clockwise" traversal: s2 = p1; s1 = !p2;

I created a state variable initialized to zero. Every time a clockwise traversal occurred, state would be incremented and every time a counter-clockwise traversal occurred, state would be decremented. If state ever reaches a value of 4, then a full clockwise traversal has occured. If state ever reaches a value of -4, then a full counter clockwise traversal has occured. If any of the two extremes are reached, state is reset to 0.
 
For you professionals, how productive are you on any given day? Is there like a programming equivalent to writer's block?

Man, gracious, I hit the wall on the regular. I work at a bank, in a code base that quite frankly terrifies me. It doesn't in reality, but it seems to have every single evil coding practice you can imagine. Simple tasks are few and far between, and there is such an excessive analysis and testing burden just to verify the smallest change that it can be paralyzing. It's not because the requirements are hard, it's because the code is just so bad.* So yes, it's sort of like writer's block, but it's really like just shutting down, taking a mental break, though sometimes it does seem like Office Space where in a 40 hour week you do 15 minutes of real, actual work.


(*Don't let me tell you about the multiple failed rewrite attempts. Nobody has the resolve [real talk: the budget] to see one through, which only adds to the technical debt.)
 

maeh2k

Member
I thought of that but it gets tricky since you're allowed to move back and forth between states.

As long as the sensors work well and see every state (i.e. don't miss any transitions), a state machine is ideal here. Moving back and forth between states is what state machines do; it's not an issue. In this case, the state machine is rather simple. You basically already have it "on paper" including the transitions.
 

injurai

Banned
(*Don't let me tell you about the multiple failed rewrite attempts. Nobody has the resolve [real talk: the budget] to see one through, which only adds to the technical debt.)

Technical debt can be terrifying to deal with. I'm currently involved in a project where the current mission statement is to handle the technical debt that has built up. In reality we are only making it worse. It's really an effort to rewrite the code base just so we know how it works. I get the feeling whoever comes a long next will blame us for the state of the code, but in reality it's all we have, and we don't have enough people to handle things properly.
 
Do developers in other countries program in their native language, or is English universal? Like, would a developer in Japan type "string" in English or Kanji?

I've worked with code written by French, Japanese and Latvian developers. Aside from comments and variables/function names it was all string and int etc.

Although the Sony guys like to write the most arcane C I've seen in professional use.

For you professionals, how productive are you on any given day? Is there like a programming equivalent to writer's block?

Depends on the project. I spend a lot of time doing nothing because I work for a dumb company where the creatives don't know how to create basic design documents. Then I wait on art and zzz.

Programming blocks only happen at my job when some idiot asks for something that makes no sense and doesn't provide me with ample engineering time. So I have to magically engineer something out of nothing.

Do not go into interactive marketing. Shit industry.
 
For you professionals, how productive are you on any given day? Is there like a programming equivalent to writer's block?

If I'm on a project I'm passionate about, working for a company that doesn't put a lot of shit in my way, i go in bursts. I'll be ultra productive for a few weeks/months until i knock out some major new library/component/feature, then I'll spend a week or two just recovering from that burst of energy.

The problem is when I'm working on something I don't care about, or working on something in maintenance mode. Then I'm basically a zombie. I'm really fortunate i landed in my current role, and I learned I really love working on open source as a result of it. There's just less shit in my way when working on open source. And the bar is higher for engineers, because all your patches are free for the world to see. So you don't end up stuck with people who don't know what they're doing but can't be fired.

If you ever have the opportunity to get paid to work on open source, I highly recommend it.
 
Technical debt can be terrifying to deal with. I'm currently involved in a project where the current mission statement is to handle the technical debt that has built up. In reality we are only making it worse. It's really an effort to rewrite the code base just so we know how it works. I get the feeling whoever comes a long next will blame us for the state of the code, but in reality it's all we have, and we don't have enough people to handle things properly.

^ My brother from a different mother.
 

Milchjon

Member
Probably been discussed many times before in here, but what's the best Python IDE (for a beginner trying to do quick and dirty stuff)?
 
Probably been discussed many times before in here, but what's the best Python IDE (for a beginner trying to do quick and dirty stuff)?

Python Tools for Visual Studio is free, open source, written by Microsoft, and miles ahead of everything else.

If you're not on Windows though i have no idea
 

phoenixyz

Member
Probably been discussed many times before in here, but what's the best Python IDE (for a beginner trying to do quick and dirty stuff)?

PyCharm. The community edition is free. If you are a student you can register and get the Pro Edition for free (and every other product from JetBrains).
Although for quick and dirty stuff you could just use a good editor with linting like Sublime Text with the Anaconda plugin.
 

Two Words

Member
I finished my C++ programming project assignment. While I was doing all of my commenting, I started to notice some redundancies in my code. Certain blocks don't appear ever get reached due to the logic. Some statements can be merged into a much smaller block of code. Overall, things feel a little bloated. But it works as far as I can tell. Would it be worth it to try and tweak the program to streamline it? I feel like I'm dealing with a house of cards right now, and I may lose track of what I changed. I know I can simply save a backup file in case things go bad. But in general, when you feel like code is not necessarily impeding performance, but being just a little bit messier, do you clean it up or just leave it as long as it is still pretty readable?
 
I finished my C++ programming project assignment. While I was doing all of my commenting, I started to notice some redundancies in my code. Certain blocks don't appear ever get reached due to the logic. Some statements can be merged into a much smaller block of code. Overall, things feel a little bloated. But it works as far as I can tell. Would it be worth it to try and tweak the program to streamline it? I feel like I'm dealing with a house of cards right now, and I may lose track of what I changed. I know I can simply save a backup file in case things go bad. But in general, when you feel like code is not necessarily impeding performance, but being just a little bit messier, do you clean it up or just leave it as long as it is still pretty readable?

I always clean it up. Although I wouldn't do it without a backup of the source, and I wouldn't make a backup of the source so much as I would create a local git repository with my code in it. But then there's the learning curve associated with git. I suspect your time will be best spent learning git for future projects, and if you have enough time left over after you've learned the basics, you can hack on this file in your git repo
 

Water

Member
I finished my C++ programming project assignment. While I was doing all of my commenting, I started to notice some redundancies in my code. Certain blocks don't appear ever get reached due to the logic. Some statements can be merged into a much smaller block of code. Overall, things feel a little bloated. But it works as far as I can tell. Would it be worth it to try and tweak the program to streamline it? I feel like I'm dealing with a house of cards right now, and I may lose track of what I changed. I know I can simply save a backup file in case things go bad. But in general, when you feel like code is not necessarily impeding performance, but being just a little bit messier, do you clean it up or just leave it as long as it is still pretty readable?
As long as it's some small project, as opposed to ten years and million lines of gunk, always clean up as soon as you see something wrong.

Everybody who isn't a beginner and has common sense uses a version control system. You should never be afraid of making changes or losing track of changes you already made, and VCSs deal with that issue effectively.
 

barnone

Member
I'm looking for particular book recommendations. I prefer books that are not traditional textbook style. I want material broadly related to problem solving / software engineering. Any ideas?
 

Two Words

Member
I always clean it up. Although I wouldn't do it without a backup of the source, and I wouldn't make a backup of the source so much as I would create a local git repository with my code in it. But then there's the learning curve associated with git. I suspect your time will be best spent learning git for future projects, and if you have enough time left over after you've learned the basics, you can hack on this file in your git repo

Given that this is for a grade, I think I'll probably end up turning in the back up unless I can be certain that the streamlined version works just as well as the older version. I'll just save it on the side and work to clean this one up then.
 

Two Words

Member
How do you guys write psuedocode? I was thinking of trying a Zeno's paradox style method. I wouldn't literally do it in equal chunks of halves, but this is the idea.

I write my start and finish, and find some clean point in between that I can can identify what I need by that point. Then I choose a point between those two points and find another arbitrary midpoint and define what I need by that point. I continue to do this for a while until I have a sort of skeleton of the program. Nothing is defined on how the program will accomplish the goal, but there will be a series of checkpoints so that I know certain pieces of data are necessary by certain points. That way, when I do decide how I am going to do something, I have some picture ahead of what I need to plan accordingly. Has anybody found success in using some model similar to this?
 

NotBacon

Member
I write it just like I write normal code. Describe the program flow with important functions, then fill in the details.

Or at least I used to. I find myself writing less and less psuedocode. Only when I get into dependency hell or math-heavy sections these days will I sketch it out.
 

Two Words

Member
I write it just like I write normal code. Describe the program flow with important functions, then fill in the details.

Or at least I used to. I find myself writing less and less psuedocode. Only when I get into dependency hell or math-heavy sections these days will I sketch it out.

I am trying to get away from, drawing up main until I go "Oh wait, I need a function.", then drawing up the function, then returning to main until I realize I need another function.
 

injurai

Banned
I tend to think of pseudo-code as a mix of imperative math and functions. I don't really do anything specific. Just block out what I need to accomplish and when. For example with merge sort in your recursive function. You first need to solve for and split up your array. Then you need to call mergesort on each half. Then under the assumption both are sorted, you then need to shuffle them back together in a sorted manner.

How you will accomplish that can be thought through later. But I block out stuff in that manner. Control of flow, with loose math.
 

Two Words

Member
Something else I wanted to ask is, do you construct functions with the absolute assumption that the data being given into the function is valid and within the bounds of the function? Is it generally good practice to make functions that test data (not necessarily input validation) to be washed clean before entering a function, or do you prefer to take the data into the function, test if the data is good in the function, and act accordingly?

Here's an example. For this movie ticket buying program, users could only buy contiguous seats on one row at a time. So if you wanted 5 contiguous seats on row 3 that are available, you can order them, but if you wanted other seats on another row as well, you'd have to do that in a separate ordering instance. I decided that before I even allow the user to buy tickets, I determine what is the largest set of contiguous seats in the theater. I then tell the user that they can enter between 1 to however many the max contiguous seats is for that case. That way, if the user says they want to order 1,000,000 seats, I don't even bother going into the checkSeats() function. I just stop them right away and ask for a new number of tickets. The alternative would be to go into the checkSeats() function and see if I can find those 1,000,000 seats for them.

I know that programming is never a cook-book answer, and that different cases will have different solutions. But I am curious how people feel about this in a general case. Do you try to filter only valid data into a function as a standard until it is not feasible or unneeded, or do you only do it when you feel it is necessary?
 

injurai

Banned
I don't do much web stuff. But sanitizing inputs is a must when you can't expect input, and the input will then drive your systems to carry on in a certain manner.

Pretty much any internal function I write does zero checks. It has it's intended use, and it's the coders responsibility to call it only when appropriate and to guarantee that the input will be correct and acceptable. This usually goes for any library or api that you use. They guarantee a certain action if you use them correctly.

It's sort of a blood-brain barrier manner of handling things. Or a firewall if you will. Once passed it everything should be considered valid within that scope. Assume clients will be stupid and code contingencies. But for yourself you shouldn't be checking if input is valid for internal functions.
 
Something else I wanted to ask is, do you construct functions with the absolute assumption that the data being given into the function is valid and within the bounds of the function? Is it generally good practice to make functions that test data (not necessarily input validation) to be washed clean before entering a function, or do you prefer to take the data into the function, test if the data is good in the function, and act accordingly?

Here's an example. For this movie ticket buying program, users could only buy contiguous seats on one row at a time. So if you wanted 5 contiguous seats on row 3 that are available, you can order them, but if you wanted other seats on another row as well, you'd have to do that in a separate ordering instance. I decided that before I even allow the user to buy tickets, I determine what is the largest set of contiguous seats in the theater. I then tell the user that they can enter between 1 to however many the max contiguous seats is for that case. That way, if the user says they want to order 1,000,000 seats, I don't even bother going into the checkSeats() function. I just stop them right away and ask for a new number of tickets. The alternative would be to go into the checkSeats() function and see if I can find those 1,000,000 seats for them.

I know that programming is never a cook-book answer, and that different cases will have different solutions. But I am curious how people feel about this in a general case. Do you try to filter only valid data into a function as a standard until it is not feasible or unneeded, or do you only do it when you feel it is necessary?

The rule is: User input must be sanitized. Another good rule of thumb is: Try not to check things more than once. However you want to accomplish that is up to you. If you want to write a function called process_user_selection_and_dispense_tickets(), and the function takes the exact value the user typed in from the keyboard, then definitely check the values in the function.

If you want to write a function called finalize_order() which takes some numbers that have already been verified for correctness, then don't check it again.

Finally, if your function is going to make assumptions that the input is valid, then you can use the assert() function in C/C++. It will make your program crash whenever the data is invalid.
 

takriel

Member
Hey y'all, I'm new to programming and we're using node.js. I'm not sure where else to ask this:

Right now I'm stuck at my homework, and I was wondering if anyone is willing to take a look at my problem (it's not that hard for a programming veteran, I'm sure :p)

Here's a screenshot: http://imgur.com/YYptvnM

This is a little program in which you have to guess a word, the word being EAST.

What I need to do now, is to advance that concept further and make it so that the word to be guessed is chosen randomly at the beginning from a list or array of, say EAST, WEST, NORTH, SOUTH.

We have been given a function that determines a random number, namely:

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 0.999999)) + min;
}

But I'm not sure where to include it, or how to make an array and connect it to the randomizing function.

In other words, I'm quite lost, and would appreciate any help!
 

Staab

Member
What I need to do now, is to advance that concept further and make it so that the word to be guessed is chosen randomly at the beginning from a list or array of, say EAST, WEST, NORTH, SOUTH.
An array is pretty simple, instead of declaring just one variable like you did:
Code:
var guessword = "EAST";
You'll type your answers between brackets (that's how JS knows it's an array), separated by commas (and in-between quotes):
Code:
var guesswords = ["EAST","WEST","NORTH","SOUTH"];
The first word is then accessed by using guesswords[0], the second using guesswords[1], etc.. (like a drawer where you store items, the first drawer is 0, second drawer is called 1, etc...)

Then, using the random function that was given to you, which essentially gives you a random integer between two values (a minimum, min and a maximum, max), you're going to have it randomly select one of the words.
Since the function asks for a minimum and maximum value, you want to check your array: you have 4 items, since it starts at 0, you want to use 0 for min and 3 for max (0,1,2,3 are your options).
You can then simply use that function to grab one word and assign it to your guessword variable which will be the one you compare the answer of the user to :
Code:
var guessword = guesswords[getRandomInt(0,3)];
This just replaces the initial var guessword = "EAST";, since you'll be randomly assigning it a variable from your array instead of straight up telling him what the guess-word is.

Just add those before the do->while loop and you should be good to go.
 
Something else I wanted to ask is, do you construct functions with the absolute assumption that the data being given into the function is valid and within the bounds of the function? Is it generally good practice to make functions that test data (not necessarily input validation) to be washed clean before entering a function, or do you prefer to take the data into the function, test if the data is good in the function, and act accordingly?

Here's an example. For this movie ticket buying program, users could only buy contiguous seats on one row at a time. So if you wanted 5 contiguous seats on row 3 that are available, you can order them, but if you wanted other seats on another row as well, you'd have to do that in a separate ordering instance. I decided that before I even allow the user to buy tickets, I determine what is the largest set of contiguous seats in the theater. I then tell the user that they can enter between 1 to however many the max contiguous seats is for that case. That way, if the user says they want to order 1,000,000 seats, I don't even bother going into the checkSeats() function. I just stop them right away and ask for a new number of tickets. The alternative would be to go into the checkSeats() function and see if I can find those 1,000,000 seats for them.

I know that programming is never a cook-book answer, and that different cases will have different solutions. But I am curious how people feel about this in a general case. Do you try to filter only valid data into a function as a standard until it is not feasible or unneeded, or do you only do it when you feel it is necessary?

Regarding your second paragraph, you're going to find that you'll need to go into checkSeats() anyway upon input, as your data could have changed. Other users could have reserved or cancelled, and that could change your total number of available contiguous seats by the time any other user finalizes his or her selection. Certainly, running some checks up front to limit the inputs can be of value, but it doesn't absolve you of the need to check afterwards.

In more general terms of your first paragraph, I view it like this: if you have a public method, that method should verify (or delegate the task of verifying) the inputs for logical correctness. I don't care if another public method elsewhere has validated those same inputs -- by nature of the method being public, anybody can call it, so you can't guarantee input vectors. The last thing you want is your code to blow up because you didn't take a moment to validate against a null or a negative number or some other invalid argument. Your private methods should then not need to worry about validation (unless, of course, validation is their job) (this is assuming your code is well designed and that you trust yourself to give yourself good values -- and by "yourself," I really mean the class, not the author of it).

At the same time, if you're calling into a public method and you haven't already validated the inputs you're using (such as, you got them from a user or even a database), then validate before even calling. Look at it as if you're consuming a library, ignoring for a moment that you might have been the author of it. If you know that the library could potentially bomb because of what you pass it, you want to validate what you're passing. The last thing you want is for your program to fail unexpectedly, and quite often a source of failure is invalid data.

It might sound as if I'm saying have validation code all over the place, repeatedly, and I'm really not. If your program is well designed, your validation will be isolated. I simply advocate that the code being consumed validate arguments before working with them, and the consuming code validate parameters before sending them, particularly when the source of those parameters can't be trusted (as in, the data came from an external source).

In code terms, if you have a method that looks like (examples provided in C#/.NET):

Code:
public int Divide(int numerator, int denominator)
{
     if (denominator == 0) throw new ArgumentException("Denominator cannot be 0.");
     return numerator / denominator;
}

This is public method that will blow up with a particular input. If you're consuming that method, you do not want to provide that input! So you validate beforehand

Code:
int numerator = GetNumeratorFromSomewhere();
int denominator = GetDenominatorFromSomewhere();
int quotient;

if (denominator > 0)
{
     quotient = someObject.Divide(numerator, denominator);
}

This is what I mean by validation on both sides. You don't want to accept bad arguments, but you also want to be good citizen and not send them.
 
Top Bottom