- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Stop Poking Me!
- Operation Erred Successfully
- A Dark Turn
- Nothing Doing
- Home By Another Way
- Coast Star
- Forsooth
- Epic
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I hope the "case 81" is a type of the submitter...
Admin
81 FTW?
I'm always amazing how RTFM still is the best way to identify bad developers even without obvious sloppy mistakes :-)
Admin
The limit of 9 padding zeroes is less bothersome, since AFAIK in C#,
int
is 32 bits(1), but there's still no point in writing code like this when there's so many built-in options for padding.(1) In C/C++, on the other hand, it would make my portability teeth itch, since C/C++ allows
int
to be any length not shorter thanshort
and not longer thanlong
, so it might be 64 bits...Admin
ChatGPT joined our competition:
Admin
I once needed to leftpad a string, but it was with a weird character, so I could not use a built-in. I did
return "*********".substring(0, length - string.length) + string;
It looks really stupid, yet I can't think of a better way.
Admin
With lowercase "l" on .length, this isn't C#, probably Java.
Admin
I'll admit I sometimes use something like this:
Return Right('*******************' & input.ToString(), DesiredLength)
Admin
I never touched C# , so I have a question. Since there's no return statement in that
ZeroPadString()
function, does the code crash or try to guess the variable to return (and in such case, does it know for sure thatresult
is the variable that must be returned?)Admin
https://learn.microsoft.com/en-us/dotnet/api/system.string.padleft?view=net-8.0#system-string-padleft(system-int32-system-char)
Admin
You are right - I thought it's a Java developer trying to develop something in C# but now I think it's a Java developer doing his normal thing. It makes all sense now :-).
Admin
It the «case 81» a typo or a sub-WTF ?
Admin
Ha, I'm not sure if ChatGPT copied the right implementation there for .net; the actual code looks like this:
Admin
Funny that all the previous similar comments were «held for moderation» at the time of writing! Sorry for the duplicate then (unless it is a feature, with so many articles showing unnecessary code duplications).
Admin
While
int
could be longer than 32 bits, you can't portably depend on it. So you shouldn't useint
for something that might be bigger, and 10 digits is enough for portable callers.Admin
String
is written with an upper-case S in Java, and there's no operator overloading, so it can't be some custom string implementation. This method is also lacking areturn
, so I don't think it's valid in either language.Admin
I'm guessing a slipup in the editing process. Yes, the return is required.
Admin
Try this on for size:
Excuse the formatting when it inevitably breaks.
Admin
Why does it matter how many bits the language supports? This is zero padding a string, so it's probably to print a report or make sure some strings sort numerically instead of alphabetically or store in some sort of a fixed length record.
The Case 81 is odd, but probably a fat finger since l is right next to :. Verdict? WTF.
Admin
Indeed, although my point was that you can't portably depend on it being only 32 bits (or shorter), so 9 is the wrong maximum pad size.
Admin
I'm just happy that value is always a non-negative integer. Right?
Admin
I'm more triggered that they aren't null checking before getting the length, but maybe that's not an issue in C#, java would definitely cry about it.
Admin
sprintf(s,"%099d",value)...
Admin
isn't it obvious?
public static string PadWithZeroes(string input, int totalWidth) { if (input.Length >= totalWidth){ return input; } return PadWithZeroes("0"+input, totalWidth-1) }
Admin
Especially since the function is not for padding (stringified) integers, but arbitrary strings.
Admin
Two minutes of Googling gave me:
C# (https://stackoverflow.com/questions/3754582/is-there-an-easy-way-to-return-a-string-repeated-x-number-of-times) string result = new String('-', 5);
I already knew Java had a String.repeat() but Googled to find out it's in Java 11+: String result = "-".repeat(5);
Of course, in Java I'd do a variant of: String.format("%0" + n + "d", number);
Unsure about C#...
Admin
In this specific case, it is not important how many number symbols the value can have, because it can have more than int or even long can hold. Remember the method's signature: "public static string ZeroPadString(string _value, int _length)" - _value is a string, so you could call String s = ZeroPadString("3502658762499502744769673496362914826758701232312587624234387", -12); without "problems"...
Admin
In the spirit of "technically correct, which is the best kind of correct": use a language with lazy evaluation, reverse the numbers, add a generator for infinitely many padding chars, take what you need, and reverse again. That's probably one line in Haskell.
Admin
In C# you should always use string interpolation for string construction except in those two cases:
You are doing it in an iteration, use StringBuilder instead.
You absolutely know what you are doing and how the language is not only designed but where the design time is heading to in the future (aka you read git csharplang for breakfast).
Now with string interpolation the intended code would look like this:
However this would be very bad practice for one simple reason: To get the most out of string interpolation, you want to construct the whole string in one go, here would be an example:
Now reasonable assumption of a translation is that those two lines get translated to:
Now potentially the second line will actually not use the old string.Format method, because with code generation a template could be constructed to avoid the parsing the format string instead, so there is another reason not to use those methods directly when you absolutely not know what your are actually doing. Compared to Java development in C# moves quickly and what was a pattern just one year ago can be an anti-pattern a year later (a good example is 'init' before and after 'required' in combination with null bangs).
Addendum 2024-10-01 02:02: I just checked, the current version of .net uses DefaultInterpolatedStringHandler instead of the legacy string.Format version, but not codegen yet.
Admin
Oh come on. I wrote a beautiful algorithm for this and it's stuck on "approval". I wish they'd just disable the stupid thing. All it does is block honest people posting.
Admin
So many missing
return
s! But not 81 of them...