Cleverest Code of the Day!
-
Remimds me of the code generated by a compiler I once knew: This was in the days of the superminis, very much CISC. This machine (called ND-500) had a "LoadIndex [register] [indexvalue] [min] [max]" instruction: If the value at [indexvalue] was not between [min] and [max], a exception was generated. This compiler could (optionally) verify all array indexing, using this instruction, but not quite in the straightforward way that you might think. A disassembly showed something like this LOAD R4, indexvalue COMP R4, min JUMPLT error COMP R4, max JUMPGT error % everything is OK, go ahead ... error: LOADINDEX R4, R4, 0, -1 % no legal range, will unconditionally generate exception Generating six instructions to replace a single one can be meaningful, but hardly when one of the six is the instruction you want to replace... I was working in the company making both the CPU and the compiler, so I went to the compiler guy for an explanation. He insisted that since index check was an option, it should not change any code generated, only add code: The index check is like a debugging aid that you might turn off in a production build, and the production version should be exactly the code you debugged, minus the debug features. With the option off, the first instruciton (LOAD R4, indexvalue) was generated. Turn it on, and you get the five additional instructions. Needless to say: Even though his argument sort of sounds plausible, I strongly disagreed with him. First, index checking is not a debug feature; you keep it on in the production version. Second: Given an option set, the compiler should be free to the best code for that selection of options, ignoring other option sets (think of optimization options!) After much arguing, he agreed to rather generate LOAD R4, indexvalue LOADINDEX R4, R4, min, max ... two instructions is better than six. But he stuck to his principles: I never got him to generate a singel LOADINDEX.
that's why i wouldn't get in discussion with senior dev on this :laugh:
-
A consultant friend of mine who has been programming far longer than I have—he started programming on the early Macs and helped write the first music notation software for the Mac—doesn't see why C# should need to use .SubString() to get the left/right most characters from a string and instead wrote these. I don't really have a problem with this sort of thing. I just don't see the point, but that's just me.
public string LeftStringFunction(string sValue, int iMaxLength) { //Check if the value is valid if (string.IsNullOrEmpty(sValue)) { //Set valid empty string as string could be null sValue = string.Empty; } else if (sValue.Length > iMaxLength) { //Make the string no longer than the max length sValue = sValue.Substring(0, iMaxLength); } //Return the string return sValue; } public string RightStringFunction(string sValue, int iMaxLength) { //Check if the value is valid if (string.IsNullOrEmpty(sValue)) { //Set valid empty string as string could be null sValue = string.Empty; } else if (sValue.Length > iMaxLength) { //Make the string no longer than the max length sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength); } //Return the string return sValue; }
"...JavaScript could teach Dyson how to suck." -- Nagy Vilmos
-
Looks like somebody's trying to reinvent VB6's
Left$
andRight$
, neither of which throw an exception if you specify a length that's longer than the string. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
either of which throw an exception if you specify a length that's longer than the string.
Why on Earth should they?
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
-
the parameter is itself a
Nullable
and i know this is the latest code that we are working on in .NET 4.0 using C# 6 language in it. -
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
-
You could consider telling the senior dev about the .HasValue property; knowledge share. Also, the correct English for "... an nullable ..." is "... a nullable ...". :)
senior is aware of that, but i am not getting why this method was authored in first place :) I will discuss for sure sometime that.
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
-
quit reviewing my code little dude. :)
To err is human to really mess up you need a computer
:omg: it was you, didn't knew :laugh:
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
Ehsan Sajjad wrote:
The code is written by very senior developer on my team with huge years of experience in c#
There's the problem right there -- they're not used to thinking of base, simple types as classes. Ask them what they think an int? is, and they'll probably tell you it's a pointer to an int. Pointers aren't classes that can have functions. Took me a while to get used to the concept too.
I live in Oregon, and I'm an engineer.
-
Ehsan Sajjad wrote:
The code is written by very senior developer on my team with huge years of experience in c#
There's the problem right there -- they're not used to thinking of base, simple types as classes. Ask them what they think an int? is, and they'll probably tell you it's a pointer to an int. Pointers aren't classes that can have functions. Took me a while to get used to the concept too.
I live in Oregon, and I'm an engineer.
you are probably right, once i was having code review meeting with that dev and i was asked to replace var keyword with that particular type name, as according to him, it will degrade performance, i then corrected him and gave reference to a post which explained it is just implicit variable that infers the type and nothing else. :)
-
A consultant friend of mine who has been programming far longer than I have—he started programming on the early Macs and helped write the first music notation software for the Mac—doesn't see why C# should need to use .SubString() to get the left/right most characters from a string and instead wrote these. I don't really have a problem with this sort of thing. I just don't see the point, but that's just me.
public string LeftStringFunction(string sValue, int iMaxLength) { //Check if the value is valid if (string.IsNullOrEmpty(sValue)) { //Set valid empty string as string could be null sValue = string.Empty; } else if (sValue.Length > iMaxLength) { //Make the string no longer than the max length sValue = sValue.Substring(0, iMaxLength); } //Return the string return sValue; } public string RightStringFunction(string sValue, int iMaxLength) { //Check if the value is valid if (string.IsNullOrEmpty(sValue)) { //Set valid empty string as string could be null sValue = string.Empty; } else if (sValue.Length > iMaxLength) { //Make the string no longer than the max length sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength); } //Return the string return sValue; }
"...JavaScript could teach Dyson how to suck." -- Nagy Vilmos
I seem to remember VB having that functionality for strings.
-
Left$ would be GwBasic (not VB6 where the $s were dropped) :-)
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com
The
$
s weren't required, but IIRC they were still allowed.Left
returned aVariant
, whereasLeft$
returned aString
. But I could be wrong - it's been over 15 years since I last touched VB6. :D
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Richard Deeming wrote:
either of which throw an exception if you specify a length that's longer than the string.
Why on Earth should they?
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
Because that's how there were designed. Probably because that's how the Java version[^] works. :) Specifying an index or a length outside of the string is usually a sign that something's gone wrong. In which case, it's better to have your application crash than for it to continue with potentially corrupted data. If you don't like it, you can always add your own checks. Or roll your own "ignore-the-errors" wrappers. Or call the VB.NET methods[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The
$
s weren't required, but IIRC they were still allowed.Left
returned aVariant
, whereasLeft$
returned aString
. But I could be wrong - it's been over 15 years since I last touched VB6. :D
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Ah yes, now that you say it, it sounds vaguely familiar :-)
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
He hasn't written enough code for the week.
-
Well obviously checking for false (in the calling code) is so much better than checking for null. Plus, if the int isn't null (contains a value) then you get the wonderful True back and true just makes you feel good. :laugh:
my one of the colleague just pinged me with :
IsHasValue() => IsHasHavingValuePerhaps();
-
While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :
private bool IsHasValue(int? a)
{
if (a== null)
{
return false;
}
return true;
}The code is written by very senior developer on my team with huge years of experience in c# :laugh:
My all-time favourite was from a mid-level programmer of ours who wanted to pass a form field value from one web page to the next: something he did all the time... except that in this instance, for some inexplicable reason, he created a table in the database, used Ajax to write the value to it and retrieve a record ID; he then put the record ID into a hidden field on the page, submitted the form, and then, in the next page, used the record ID to retrieve the value from the database and delete the record. Unfortunately, his English wasn't that great... so in response to the question, "What the hell is this supposed to be?" he said, "That mean I do my way. Fuck you, Dan!"
-
My all-time favourite was from a mid-level programmer of ours who wanted to pass a form field value from one web page to the next: something he did all the time... except that in this instance, for some inexplicable reason, he created a table in the database, used Ajax to write the value to it and retrieve a record ID; he then put the record ID into a hidden field on the page, submitted the form, and then, in the next page, used the record ID to retrieve the value from the database and delete the record. Unfortunately, his English wasn't that great... so in response to the question, "What the hell is this supposed to be?" he said, "That mean I do my way. Fuck you, Dan!"
I also had a mid level developer colleague who had set the bool flag to false by default and then the next statement was checking if it's true