|
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
It took this long to get this simple return?
|
|
|
|
|
You're not following good form. You should use CONSTANTS for things that like.
return someBooleanVariable ? Boolean.Parse(Boolean.TrueString) : Boolean.Parse(Boolean.FalseString);
|
|
|
|
|
You mean
return someBooleanVariable.ToString() == Boolean.TrueString? Boolean.Parse(Boolean.TrueString) : Boolean.Parse(Boolean.FalseString);
Right?
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Good call.
|
|
|
|
|
Chris Maunder wrote: You mean they missed the totally obvious contraction to
return someBooleanVariable ? true : false
They should hang their head in shame!
No! Wrong!
return (someBooleanVariable == true)
|
|
|
|
|
Not good enough:
if (((Boolean)someBooleanVariable).equals(true)) {
return someBooleanVariable == true ? someBooleanVariable : false;
} else {
return someBooleanVariable == false ? someBooleanVariable : true;
}
it's called the Irish Method - to be sure, to be sure, to be sure.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
G James wrote: Later they refactored it to this thinking it was an improvement.
return someBooleanVariable == true ? true : false;
That's pretty poor refactoring, it should have been:
interface IBooleanConverter {
bool BooleanToBoolean(bool value);
}
class BooleanConverter : IBooleanConverter {
public BooleanToBoolean(bool value) {
return value == true ? true : false;
}
}
interface IBooleanConverterFactory {
IBooleanConverter CreateBooleanConverterFactory();
}
class ConfigurationBooleanConverterFactory : IBooleanConverter {
public IBooleanConverter CreateBooleanConverter() {
return (IBooleanConverter)
Activator.CreateInstance(ConfigurationManager.AppSettings["Types.BooleanConverter"]);
}
}
static class BooleanConverterFactoryFactory {
public IBooleanConverterFactory CreateBooleanConverterFactory() {
return (IBooleanConverterFactory)
Activator.CreateInstance(ConfigurationManager.AppSettings["Types.BooleanConverterFactory"]);
}
}
return BooleanConverterFactoryFactory.CreateBooleanConverterFactory().CreateBooleanConverter().BooleanToBoolean(someBooleanVariable == true ? true : false);
It is clear that this is more maintainable. There is currently this method in our code-base because some [one] of our developers is so bad that we need to hold his hand through everything:
public static T As<T>(this object obj) {
return obj as T;
}
Yes guys, an extension method that replicates a keyword.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
my eyes bleed and my head hurts
it's so dificult to believe i'm seeing so well structured sh*t-code
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Sentenryu wrote: it's so dificult to believe i'm seeing so well structured sh*t-code
And a lesson has been learnt about GOF design patterns .
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
I hope you are not one of those gang of 4 guys
Ranjan.D
|
|
|
|
|
Ah, now I see what people mean by maintainable code - code that will require maintenance, and hence pay the bills for years to come. Genius.
|
|
|
|
|
This could make a good business sense when some decisions for different clients have not been made. Make the code structure ready and wait for the changes. I recently worked on a project in which different user role was supposed to have a different permission. But the decision was not made. Therefore all roles were assigned the same permission during development. Sometimes, the decision is never made. Several years later, when you look at the code, you would question why you did that.
TOMZ_KV
|
|
|
|
|
After my well deserved vacation I returned to work.
Beeing happy I received a task to modify a DotNetNuke module, made by some externals from a country far far away.
Happyness didn't last long when I came along a SearchEngine class:
public const string SELECT = "SELECT";
public const string DISTINCT = "DISTINCT";
public const string FROM = "FROM";
public const string WHERE = "WHERE";
public const string HAVING = "HAVING";
public const string AND = "AND";
public const string OR = "OR";
public const string IN = "IN";
public const string ON = "ON";
public const string AS = "AS";
public const string GROUP_BY = "GROUP BY";
public const string ORDER_BY = "ORDER BY";
public const string DESC = "DESC";
public const string ASC = "ASC";
this continues for about 1500 lines of code declaring every possible table/column etc I could think of.
90% of the consts aren't even in use... and it all ends up in statements like that:
sqlString.AppendFormat("{0} {1} {2}.{3},{2}.{4},{2}.{5},{2}.{6},{2}.{7},{2}.{8},{2}.{9},{2}.{10},{2}.{11},{12} {13} {14} {15} {2} {16} {17} = @{17}",new string[]
{
SqlConsts.SELECT,
SqlConsts.DISTINCT,
SqlConsts.TBL1,
SqlConsts.OBJECT_NO,
SqlConsts.OBJECT_ID,
SqlConsts.COUNTRY,
SqlConsts.REGION,
SqlConsts.SUBREGION,
SqlConsts.TOURAREA,
SqlConsts.LATITUDE,
SqlConsts.LONGITUDE,
SqlConsts.CITY,
SqlConsts.COUNT_ANY,
SqlConsts.AS,
SqlConsts.UNIT_COUNT,
SqlConsts.FROM,
SqlConsts.WHERE,
SqlConsts.YEAR
});
...am I missing something or is this an acceptable way of solving database related tasks?
Can you spot a reason for this? If so, please make my happiness return... somehow... please...
Happy new year
Andy
|
|
|
|
|
Great Zarquon! Thermite the disk immediately before that corrupts something else. Not even I would do that*.
There is absolutely nothing good about that and all sorts of bad. The worst thing I can think of now (no coffee yet) is that you can't copy and paste it to SSMS (or similar) to test it. Just below that would be lack of syntax colouring if available.
You just ruined my day. Thanks for sharing.
* If I did, I'd use an enumeration.
|
|
|
|
|
I like the "thermiting" idea of you...
And this is just a small snippet of the project... it's like a "Coding Horror" blog that came to live...
As a grown up man I am not ashamed of my tears
|
|
|
|
|
Good heavens. Shoot the purp!
|
|
|
|
|
Oh my god.
What did I just read about "my eyes are bleeding"?
"some externals from a country far far away" ... indeed.
And to think they were all public constants
|
|
|
|
|
Were they getting paid by the line of code?!
This is what happens when you remove the "adult supervision" from a bunch of idiots writing code.
|
|
|
|
|
My best guess is misuse of intellisense.
Maybe the perpetrator (amongst other) is dyslexic and has invented his own spellchecker.
People say nothing is impossible, but I do nothing every day.
|
|
|
|
|
Yes, this is the first normal form of the so called "Bakers Binding". It's very robust and flexible,you know! provide excellent abstraction without too much complexity!
In the second normal form, you introduce a
public const string COLUMNSEPARATORFORSELECTSTATEMENT = ",";
To be fair, hunting down select statements when the db scheme changes is painful; you have to exercise every code paht that contains a select.
That solution is akin to smashing your toe to forget a flea bite, though.
|
|
|
|
|
...good one though
there actually exists such a seperator iuhasdfhu,gvsd.fgzdfjhb (sorry, I just had to smash my head into the keyboard... it reliefs the pain)
|
|
|
|
|
An attempt to create new, better LINQ to SQL?
|
|
|
|
|
"Internal Electromagnetic Shockwave Generator Stablizer Error"
Not sure if I should run or not.
(Don't have a screenshot, sorry. Forgot to get one)
This popped up when an error occurred while starting the debugger (it was a stack overflow).
On a similar note, I hit an error yesterday that had me going around in circles for several hours.
I have a service manager class that contains a method GetService. I overrode a property from the base class to provide a logging service implementation, and left the message service property alone.
Well, that message service property called a method, which called the GetService method, which returned the message service property...... I think you can figure out where this one is going.
The problem that caused the strange error message above is a different thing, caused by a goof I made while using CassiniDev.
Baboom.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
<context>SQL Server 2008 R2</context>
Someone added a function to my database! Well OK, it's a team effort so that's fine, but it's incorrect and not well-written.
What I think the purpose is is to take a column name in PascalCase and make it look more presentable by adding a SPACE before each capital (uppercase) letter. That's what I understand from the name and it's a reasonable thing to do and it does that.
Buuut... what it really does is add a SPACE before each non-lowercase letter (e.g. digits, symbols, whitespace).
Aside from producing unexpected output it's also hard to read and difficult to maintain -- I opted to rewrite it rather than try to fix it. Here's the original:
CREATE FUNCTION [dbo].[SpaceBeforeCap]
(
@str nvarchar(max)
)
returns nvarchar(max)
as
begin
declare @i int, @j int
declare @returnval nvarchar(max)
set @returnval = ''
select @i = 1, @j = len(@str)
declare @w nvarchar(max)
while @i <= @j
begin
if substring(@str,@i,1) = UPPER(substring(@str,@i,1)) collate Latin1_General_CS_AS
begin
if @w is not null
set @returnval = @returnval + ' ' + @w
set @w = substring(@str,@i,1)
end
else
set @w = @w + substring(@str,@i,1)
set @i = @i + 1
end
if @w is not null
set @returnval = @returnval + ' ' + @w
return ltrim(@returnval)
end
And here's mine (I added the fn wart because that's the standard I inherited ):
CREATE FUNCTION [dbo].[fnSpaceBeforeCap]
( @str NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @offset INTEGER
SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )
WHILE ( @offset > 0 )
BEGIN
SET @str = STUFF ( @str , @offset + 1 , 0 , ' ' )
SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )
END
RETURN @str
END
It uses PATINDEX to find a non-SPACE followed by an uppercase letter then STUFFs a SPACE between them.
I'd like to say that mine is more efficient, but I don't feel like doing any testing. What I will point out is that the original uses SUBSTRING (twice!) to test each character and SUBSTRING to form the new value, whereas mine has that kind of thing hidden in black boxes. What I don't like about mine is that PATINDEX doesn't allow a parameter to tell it where to start so it always starts over from the beginning -- see also Schlemiel the painter's Algorithm[^].
|
|
|
|