defensive-programming

Defensive anti-multithreading class implementation

和自甴很熟 提交于 2019-12-10 21:42:02
问题 I have an object that is not safe for multithreaded applications (in several ways), and I want to provide an internal check to make sure that critical methods are not accessed concurrently. Question What techniques should I employ to detect and prevent multiple threads from accessing my class? Is it sufficient to track the Thread.ID on all methods, properties, etc. that the consumer may use? 回答1: Just document that it isn't thread safe. That's the convention used by the classes in .NET. 回答2:

JavaScript anti-silent techniques to indicate failure

末鹿安然 提交于 2019-12-10 02:09:40
问题 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. The function credit is part of an Account object. Here's the naive solution. function credit(amount) { this.balance += amount; } A major problem with this

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

若如初见. 提交于 2019-12-09 14:23:31
问题 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::ios_clear" as an error message from the what method of a downcasted catch ( std::exception ) of a fstream which has exceptions enabled. This doesn't mean much

Test Cases VS ASSERTION statement

蹲街弑〆低调 提交于 2019-12-06 21:57:54
问题 In my most C++ project I heavily used ASSERTION statement as following: int doWonderfulThings(const int* fantasticData) { ASSERT(fantasticData); if(!fantasticData) return -1; // ,,, return WOW_VALUE; } But TDD community seems like to enjoy doing something like this: int doMoreWonderfulThings(const int* fantasticData) { if(!fantasticData) return ERROR_VALUE; // ... return AHA_VALUE; } TEST(TDD_Enjoy) { ASSERT_EQ(ERROR_VALUE, doMoreWonderfulThings(0L)); ASSERT_EQ(AHA_VALUE,

Why is the MVC paradigm best suited for web applications?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:03:12
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? Gordon Well, basically: separation of concerns on the usage level, not the physical level. Quoting PoEAA on MVC MVC splits user interface interaction into three distinct roles. With MVC , you are separating presentation (V, C) from the domain logic (M) and you also

Is it possible that Java String.split can return a null String[]

孤街浪徒 提交于 2019-12-04 16:09:02
问题 Is it possible for split to return a null String[] ? I am curious as I want to try to be as defensive as possible in my code without having unnecessary checks. The code is as follows: String[] parts = myString.split("\\w"); do I need to perform a null check before I use parts post splitting? 回答1: It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String) says This method works as if by invoking the two-argument split method .

Is it possible that Java String.split can return a null String[]

对着背影说爱祢 提交于 2019-12-03 09:21:17
Is it possible for split to return a null String[] ? I am curious as I want to try to be as defensive as possible in my code without having unnecessary checks. The code is as follows: String[] parts = myString.split("\\w"); do I need to perform a null check before I use parts post splitting? palto It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String) says This method works as if by invoking the two-argument split method ...and String#split(String,int) says: If the expression does not match any part of the input then the

0xDEADBEEF equivalent for 64-bit development?

被刻印的时光 ゝ 提交于 2019-12-03 08:08:33
问题 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 constant, e.g. BAD_PTR.) If pInt is dereferenced before it gets a proper value then it will crash immediately on most systems (instead of crashing much later when

Does wrapping everything in try/catch blocks constitute defensive programming?

*爱你&永不变心* 提交于 2019-12-03 05:36:23
问题 I have been programming for the last 3 years. When I program, I use to handle all known exceptions and alert the user gracefully. I have seen some code recently which has almost all methods wrapped inside try/catch blocks. The author says it is part of defensive programming. I wonder, is this really defensive programming? Do you recommend putting all your code in try blocks? 回答1: My basic rule is : Unless you can fix the problem which caused the exception, do not catch it, let it bubble up to

techniques for obscuring sensitive strings in C++

一曲冷凌霜 提交于 2019-12-03 00:39:15
问题 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 app) will reveal the above string. What techniques should be used to obscure such sensitive data? Edit: OK, so pretty much all of you have said "your executable