defensive-programming

CLR: If a constructor fails will it always throw an exception?

In Delphi, if there was an exception during construction of an object: any allocated memory would be released and an exception would be thrown. For example, the following was guaranteed to either return a valid Camera object, or throw an exception: Camera c = new Camera(); You never had to check the resulting variable for null: Camer...

0xDEADBEEF equivalent for 64 bit development?

For C++ development for 32-bit systems (be it Linux, Mac OS or Windows, PowerPC or x86) I have initialised pointers that would otherwise be undefined (e.g. they can not immediately get a proper value) like so: int *pInt = reinterpret_cast<int *>(0xDEADBEEF); (To save typing and being DRY the right-hand side would normally be in a ...

Defensive programming against malicious attacks

The company that I work for is redeveloping an in-house product for external use. The product will initially be developed in C# using WPF, then ported to Silverlight. One of the focus points is coding against malicious attacks e.g. SQL injection etc. Questions: Can anyone recommend URLs pointing to articles on security 'best practic...

Defensive Programming: Guidelines in Java

I’m from a .NET background and now dabbling in Java. Currently, I’m having big problems designing an API defensively against faulty input. Let’s say I’ve got the following code (close enough): public void setTokens(Node node, int newTokens) { tokens.put(node, newTokens); } However, this code can fail for two reasons: User passe...

techniques for obscuring sensitive strings in C++

I need to store sensitive information (a symmetric encryption key that I want to keep private) in my C++ application. The simple approach is to do this: std::string myKey = "mysupersupersecretpasswordthatyouwillneverguess"; However, running the application through the strings process (or any other that extracts strings from a binary ap...

IOrderedEnumerable and defensive programming

Hi, I'm fond of defensive programming. I hate exception throwing, but this is not the subject of my question. I adapted an extension to linQ to be able to perform an order by with a column name public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string sortExpression) With defensive programming, this method ret...

Defensive techniques for ASP.MVC for internet facing site.

I am working on my first asp MVC project that will ultimately end up on a publicly accessible web server (I have worked on some internal apps in MVC). What techniques, practices should I be thinking about (specific to MVC or otherwise) to improve security. Off the top of my head obviously there is the AcceptVerb attribute for actions an...

How defensive should you be?

Possible Duplicate: Defensive programming We had a great discussion this morning about the subject of defensive programming. We had a code review where a pointer was passed in and was not checked if it was valid. Some people felt that only a check for null pointer was needed. I questioned whether it could be checked at a high...

Basic defensive programming

Possible Duplicate: Favorite (Clever) Defensive Programming Best Practices I am always advised by some programmers to pay concentration to easy debugging. What is defensive programming and to which extend should it be considered while practicing? And one more important question: is there any key things to consider while coding...

Robust Code framework?

I hate writing code that makes my software more solid. This is something the framework should have done! So, is anybody aware of a code "enhancing" utility that solidifies the code? If I had to create something like this myself, it would work as follows: When you're compiling your code with a Debug flag, it would auto-magically add "sol...

Java Defensive Copies

I've seen defensive copies coded like this void someMethod(Date d) { myDate = new Date( d.getTime() ); } But that doesn't make sense to me, isn't there a way in Java to create an Identical copy in memory of that object? I've read the clone() will not work in all instances, but I don't understand why ...

Why is the MVC paradigm best suited for web applications?

I'm fairly certain my professor will ask me why I chose to use MVC for my web application. Truth be told, I'm new to MVC. I read about it, I'm building a blog application using it, I think it's very logical to approach a problem this way. But why? O_O I draw a blank. How is better suited than say, building an N-tier application? ...

Getting meaningful error messages from fstream's in C++

What is the best way to get meaningful file access error messages, in a portable way from std::fstreams ? The primitiveness of badbits and failbits is getting to be bit annoying. I have written my own exception hierarchies against win32 and POSIX before, and that was far more flexible than the way the STL does it. I am getting "basic::i...

Why are fail fast style programs shorter than defensive style programs?

I have read about how the fail-fast style of programming in languages like Erlang end up with much shorter programs than the defensive style found in most other languages. Is this correct for all types of programs and what is the reasoning for this? ...

JavaScript anti-silent techniques to indicate failure

What would be a good way to report errors in JavaScript instead of relying on nulls, and undefineds when errors do occur and a function is unable to proceed forward. I can think of three approaches: do nothing throw an exception assert Here's a simple example scenario - a function that credits a user account with the amount passed in...

How to avoid key-loggers when authenticating access

As per the title really, just what can be done to defeat key/keystroke logging when authenticating access? I have just posted a related question (how-to-store-and-verify-digits-chosen-at-random-from-a-pin-password) asking for advice for choosing random digits from a PIN/password. What other reasonably unobtrusive methods might there be?...

How else can this code be optimized for defensive programming?

For my data structures project, the goal is to read in a provided file containing over 10000 songs with artist, title and lyrics clearly marked, and each song is separated by a line with a single double quote. I've written this code to parse the text file, and it works, with a running time of just under 3 seconds to read the 422K lines...

Defensive database programming- robust code with T-SQL?

In the application development there is a concept of defensive programming. How to implement defensive programming techniques and writing robust code using Transact-SQL? ...

What's the most defensive way to loop through lines in a file with Perl?

I usually loop through lines in a file using the following code: open my $fh, '<', $file or die "Could not open file $file for reading: $!\n"; while ( my $line = <$fh> ) { ... } However, in answering another question, Evan Carroll edited my answer, changing my while statement to: while ( defined( my $line = <$fh> ) ) { ... } Hi...

A question about checking for the existence of a file versus the directory being empty and reliability

Hi, I know that pretty much every programming language has a method to check the existence of a file or directory. However, in my case, a file is made which stores program settings. If it does not exist (ie !File.Exists or Directory.Count == 0 where Directory is the containing directory of the file), then prompt for some settings to be...