|
Dang! Beat me to it.
Marking it as "news" is pretty hilarious.
|
|
|
|
|
|
Dear VUnreal,
Please find below a simplified code segment that may help you in achieving your code in one line using a helper class.
please feel free to add additional checks and safeguards, and rewrite the helper as an extension to the string class.
string valueStr = value.ToString();
TruthChecker tc=new TruthChecker();
PDFIncludeWatermark_ = tc.CheckIfTrue();
class TruthChecker
{
private static List<string> _truths = null;
List<string> Truths { get { return _truths!=null ? _truths : (_truths = FindAllTruths() ); } }
TruthChecker() {
}
private static List<string> FindAllTruths() {
List<string> allTruths = new List<string>();
char[] t = {'t','r','u','e'};
for(int i=0;i<Math.Pow(2,t.Length); i++) {
StringBuilder sb = new StringBuilder();
for(int j=0; j<t.Length; j++) {
int k = (i>>j)&1;
sb.Append(t[j]+('A'-'a')*k);
}
allTruths.Add(sb.ToString());
}
return allTruths;
}
public string CheckIfTrue(string tru)
{
try {
var v =
from t in Truths
where t == tru
select t;
if (v.ElementAt(0).ToString().Length == "true".Length) {
return "true";
}
} catch (Exception ex) {
tru = ex.Message;
}
return "false";
}
}
Please visit IOCCC.org to learn more about useful coding techniques.
|
|
|
|
|
Why do so many think that try - catch blocks are just here to stop you from seeing runtime errors?
It is a plain nightmare to debug a project which has code like this sprinkled all over the place.
(Note: I removed some confidental information from the code)
try
{
if (SomeConstant != "")
{
this.comboBox.SelectedItem = this.comboBox.Items[this.comboBox.Items.IndexOf(SomeConstant)];
}
}
catch { }
Or one of my favourites:
protected override bool Exists()
{
bool returnValue = false;
try
{
product = GetProductOverWebservice()
if (product != null && product.product_id == m_RecordID.ToString())
{
_product = productinfo;
returnValue = true;
}
}
catch (Exception ex)
{
}
return returnValue;
}
|
|
|
|
|
I agree with your two examples... but there have been a couple of times where I have done the same thing because I don't want an exception.
perhaps a file access on start up that is not important if it fails, I don't want to inform the user and I certainly don't want to crash the application. it is only on rare occasions thou
Yes, I know I could log the error but maybe it really is that unimportant, plus perhaps my error logging process would have an empty catch if it fails to write to a log file (after all, can't log if the logging is failing)
This will do for now
|
|
|
|
|
|
That really is bad code. Should be...
try{
} catch { }
This will do for now
|
|
|
|
|
An idea for "oE" might be: "obscure Exception". The lowercase "o" is to keep it as obscure as possible.
|
|
|
|
|
More likely, "obvious Exception", since If the form does not start you'll notice soon enough.
Obviously.
|
|
|
|
|
The Dutch (yes, I'm from the Netherlands) word for 'unknown' is 'Onbekend' and the Dutch word for 'unexpected' is 'Onverwacht'... So maybe I should have translated oE to uE?
I love all the guesses coming in though, keep them coming!
It's an OO world.
|
|
|
|
|
Naerling wrote: Don't really know what oE is
That's obvious Enough , isn't it?
|
|
|
|
|
That's excellent.
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
Naerling wrote: Don't really know what oE is
I guess it means "objectException", in an extra nasty form of hungarian notation.
|
|
|
|
|
Regards,
Koushik.
Most people never run far enough on their first wind to find out if they've got a second. Give your dreams all you've got and you'll be amazed at the energy that comes out of you.
|
|
|
|
|
Awesome.
Regards,
Koushik.
Most people never run far enough on their first wind to find out if they've got a second. Give your dreams all you've got and you'll be amazed at the energy that comes out of you.
|
|
|
|
|
It once took me 2 days! to track down a bug because someone had decided to swallowed the exception catch { } . The problem is some programmers just don't get it.
The best article I've ever read regarding exception handling is Exception Handling Best Practices and it's on CodeProject.
"You get that on the big jobs."
|
|
|
|
|
|
Hi Naerling,
The problem is you may not be aware that it is an exception that is the problem. In this case it smelt like business logic because no exceptions where being thrown or logged.
The Exception window is a great tool. It can be found in the menu bar under Debug, Exceptions...
"You get that on the big jobs."
|
|
|
|
|
Actually, the catch that said 'Do nothing, if the form does not start you will know soon enough' (see a previous post) did nothing because some forms were started and disposed immediatly, or gave the user the option to end it while it was still loading. This, of course, gave an exception. But because it was meant to behave like that the programmer did not want to do anything with the exception... So when I fixed it by showing a messagebox and logging the exception I actually got told to undo my changes because 'the business logic was designed that way'! How about that for business logic?
It's an OO world.
|
|
|
|
|
RobCroll wrote: The problem is some programmers just don't get it.
Unfortunately, it's usually the customers who don't get it.
Our code is full of this "non-specific" error handling - not because we are bad programmers, but because customers don't want to see error messages (we've had complaints, such as "It's telling me that the socket has refused the connection! I don't want to see it!"). To me, that is stupid - I'd rather see a problem instead of sitting around wondering why something isn't working. On the other hand, we work a lot with real-time data, so customers don't want it constantly stopped by dialog boxes intimating that their system is badly set up (as it usually is)!
At least all our exceptions are logged - a small scrap of comfort for us.
|
|
|
|
|
Some of the exceptions I get I don't understand! So what chance does a user have. They just don't understand the message.
I like to encapsulate the error message in the business layer and throw a more user friendly error message to the presentation layer “Could not save customer information because computer networks are down.”. Users understand that. They have no idea of what a TCP/IP socket is but they understand that networks aren't always working.
That's the problem. It's not that there is a problem but the the message is meaningless. They don't understand the message.
So whether you are implementing MVC or MVVM or whatever, at some stage you need to consider the message the user gets.
Once they understand the problem, it's all cool... “see your Systems Administrator”. Now who the hell is that?
BTW that small scrap would be a life saver.
"You get that on the big jobs."
|
|
|
|
|
I guess that's why they invented the option in the debugger to break on any exception being thrown.
There is no failure only feedback
|
|
|
|
|
Few days ago I just reviewing some codes on a project and I found that there are too may commented unnecessary codes in each file.I am giving you all an example:
public virtual bool TraverseRecord(string TraverseFor, string Id)
{
bool HasRows = false;
string sql = "";
sql = "SP_NAVIGATE_CHILD_T '" + TraverseFor + "', '" + Id + "'";
iMHUtility.ExecuteStoredProcedure(sql);
if (iMHUtility.IsErrorOnExecution() == false)
{
if (iMHUtility.HasMoreRecords())
{
HasRows = true;
ValueInitialize();
}
}
return HasRows;
}
Look at the TraverseRecord, the first one is definitely useless, so why some programmers leave those useless lines as comment. I think this is disgusting.
|
|
|
|
|
I've seen similar stuff in places that for some unknown reason didn't use source control, but rather comments as a versioning mechanism. (No, that place is no longer in business... Surprise) The OP probably just copied and pasted it with the intention of modifying the code, but in this case I think he forgot to perform the latter.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Thats why code source control and review is important.
|
|
|
|