|
I tell people and tell people and tell people to use parens and not assume people know operator precedence.
People tell me I'm stupid. I choose to believe they mean for saying use parens.
Walk through it and change the values in the debugger and make a state table.
I also go with the XOR interpretation.
Opacity, the new Transparency.
|
|
|
|
|
We have a webpage that contains a table with collapsible sections. Straightforwardly, the code behind the show/hide links took the approach of (1) searching for the tr elements that need to be toggled, and (2) changing the .style.display property to "" or "none" as appropriate.
Less straightforwardly, step #1 was implemented not with DOM methods, but by searching the innerHTML for a string starting with "<TR ID=DISPFILE_". In uppercase, so it wouldn't work on Firefox which normalizes the tag names to lowercase.
At least the original programmer was consistent: Step #2 was implement by a similar text search for "STYLE='display: none'".
|
|
|
|
|
Firstly, -10 points for using nulls on a green field development in my book.
Then I found these fields at the top of the class:
internal class BarHelper : FooHelper
{
decimal? _decimalNull = null;
short? _shortNull = null;
int? _intNull = null;
bool? _boolNull = null;
DateTime? _dateTimeNull = null;
}
What are these for I wondered? A find reference on _decimalNull yeilds:
_bazTableAdapter.InsertRow(
cashbackAmount != null ? cashbackAmount.ValueAsDecimal : _decimalNull,
cashbackAmount != null ? cashbackAmount.CurrencyAsShort : _shortNull,
billAmount != null ? billAmount.ValueAsDecimal : _decimalNull,
billAmount != null ? billAmount.CurrencyAsShort : _shortNull,
billAmount != null ? billAmount.ConversionRateAsDecimal : _decimalNull);
It's all just wrong (including the DataSets and Table adapters) and just makes me want to weep. Even just using cashbackAmount != null ? cashbackAmount.ValueAsDecimal : new Nullable<decimal>() would have been an improvement. On the plus side, as I'm being repeatedly told, we don't have time to do it properly.
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Awesome code. Can I use it in my application or is it licensed?
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
It'll cost you 20 IQ points per line, or 50 to put in a commercial product
|
|
|
|
|
You could write a generic helper class for that.
static class TypedNull
{
public static T GetNull<T>()
{
return null as T;
}
}
billAmount != null ? billAmount.CurrencyAsShort : TypedNull.GetNull<short?>()
10 PRINT Software is hard. - D Knuth
20 GOTO 10
modified on Wednesday, December 16, 2009 4:49 PM
|
|
|
|
|
not quite their code! How about:
public static decimal? AsDecimal(this Foo f)
{
if (f == null)
return null;
return f.DecimalValue;
}
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
you forgot the redundant else
|
|
|
|
|
Nullable types have their uses; this should not be one of them!
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
you're missing the point,
the post is not about Nullable types but Nullable people
|
|
|
|
|
public enum Category
{
MinorProduct = 1,
MajorProduct = 2,
NonStockProduct = 3
}
public void ProcessStock(StockItem item)
{
if (item.Category == MinorProduct &&
item.Category == MajorProduct &&
item.Category == NonStockProduct)
{
Throw new Exception("Invalid Stock Category");
}
}
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
I trust == is a typo for != . Yours or theirs?
..Peter
|
|
|
|
|
I think that's the horror actually :p
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
That's not an horror, just a typical 6pm inattention error!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
That's not an horror, just a typical 6pm inattention error!
That shoud be scary - I mean, to work for a company, where at 6 p.m. enum values can jump out of enum range.
They should also add
private void method(bool b)
{
if (currenttime>=6pm)
{
if (b!=true && b!=false)
{
throw new After6pmException();
}
}
}
|
|
|
|
|
Management should learn not to overwork their staff!!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
modified on Wednesday, December 16, 2009 5:57 PM
|
|
|
|
|
Joke aside there plenty of case when an Emun value can be outside the normal value of the Enum.
From the top of my head here are 3 valid example
- when use as Flag / bit wise value
- when the value is cast from a random int
- when the value comes from serialization of a different version
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Thank you. For some reason I was sure C# cares at least a little bit what do you assign to enum variable. You are right, it does not. Good thing is - I learned something new.
|
|
|
|
|
Member 357537 wrote: Thank you. For some reason I was sure C# cares at least a little bit what do you assign to enum variable. You are right, it does not. Good thing is - I learned something new.
Me too. I had always assumed that it would check the value being assigned to an enum variable. In fact, it does not and any old int will do.
I come from a Java background and I just took it for granted that C# would do the same sort of check as Java. In Java, it checks to make sure that you can only use one of the defined enum values, and anything else gives an error (enums in Java are not integers, they are a type in their own right so you cannot simply cast an integer to an enum).
I have to say I'm shocked. I'm sure someone will come up with a reason why you can use any old random integer value in C# but it seems a bit sloppy to me. After all, part of the reason for using enums is to constrain the range of values.
|
|
|
|
|
It's easy, the FLAGS !
If your enum is
enum MyEnum{
flag1 = 1,
flag2 = 2,
flag3 = 4,
flag4 = 8,
}
And if you set everything ON (MyEnum = 15), the value is valid but not in the range.
I think thats the reason why
Merry Christmas !
|
|
|
|
|
Yes and if the enum values are all disjoint - like they are for a set of flags or possibly for a field in a binary structure - then the code to validate the value would essentially have to do a search every time an enum is assigned. That's a tax I would not want to have to pay just because other developers can't be trusted. (As if I were perfect...)
And the enum-as-flag idiom comes from C++ where the only way to declare a const int value in a class scope was to use an enum.
Bonus info: The compiler I use (Microsoft C++) lets you declare the size of the integer underlying the enum. So for case where I need more than 32 flags, I can write:
enum MyBigEnum : __int64 {
MBE_Unknown = 0,
MBE_QualityFlag = 0x0000000001,
MBE_EnergyFlag = 0x0000000002,
MBE_MyLastFlag = 0x4000000000,
};
-- T
|
|
|
|
|
Additional bonus info: The underlying size of an enum can be declared in C# (and, presumably, in the other .NET languages as well) in the same way the Microsoft compiler allows enums to have a defined size in C++. So an enum value isn't always an int.
|
|
|
|
|
David Skelly wrote: part of the reason for using enums is to constrain the range of values
It is indeed. But I think C# strikes the right balance; another reason for using enums is that it provides a more readable alternative to hard-coded ints and a more efficient alternative to string literals.
C# does not allow implicit casts to enum values, precisely because it is unsafe. There is no run-time check when explicitly casting because it would introduce significant overhead.
I find it rather too strict if Java as you claim does not allow casting to enum types from integer types. Imagine you have a class with many fields that each have a small predefined set of legal values. We can define enums to represent these sets, use byte as the base value, and store such data in a database using one byte per attribute. Storing the names is extremely inefficient: Each column has to be sized according to the maximum length permitted in the set of values. You'd easily go to 20 characters in many cases, and sometimes more, which depending on encoding will require 20 to 40 bytes of storage space. If you have five such fields, you could end up using 200 bytes instead of 5. And large row size == bad performance. If you can't cast to enum values, you'd have to write switches instead (switch (value) { case 1: return CreditCard.MasterCard; break; ...}) which is pretty boring work!
|
|
|
|
|
Reflector showed me this 'gem' in a contractors project:
string cBRK = Convert.ToChar(int.Parse("13")).ToString() + Convert.ToChar(int.Parse("10")).ToString();
this.ReportStatus("93101Bytes" + cBRK + BytesRecieved.ToString() + cBRK + fileSize.ToString() + cBRK);
kind regards,
JoFli
|
|
|
|
|
I bet, the creator of this is proud of his clever and 'creative' solution...
blog.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|