defensive-programming

Erlang's let-it-crash philosophy - applicable elsewhere?

孤人 提交于 2019-11-28 14:25:40
问题 Erlang's (or Joe Armstrong's?) advice NOT to use defensive programming and to let processes crash (rather than pollute your code with needless guards trying to keep track of the wreckage) makes so much sense to me now that I wonder why I wasted so much effort on error handling over the years! What I wonder is - is this approach only applicable to platforms like Erlang? Erlang has a VM with simple native support for process supervision trees and restarting processes is really fast. Should I

Copy constructors and defensive copying

风格不统一 提交于 2019-11-27 14:43:00
What is a copy constructor ? Can someone share a small example that can be helpful to understand along with defensive copying principle ? Here's a good example: class Point { final int x; final int y; Point(int x, int y) { this.x = x; this.y = y; } Point(Point p) { this(p.x, p.y); } } Note how the constructor Point(Point p) takes a Point and makes a copy of it - that's a copy constructor . This is a defensive copy because the original Point is protected from change by taking a copy of it. So now: // A simple point. Point p1 = new Point(3,42); // A new point at the same place as p1 but a

When should I use Debug.Assert()?

廉价感情. 提交于 2019-11-27 05:48:22
I've been a professional software engineer for about a year now, having graduated with a CS degree. I've known about assertions for a while in C++ and C, but had no idea they existed in C# and .NET at all until recently. Our production code contains no asserts whatsoever and my question is this... Should I begin using Asserts in our production code? And if so, When is its use most appropriate? Would it make more sense to do Debug.Assert(val != null); or if ( val == null ) throw new exception(); In Debugging Microsoft .NET 2.0 Applications John Robbins has a big section on assertions. His main

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

南楼画角 提交于 2019-11-26 21:15:03
I'm reading some lecture notes of my C++ lecturer and he wrote the following: Use Indentation // OK Never rely on operator precedence - Always use parentheses // OK Always use a { } block - even for a single line // not OK , why ??? Const object on left side of comparison // OK Use unsigned for variables that are >= 0 // nice trick Set Pointer to NULL after deletion - Double delete protection // not bad The 3rd technique is not clear to me: what would I gain by placing one line in a { ... } ? For example, take this weird code: int j = 0; for (int i = 0 ; i < 100 ; ++i) { if (i % 2 == 0) { j++;

Copy constructors and defensive copying

雨燕双飞 提交于 2019-11-26 16:55:11
问题 What is a copy constructor ? Can someone share a small example that can be helpful to understand along with defensive copying principle ? 回答1: Here's a good example: class Point { final int x; final int y; Point(int x, int y) { this.x = x; this.y = y; } Point(Point p) { this(p.x, p.y); } } Note how the constructor Point(Point p) takes a Point and makes a copy of it - that's a copy constructor . This is a defensive copy because the original Point is protected from change by taking a copy of it

How to hide strings in a exe or a dll?

穿精又带淫゛_ 提交于 2019-11-26 10:35:34
问题 I discovered that it is possible to extract the hard-coded strings from a binary. For example the properties view of Process Explorer displays all the string with more than 3 characters. Here is the code of a simple executable that I wrote to simply test it: #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 #endif #include <stdio.h> #include <tchar.h> #include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { _TCHAR* hiddenString1 =_T(\"4537774B-CC80-4eda-B3E4-7A9EE77991F5\"); _TCHAR*

What&#39;s the purpose of using braces (i.e. {}) for a single-line if or loop?

蹲街弑〆低调 提交于 2019-11-26 07:51:51
问题 I\'m reading some lecture notes of my C++ lecturer and he wrote the following: Use Indentation // OK Never rely on operator precedence - Always use parentheses // OK Always use a { } block - even for a single line // not OK , why ??? Const object on left side of comparison // OK Use unsigned for variables that are >= 0 // nice trick Set Pointer to NULL after deletion - Double delete protection // not bad The 3rd technique is not clear to me: what would I gain by placing one line in a { ... }