- Feature Articles
- CodeSOD
- Error'd
- 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
This looks oddly familiar. We have a class by the same name that also reinvents the wheel.
Admin
I used to maintain a codebase wherein we had a "StringUtils" class, but this was mainly to allow us to conveniently catch null strings. Also, the string manipulation routines in java 1.3 was limited compared to more recent versions. Hence it was necessary.
Note the "toRepalce" in the codebase. Dyseliaxa needs to be rmevoeed from all cdboeases.
Admin
Well, I guess Freemarker, MVEP or Velocity were very hard to understand for this so called architect.
Admin
Re the comment in the source...1.
Admin
String.replaceAll expects a regular expression, so the given helper method is not a real reinvention of the wheel.
Admin
Repalce.
Also... who the hell uses the indefinite article at the start of variable/parameter names? Baffling.
Admin
So replaceAll("","","Bob") returns "Bob"? That's pretty Zen.
Admin
String.replaceAll do expect a regexp, and as such is the wrong function for when you are replacing literal substrings. The function you want is String.replace.
Admin
So what's the recommended way to do this?
String.replaceAll wants a regex => does not fit
String.replace only replaces the first => does not fit
Calling replace multiple times? Would replace the searched string in the replacement string => does not work
Admin
Admin
String.replace replaces all instances of a Char or CharSequence (Strings implement CharSequence). String.replaceFirst replaces only the first instance.
Admin
Admin
The recommended way to replace all instance of a literal substring with a different literal substring is:
targetString.replace(toReplace, replacemnt);
No patterns/matchers/regex involved.
Admin
Just to get the obligatory posts out of the way:
TRWTF is Java.
I'm Philip.
I'm Philip, and so's my wife!
Captcha letatio: "The mayor has denied rumors that he received letatio from his secretary."
Admin
http://hg.openjdk.java.net/jdk7u/jdk7u40/jdk/file/ed809dc4f278/src/share/classes/java/lang/String.java row 2179
Admin
Zen would be replaceAll("abc", "", "Bo" returns "BoaBobBocBo"
Admin
Can we kill this constant == variable pattern yet? You're not testing the value of null or 3 or PI; you're testing the variable. Object-verb-subject order doesn't grammatically match very many programming languages (or many spoken languages, for that matter), so stop using it! Nao!
A Yoda-style
would be perfectly acceptable, however.Admin
I can't see a WTF here. The replace method could be written with less code lines and not allow empty string argument, but it seems to work fine.
Sometimes you must use old java versions like JDK1.3, especially if you are programming for embedded systems where you are limited in memory, which means: You cannot upgrade to a newer Java version, and you can add a method, but not include an additional library like the jakarta-commons-*.jar!
In JDK1.3 there is only a single replace-method available: replace(char oldChar, char newChar)
Admin
Admin
Admin
Admin
I don't know about Java, but in C# if (aReceiver = null) { stuff }
would be a compiler error as the expression doesn't result in a boolean.
Admin
So the people who use "normal" style are the suspicious ones. However, the Yoda programmers are unsufferable know-it-alls. I prefer the former types as my coworkers.
Admin
Much nicer in C++:
No bugs or compiler errors.
Admin
Unicorns and Rainbows! Who cares about anything else? Quick... someone call the BATFE!
Edit: Clicking [Expand Full Text] then clicking on "orientation" works in the comments section as well... didn't expect that... Trippy...
Admin
Which modern compiler doesn't warn about 1,000 things at a time? Why search for a needle in a haystack when you can just find the real errors?
Admin
Yoda followed Altaic grammar, topic-comment-verb, not object-verb-subject. It's a hidden refernce to Japanese martial artists. Now, this is being a know-it-all!
Admin
Nice trolling there.
Admin
Though there is still one case where the C/C++ warning does a better job:
is, without checking, I think legal C# and Java.
Admin
And experienced enough to have moderate RSI. Typos do happen, and when your hands hurt, they happen more.
CAPTCHA: incassum - incassum not here, I'll leave the side door open.
Admin
Admin
No it's not.
Having "replace" manage to only replace the first instance is not intuitive at all.
What if someone asked you to buy a dozen eggs, but replace broken eggs, and you only replaced the first broken egg?
replace"All" should be implied.
replace"First/Last/Count" should be a specification.
Admin
In Java, doing something like:
if ("TestString".equals(variable)) { : : etc.
saves on having to test whether variable is null. Do it the other way round and you have to try-catch NullPointerExceptions.
Admin
Absolutely.
Admin
Not really.
Using a compiler that only accepts boolean values for the result of an if statement is the best option.
Continuing to place the constant on the left in such a language is TRWTF.
Admin
If your compiler gives you 1000 warnings I rather think you need to go back to Programming 101. Leaving warnings unattended is bone-headed.
Admin
Or you could do what I did and make an extension method on object that checks for null.
Then you just have variable.EqualsNullSafe("TestString"). And if variable is null it returns false.
Admin
Beware the Ides of March.
Admin
Admin
I once lost a week of my life because someone who was assumed to be very clever replaced
if (ptr != NULL)
with
if (! ptr)
without checking the code, without doing a code review, in a place where it caused the maximum possible damage. The reason for the change was that he felt the second way looked more clever. I could have killed him. I should have killed him. Note that unlike the "=" vs. "==" confusion, the compiler has absolutely no chance to give a warning, just like it can't give a warning if you write "ptr == NULL" instead of "ptr != NULL".
Admin
Mine doesn't. Because I've turned on the "warning = errors" settings, so any warning gets fixed immediately. Actually, I'd estimate that more than half the warnings turn out to be logic errors which would have been much harder to find if I didn't leave it to the compiler.
Admin
Admin
FTFY
Admin
So you did an extension method on object that checks for null so you can call object methods on null references in Java ?
And what you was smoking while doing that ? I want some of your good shit.
It turns null to an object just with a couple of puffs.
null is not an Object, is just a special kind of weird type who can't be used to declare or cast anything to it, it only can be referenced.
Admin
Matt Westwood is right here.
If you have a constant TEST_VALUE which is statically defined somewhere, you should always do if(TEST_VALUE.equals(input)){}
If you instead call the equals() method of the input object, you must first check it for non-null like this: if(input != null && input.equals(TEST_VALUE)){}
doing it in the right order saves the null check.
The guy who talked about overloading object to handle this is a troll who is trying to create Null Pointer Errors (if 'input' is null, it doesn't have that overloaded method anyway, it's NULL!!).
Admin
Admin
You can do that with extension methods in C# as he says. This runs and prints 'Object is null'
Admin
Didn't you mean
See how stupid that notation is? Even in a debate about the merits of that pattern, it is just simply more natural to write myVar == conditionalValue.Debate over.
Admin
Admin