|
I think a coder is stringing you along.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
If the codebase originated from a dotNet1.1 codebase, then the statement makes complete sense because string.IsNullOrEmpty did not exist at that time.
Something that would definitely get updated in a code review: yes,
Horror: close, but not quite.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
+1 plus they wouldn't know generics, thus the need to cast to String.
'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood
'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail
|
|
|
|
|
Refactoring Fail.
To match the original functionality what you need is String.IsNullOrWhiteSpace() . String.IsNullOrEmpty(lastItem.Value) will return false if lastItem.Value equals " ". String.IsNullOrEmpty(lastItem.Value.Trim()) will exception if lastItem.Value is null.
My other question would be how old the codebase is? IsNullOrEmpty() wasn't added until .net 2.0; IsNullOrWhiteSpace() wasn't added until 4.0.
I wrote an IsNullOrEmpty() method when I needed one in a 1.1 app; but unless you're doing it in multiple locations there's nothing wrong with inlining.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
1. A wtf? There is no difference according to MSDN[^]
I actually prefer the "" instead of String.Empty, but that's personal taste.
I agree to two and three though, but not everyone knows the IsNullOrEmpty method.
V.
|
|
|
|
|
In .NET 4, there is IsNullOrWhiteSpace[^] as well, which is even more useful!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Did you scroll down to the end?
VB Programmers: Null means Nothing in this method name
If it means nothing, then why did they put it into the name?
I'm invincible, I can't be vinced
|
|
|
|
|
Yep! And I have no problem with that at all...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Shouldn't you have said "And I see Nothing wrong with that at all..."?
|
|
|
|
|
Just think about the poor SQL programmer where the standard setting is that "value != null" is always false and as a comparison, "value = null" is also always false.
|
|
|
|
|
I was told that string.Empty doesn't create a whole bunch of new string instances. I will keep that quote from the MSDN guy to hand for next time I'm told using "" is wrong. (I prefer it too because two characters is almost always better then 11 and it's immediately obvious what "" is without actually having to read a word.)
|
|
|
|
|
Yes, I know there's no difference and that it's a matter of personal taste, so the worst that can happen is that it might mildly annoy a maintenance programmer that expected a different coding style. That said, I probably should have mentioned earlier that it happens to be in a codebase in which things like this is discouraged. In fact, string literals in general are discouraged: there should be a separate class full of constants. This means that the code above sticks out like a sore thumb around here.
|
|
|
|
|
Sometimes you just have to make absolutely sure it is a string. And since it is OO programming they could have done this:
lastItem.Value.ToString().ToString().ToString().Trim()
just to make really, really sure.
|
|
|
|
|
I like that! But you know, sometimes you want to be a little more sure than other times. Therefore, I propose the that following method be added:
public static string ConvertToString(object objectToConvert, ulong howSureDoYouWantToBeThatItIsString)
{
string temp = objectToConvert.ToString();
for(ulong n = 0; n < howSureDoYouWantToBeThatItIsString; n++)
{
temp = temp.ToString();
}
return temp;
}
Now ain't that a piece of beauty!
|
|
|
|
|
I like that! I wanted to see how many times that is possible to be looped: 18,446,744,073,709,551,615. That's a few loops.
I figure, on my 2GH machine, that's approximately 272.19712525667353 years. (probably double that because 1 flop per loop and 1 flop per string conversion)
double years = (double)((ulong.MaxValue / int.MaxValue) / (3600 * 24)) / 365.25;
flop = "FLoating point OPeration"
Is it 1 or 2000 flops per string conversion for a 2000 character string?
PS Yes, I'm having fun with people without an understanding of precision too.
|
|
|
|
|
I didn't know about IsNullOrEmpty, and apparently we both didn't know about IsNullOrWhiteSpace which removes the need for Trim.
|
|
|
|
|
Maybe he was thinking of the 'String' theory!
Yeah, I know, it's a groaner.
Attempting to load signature...
A NullSignatureException was unhandled.
Message: "No signature exists"
|
|
|
|
|
Hi guys
im new here but i tough i would share this example of code i encountered a couple months back
in one class
Select field1,field2,field3
From table1
Where type="EDU"
the only fucntion using this result set was coded like this
If(field1=cond1 and field2=con4 and Active=true)
do something
else If(field1=cond1 and field2=con2 and Active=true)
do something
else If(field3=cond10 and field2=con2 and Active=true)
do something else
this went on for like 9 time
now someone added another If lower down in the code but forgot the Active=true hense causing a bug ! now after searching to make sure that the Selqct wasent used anywhere else(the name was a dead give away but you never know)
First off
Select field1,field2,field3
From table1
Where type="EDU" AND Active=True
PLEASE add this AND ... just reducing the result set will bost this application speed has its database was Huge
as for the rest we where not allowed to modify working code but come on 9 inbricked iff with some repeating coditions ....
modified 6-Feb-12 21:04pm.
|
|
|
|
|
Is it really necessary to select the same field THREE times?
|
|
|
|
|
Lol yeah sorry for the type O
i edited the post
Cheers
|
|
|
|
|
|
Jtai wrote:
Select field1,field2,field3
From table1
Where type="EDU"
When I read the code, I thought it was a pity that Active wasn't in the DB so you could restrict the number of lines based on Active. Then I thought, don't even run the logic if Active isn't true. As I continued reading, I realized the real condition. Did you drop the Active check in the code along with modifying the select?
If it's still a large # of rows being read, you might want to include your condition checks in the selection criteria. Also, hopefully, you are really running a sproc to get your data.
Here's an example of using 4 condition checks with very different selection criteria. If two condition checks overlap then the data produced would overlap. The table had 5K rows in it, the selection had 39 rows in it.
declare @tbl table (Cond int, sTotID int, stot1 int, stot2 int, stot3 int)
insert into @tbl
values (1,-1, 20, -1, 49), (2, 203148, -1, -1, -1), (3, -1, 15, 29, -1), (4, -1, 25, 32, -1)
SELECT TOP 1000 Cond, TotID
,NumbRecs
,tot1
,tot2
,tot3
FROM dbo.ThreeNumberSum
JOIN @tbl on (sTotID = -1 or sTotID = TotID) AND (stot1 = -1 OR stot1 = tot1)
AND (stot2 = -1 OR stot2 = tot2) AND (stot3 = -1 OR stot3 = tot3)
Your code could just check 1 value to match all the multiple conditions. Here is an extract of all the rows:
Cond TotID NumbRecs tot1 tot2 tot3
1 202749 32 20 27 49
... middle number 28-30
1 203149 208 20 31 49
1 203249 184 20 32 49
1 203349 128 20 33 49
1 203449 56 20 34 49
1 203549 8 20 35 49
1 203649 8 20 36 49
2 203148 424 20 31 48
3 152940 24 15 29 40
3 152941 40 15 29 41
3 152942 32 15 29 42
3 152943 48 15 29 43
3 152944 8 15 29 44
3 152945 8 15 29 45
4 253228 28956 25 32 28
4 253229 49698 25 32 29
... last number 30-35
4 253236 595694 25 32 36
4 253237 529858 25 32 37
... last number 38-48
4 253249 8 25 32 49
|
|
|
|
|
tecgoblin wrote: It's surprising how many people try to create their own solutions to problems which already have established and well-behaving solution :S.
In the spirit of this comment I wanted to introduce a new standard - the DAO Framework. Problem: "Some day you may wish to change databases." Solution: "Create a generic database access framework to ease migration."
Example:
Firstly, define a DBConst file as follows:
public class DBConst {
public static String SELECT = "SELECT";
public static String WHERE = "WHERE";
public static String AND = "AND"
etc
}
Then create an interface: SQLFactory as follows:
public interface SQLFactory {
public String createSQL(String[] selectors, String[] whereClauses);
}
If I remember correctly, now all you have to do is (Not in the original this was a class, I invented using interfaces - yay me):
public interface MyTable {
public static String TABLE_NAME = "MyTable";
public static String COL_1 = "Col1";
public static String COL_2 = "Col2";
}
Okay, we're nearly ready to write some code:
public class MyTableDao {
public MyObject selectObject(String selector) {
String[] columns = {MyTable.COL_1, MyTable.COL_2);
Object[] types = {DbTypes.DATE, DbTypes.INTEGER};
String[] whereClause = {MyTable.COL_1};
Object[] whereValues = {selector};
MySqlFactory sqlFactory = new MySqlFactory();
String sql = sqlFactory.createSql(columns, whereClause);
PreparedStatement ps = connect.prepareStatement(sql)
MySqlInjector.injectValues(ps, new Object[] {selector});
ResultSet rs = stmt.executeQuery(query);
MyObject[] objs = MyObjectFactory.getObjects(rs, columns, types);
}
Yeah, I think that was it. I'm writing from memory from 5 years ago so forgive if errors.
Key standards:
* List the columns to retrieve
sb.append(DbConsts.SELECT).append(" ").append(COL).append(,) ...
* List the types of the columns so an object factory can retrieve the right type
if(type[0]==DbTypes.INTEGER) rs.getInt();
* List the WHERE clauses
sb.append(DbConsts.WHERE).append(" ").append(clauses[0]...
I forgot about the static object factories until now. I suspect the SQL factories were static rather than implementations of an interface. It was more by program by convention.
The rest of the implementation is left as an exercise for the reader.
Can we stop all this nonsense with Hibernate now please?
|
|
|
|
|
|
Absolutely not. No joke. Can't remember/was never sure whether people accepted it because of the designer's standing in the company or because they didn't realise.
|
|
|
|
|