|
Andrei Straut wrote: On the other hand, I'm having fun now with LINQ parsing the list of products from suppliers (didn't get to play with LINQ so far, but I'm quite liking it. Heard it's pretty slow compared to the other alternatives, but in my case speed is not that much of an issue)
One thing that can make LINQ slow is not caching query results appropriately. (Not saying it's not slow to begin with; I really don't know!) For instance:
public IEnumerable<FancyBusinessObject> GetLotsOfData() {...}
public void CrashYourServer()
{
var data = GetLotsOfData();
foreach (var item in data)
{
item.DoFancyBusinessOperation();
}
foreach (var item in data)
{
item.DoOtherFancyBusinessOperation();
foreach (var otherItem in data)
{
ComparisonResults.Add(item.CompareTo(otherItem));
}
}
}
public void PlayNice()
{
var data = GetLotsOfData().ToArray();
foreach (var item in data)
{
item.DoFancyBusinessOperation();
}
foreach (var item in data)
{
item.DoOtherFancyBusinessOperation();
foreach (var otherItem in data)
{
ComparisonResults.Add(item.CompareTo(otherItem));
}
}
}
|
|
|
|
|
I have found several sites that do that, and found that at least one uses (used?) admin/admin as the administrator login. I did alert them to the issue.
Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.
Dave Barry
Read more at BrainyQuote[ ^]
|
|
|
|
|
I swear there was not a single code review done on any part of that 'thing'. If there was, the code reviewer is a complete idiot.
Long story short, was trying to figure how to provide my own 'complex' custom properties in a web part. Easy you say? I thought so too.
My first attempt at it was using a TypeConverter applied on the property (yes a plain ol' C# one), but no, that went no where.
Then I discovered, a TypeConverter will only be instantiated if applied to the type and not the property.
The property being a string, caused me to dish out some silly 'computational object' aka class Category { string Value; } .
But alas, that was fruitless as Sharepoint does not bother calling GetStandardValuesSupported even.
All I want is bloody dropdownlist with a few values!
Then I decided to look at what they invent inside the above-mentioned assembly.
WARNING: Do NOT attempt to do the following without your shrink present and/or a fire truck nearby. People have been known to commit suicide and/or spontaneously combust attempting the following. I take no responsibility after this point.
I opened up Reflector, and yes, they indeed re-invented the wheel and poorly as hell. All the extensibility stuff is marked internal. And hardcoded mostly. So unless I work for Microsoft, tough cookies. (BTW I did the same thing as an exercise many many years back, why this is above their skill level is beyond me. See xacc.propertygrid[^].)
While trying to see what could be done, I came across some bad code, but this one stabbed me in the eye. I will only regain vision in a few weeks the doctor says.
string name;
if ((name = obj.Info) != null && name == "Zone") { ... }
Why on earth could they not just use this:
if (obj.Info == "Zone") { ... }
What self-respecting developer would allow such rubbish to pass a code review or even allowed to be written? Did they think they were optimizing the code? Did they even read the manual?
|
|
|
|
|
What are you on about? Please take a breath and add some clarification. If you need a hug (and you seem to) then by all means go get one, even a liquid one.
|
|
|
|
|
Sometimes the code does not get correctly "reflected" by the Reflector, perhaps it is just a Compiler Optimization or so.
|
|
|
|
|
Kevin Drzycimski wrote: Sometimes the code does not get correctly "reflected" by the Reflector, perhaps it is just a Compiler Optimization or so.
I was actually using ILSpy, which normally does not change the IL to be more readable.
The C# compiler will never re-introduce a local, an assignment to that local, and a subsequent null check. If it had some form of CSE, I would expect the former 2, but the null check is completely redundant. On the other hand, the introduced local is also redundant as the value of name in the branch can on be "Zone".
It could be that this was written in VB.NET.
|
|
|
|
|
..with the amount of code coming from Microsoft, it's a wonder that this does not happen more often. I find it amusing that people peek inside their assemblies and are pissed if they do not find near-perfect code.
We don't expect that from other companies, do we?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
They obviously don't realize an 'axe-murderer' is maintaining/using their code
|
|
|
|
|
Lol, I wrote this a few years ago for a school project... I think I wrote it on Monday at lunchtime.
function randomNumber(){
var lunchtimeMondayReturn=0;
while(true){
var lunchtimeMonday=Math.random();
if(!((lunchtimeMonday+'').split('')[2]>3)){
lunchtimeMondayReturn=(lunchtimeMonday+'').split('')[2];
break;
}
}
return lunchtimeMondayReturn;
}
|
|
|
|
|
In a Pascal class in college I used all nonsense words. QUAMPANI, BELABA, DEFALA, TANEKABI, etc.)
|
|
|
|
|
PIEBALDconsult wrote: TANEKABI
That's somewhat close to Tenkaichi.
|
|
|
|
|
I rather like Vegeta[^], although it's a character name rather than a series
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
Lol. I've done that before... I'm terrible at naming variables.
|
|
|
|
|
If that were Java, you could put it in a class and use some accessors in a really nice way:
isLunchTimeMonday()
getLunchTimeMonday()
Although properties in .NET make a really nice way too:
var whatTimeIsIt = HungryClass.LunchTimeMonday
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
Ano Anony Wrote: Lol, I wrote this a few years ago for a school project...
I hope you are better at naming variables now.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
I know a kid who names his variables x, xx, xxx, xxxx, and so on and so forth. His methods have names like "JDOEHSNFD" (Note all caps). The professor was not very happy when trying to read (and grade) his code. The student also did things like 'String xxx = "879374930";' and then the next line would be 'int xxxx = Integer.parse(xxx).intValue();' The string variable would not be referenced AT ALL after that! Is it any wonder he failed?
Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.
Dave Barry
Read more at BrainyQuote[ ^]
|
|
|
|
|
Here's a little gem I came across a while back.
private bool f(params object[] args) { return 0 != (int)Convert.ToUInt32(args[args.Length - 1]); }
It's employed all over the code module. Here's one place:
byte[][] outBuf = new byte[2][];
outBuf[0] = new byte[256];
outBuf[1] = new byte[256];
for (int i = 0, j = 0; j < outBuf[i].Length || f(j = 0, ++i < 2); j++)
outBuf[i][j] = 0x20;
It would seem that the coder was disapointed that C# wouldn't allow abuse of the comma operator. So he substituted an argument list!
Here's another place where it's used.
public bool Visible
{
get { return f(_RawData[1] & VISIBLE); }
set
{
if(f(_RawData[1] & VISIBLE) != value )
_RawData[1] ^= VISIBLE;
}
}
Well, that explains the name.
Is this clever or a horror?
|
|
|
|
|
From that, it look like a horror.
Did he comment it at all? Or just throw it together and walk away?
'Cause if he didn't, it's probably time he met Mr Ugly Stick...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
That is definately an absolute horror. I hate it when people think they are smart because they write such code. It is totally unmaintainable and unreadable to anyone (including the author of it a couple of weeks after he wrote it!)
|
|
|
|
|
|
It's definitely a horror. It's unclear, poorly named and results in ugly calling code. In addition, the cast and call to a Convert function will make it slow.
|
|
|
|
|
What do you mean by "C# wouldn't allow abuse of the comma operator"? C# doesn't have a comma operator.
A quick search pulled up this little gem (I wonder how it got copied from the MSDN foum ): http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/93126/C-for-loop-and-comma-operator[^]
Yes, that's a horror -- the method should have a better name and it could even be made generic!
public T
LastOf
(
params object[] list
)
{
return ( (T) list [ list.Count - 1 ] ) ;
}
It's still silly, but this just points up how easy it is to implement a way to get around the limitation. Come to think of it, couldn't you do it with Linq? I don't know Linq, but something like: (new int[] { a++ , b++ }).Last ?
P.S. Here's a little test of the concept:
int i = 0 ;
int j = 0 ;
while ( (new int[] { i++ , j += 2 }).Last() < 10 )
{
System.Console.WriteLine ( "{0} {1}" , i , j ) ;
}
for ( i = 0 , j = 0 ; (new int[] { i++ , j += 2 }).Last() < 10 ; )
{
System.Console.WriteLine ( "{0} {1}" , i , j ) ;
}
modified 7-Aug-12 18:09pm.
|
|
|
|
|
Apparently no, isnt't it? But run this code:
class Program {
static void Main(string[] args) {
object o = null;
Console.WriteLine(o.IsNull());
}
}
public static class ExtensionMethods {
public static bool IsNull(this object obj) {
return (obj == null);
}
}
If you expected it to throw a NullReferenceException (as I did), then you're in for a surprise. It actually prints true. Makes me wonder how extension methods are implemented internally. Something like a wrapper object over the null object? Any .NET experts here that can explain?
|
|
|
|
|
Extension methods are implemented as you would expect: as a call to a static method, with a parameter.
So effectively, what you have written is:
public static bool IsNull(object o)
{
return (o == null);
}
...
if (IsNull(null))
{
... Which nobody would expect to throw a null reference.
The extension method bit is just syntactic sugar.
Looks odd though!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Yes, I understand. But what intrigues me is the
o.IsNull() syntax, the mental picture I get is that since o is null, any attempt to call a method/field would throw a NullReferenceException, replace IsNull() with ToString() and you'll know what I'm talking about.
object.IsNull(o) would have made much sense.
|
|
|
|