Comment On Refactoring History

Refactoring is a trick that managers play on us, to waste our time and break our spirits. Just because they can't wrap their tiny minds around large chunks of similar code doesn't mean that you are unable to wield these weapons. Sometimes, however, we are forced to do things we don't want to do. If you have to refactor: do it late and infrequently. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Refactoring History

2007-03-26 09:05 • by bstorer
[quote]
...whereas the two latter have been lost to the ages.
[quote]
Can you lose something you never wanted?

Re: Refactoring History

2007-03-26 09:11 • by vertagano
128440 in reply to 128439
[quote user="bstorer"][quote]
...whereas the two latter have been lost to the ages.
[quote]
Can you lose something you never wanted?[/quote]

Yes. " 14. To cause to be destroyed. Usually used in the passive: Both planes
were lost in the crash."

Re: Refactoring History

2007-03-26 09:11 • by Al (unregistered)
Interestingly enough, similar excavations turned up a rather special end result to all this, expressed best through the use of code.


Function replaceEmp( name )

Dim employee
employee = replace(employee, "old guy", "new guy")

End Function


Even if you hate VBScript, how can one not giggle at "Dim employee"?

Re: Refactoring History

2007-03-26 09:12 • by Kerin
I think what scares me is how much this natural progression resembles my first attempt at coding an isometric RPG game.

Re: Refactoring History

2007-03-26 09:13 • by Al (unregistered)
Forgive my lack of coffee. I meant to type this:


Function replaceEmp( name )

Dim employee
employee = replace(name, "old guy", "new guy")

End Function

Re: Refactoring History

2007-03-26 09:13 • by Jonh Robo (unregistered)
A Refactoring Conundrum

...but if you lose:

If Session("Reprint") = "" Then
Session("Reprint") = ""
End If

then how will Session("Reprint") ever get set to "" if
Session("Reprint") is equal to ""?

Re: Refactoring History

2007-03-26 09:14 • by vertagano
128445 in reply to 128441
Al:
Interestingly enough, similar excavations turned up a rather special end result to all this, expressed best through the use of code.


Function replaceEmp( name )

Dim employee
employee = replace(employee, "old guy", "new guy")

End Function


Even if you hate VBScript, how can one not giggle at "Dim employee"?


One might not be giggling because they're too busy trying to figure out why a parameter was included and never used. ;-)

Re: Refactoring History

2007-03-26 09:16 • by Rafael Larios (unregistered)
WOW..... just..... WOW.


When I write programs, I allways tell myself.. "There isn't any other easy way?"...

seems that the programer at the snippet didn't question himself very often.


What the hell is Gygax?

Re: Refactoring History

2007-03-26 09:21 • by vertagano
128447 in reply to 128446
Gary Gygax is the inventor of D&D, and many other roleplaying games.

Or, it could be refering to Daniel Gygax, a Swiss football player....

Re: Refactoring History

2007-03-26 09:25 • by bstorer
128448 in reply to 128440
vertagano:
bstorer:

...whereas the two latter have been lost to the ages.

Can you lose something you never wanted?


Yes. " 14. To cause to be destroyed. Usually used in the passive: Both planes
were lost in the crash."

Don't cite dictionary definitions at me. You need to get meta!

Re: Refactoring History

2007-03-26 09:32 • by Edowyth
yeah, basic string manipulation should have been tested before this joker was hired...

If you hire someone who's LEARNING the skill set(s) required for the job, you should know beforehand (well, if you expect to make any money)!

Re: Refactoring History

2007-03-26 09:43 • by bstorer
128450 in reply to 128449
Edowyth:
yeah, basic string manipulation should have been tested before this joker was hired...

If you hire someone who's LEARNING the skill set(s) required for the job, you should know beforehand (well, if you expect to make any money)!


But you make your money on the fact that you're paying the guy far less. Of course, if you're a company dumb enough to overpay for an employee with no experience, you get what you deserved.

Re: Refactoring History

2007-03-26 10:02 • by rogueuser
I hate single letter varibles! Everytime I have to search code for a varible named 'a' I want to scream!

Re: Refactoring History

2007-03-26 10:03 • by akatherder
The dumb employee gets to train on the job and gets a paycheck.

The company hopefully gets to pay him what he's worth (very little). They presumably get code that still works. The manager doesn't care how crappy and inefficient it is. It's not like he has to maintain it.

We get to see more stupid code.

It's a win-win-win situation!

Re: Refactoring History

2007-03-26 10:09 • by KattMan
128457 in reply to 128454
rogueuser:
I hate single letter varibles! Everytime I have to search code for a varible named 'a' I want to scream!


I love my single letter vars, they go on little single letter dates and have single letter sex creating nice multi letter vars. Where do you think well named vars come from?

Re: Refactoring History

2007-03-26 10:12 • by Rootbeer

Thank god they replaced those inscrutable single-letter variable names with longer, self-explanatory var names like "vlu"!

Re: Refactoring History

2007-03-26 10:16 • by Massif (unregistered)
128459 in reply to 128457
Is that where two-letter commands come from? Are well-named variables several generations down?

Re: Refactoring History

2007-03-26 10:22 • by rp (unregistered)
128460 in reply to 128444
Jonh Robo:
A Refactoring Conundrum

...but if you lose:

If Session("Reprint") = "" Then
Session("Reprint") = ""
End If

then how will Session("Reprint") ever get set to "" if
Session("Reprint") is equal to ""?


I'm not quite sure whether you're serious, but I hope you are.

The trouble with refactoring is the hidden landmines (i.e. people don't document the invariants their code relies on). In this case, a string can compare equal to "" without actually being the same as "", for instance, when it's a null reference (Nothing in VB.NET), and then be passed to a piece of code that depends on such strings actually being the same as "". Try this at home:


Option Strict On

Imports System

Public Module Main

Private Function almostNoOp(ByVal s As String) As String
If s = "" Then
s = ""
End If
Return s
End Function

Private Function withLength(ByVal s As String) As String
Return s & " (" & s.Length & ")"
End Function

Private Sub emitStringWithLength(ByVal s As String)
Console.WriteLine(withLength(s) & Environment.NewLine)
End Sub

Public Sub Main()
emitStringWithLength("starting")
emitStringWithLength(almostNoOp(Nothing))
emitStringWithLength("that was test 1")
emitStringWithLength(Nothing)
emitStringWithLength("that was test 2")
End Sub

End Module

Re: Refactoring History

2007-03-26 10:34 • by TomB (unregistered)
I'm pretty sure you need a return value for a function

Function replaceEmp( name )

Dim employee
employee = replace(name, "old guy", "new guy")

End Function

should be

Function replaceEmp( name )

replaceEmp = replace(name, "old guy", "new guy")

End Function

Re: Refactoring History

2007-03-26 10:36 • by DingBat (unregistered)

I dunno, the OP is using the word "refactor" in strange ways I've never heard before.

Refactoring, as I understand it, is a developer tool used to keep code from getting to exactly the place this example arrived at. What do managers have to do with refactoring?

Re: Refactoring History

2007-03-26 10:44 • by speaking of wtf's (unregistered)
man does this forum software suck!

Re: Refactoring History

2007-03-26 10:56 • by bstorer
128466 in reply to 128457
KattMan:
rogueuser:
I hate single letter varibles! Everytime I have to search code for a varible named 'a' I want to scream!


I love my single letter vars, they go on little single letter dates and have single letter sex creating nice multi letter vars. Where do you think well named vars come from?


Hmmm... perhaps we need a genetic algorithm to generate good variable names...

Re: Refactoring History

2007-03-26 10:58 • by Jimmie (unregistered)
128467 in reply to 128463
ASP/VB is in major need of just one little thing...

BRACES!!!!

Wend?

End Function?

End If?

OMG! That kind of coding structure is sooooo 1980!

the real WTF is the VB syntax

Re: Refactoring History

2007-03-26 11:00 • by KattMan
128469 in reply to 128462
DingBat:

I dunno, the OP is using the word "refactor" in strange ways I've never heard before.

Refactoring, as I understand it, is a developer tool used to keep code from getting to exactly the place this example arrived at. What do managers have to do with refactoring?


Depends on your context.
To a programmer, refactor means to fix the code, which in the end of the article it was refactored in this fashion.
To a manager, refactor means to upgrade or add functionality. It is a danger of us techies using our language to explain something to non-techies.

Re: Refactoring History

2007-03-26 11:20 • by TheRider
128471 in reply to 128458
Rootbeer:

Thank god they replaced those inscrutable single-letter variable names with longer, self-explanatory var names like "vlu"!

We all know that variable names should at least be four-letter-words! :P

Re: Refactoring History

2007-03-26 11:25 • by Faxmachinen
Am I the only one who thinks that escaping quotes with a pair of quotes in quote-delimited strings is a bit stupid?
I don't usually bash languages I don't know, but jeez.

Re: Refactoring History

2007-03-26 11:32 • by KattMan
128473 in reply to 128472
Faxmachinen:
Am I the only one who thinks that escaping quotes with a pair of quotes in quote-delimited strings is a bit stupid?
I don't usually bash languages I don't know, but jeez.


That actually is the best way to escape anything unless you want to build all kinds of rule exceptions. Even C does this with the \ character; if you actually want it, then escape it with itself.

For a real WTF, just take a look at the CSV format and find out how it wants you to escape things and decide how easy it would have been if they had just allowed ,, to escape the ,.
The problem is this:
You want a comma in your data, encase your data in quotes. Now you created a new problem, what if you want quotes in your data? You have to escape those now. Always easier to allow your delimiting character to escape itself or you are simply adding new delimiters.

Re: Refactoring History

2007-03-26 11:33 • by erKURITA (unregistered)
If Session("Reprint") = "" Then
Session("Reprint") = ""
End If


Head->Desk

Re: Refactoring History

2007-03-26 11:34 • by bstorer
128477 in reply to 128472
Faxmachinen:
Am I the only one who thinks that escaping quotes with a pair of quotes in quote-delimited strings is a bit stupid?
I don't usually bash languages I don't know, but jeez.

That's how you escape a single quote in a string in SQL. Some DBs will let you use \', too.

Re: Refactoring History

2007-03-26 11:36 • by ssprencel
128478 in reply to 128461
TomB:
I'm pretty sure you need a return value for a function

Function replaceEmp( name )

Dim employee
employee = replace(name, "old guy", "new guy")

End Function

should be

Function replaceEmp( name )

replaceEmp = replace(name, "old guy", "new guy")

End Function


But then you loose out on the Dim employee.


Function replaceEmp( name )

Dim employee 'ToDo: Remove this variable

replaceEmp = replace(name, "old guy", "new guy")

End Function


Re: Refactoring History

2007-03-26 11:46 • by Peter (unregistered)

Re: Refactoring History

2007-03-26 11:50 • by Ed (unregistered)
I'm guessing this is a typo..

Function CheckString(s)
p = InStr(s, "'")
While p > 0
s = Mid(s, 1, p) & "'" & Mid(s, p + 1)
pos = InStr(p + 2, s, "'")
Wend
CheckString = "'" & s & "'"
End Function

because if this code is run as above, it'll get stuck in an infinite loop.

Re: Refactoring History

2007-03-26 11:54 • by JGM (unregistered)
128488 in reply to 128473
But if you allow ,, to escape , how do you specify an empty column?

Re: Refactoring History

2007-03-26 12:05 • by emurphy
128493 in reply to 128439
[quote user="bstorer"][quote]
...whereas the two latter have been lost to the ages.
[quote]
Can you lose something you never wanted?[/quote]

Mona Mayfair: "I didn't lose my virginity. I obliterated all traces of its existence."

Re: Refactoring History

2007-03-26 12:31 • by KattMan
128499 in reply to 128488
JGM:
But if you allow ,, to escape , how do you specify an empty column?


Well true.
But you should have one escaping character and use it everywhere even when escaping itself. I admit my example was off the mark but it does show part of the problems with not defining an escaping character. There are other formats that come to mind, VB and SQL handling things in a similar fashion while C uses \ and \\ to escape the escape character.

Then XML comes to mind where there is no easy way to place a & in a field that needs to be parsed by XSL then used in the resulting HTML. For example you want to have a non-breaking space so the final HTML needs to show   but the XSL transform will escape it out to a space on it's own leaving you with literally a blank that might not get rendered so you have to encode this in the original XML as such:  . This means you have to know how many transforms you are going to go through from the begining before you can encode it properly. Got another level to transform through, then you need to encode it as such:  


Things just get hairy when you don't have a good escaping format.

Re: Refactoring History

2007-03-26 12:43 • by Faxmachinen
128506 in reply to 128473
KattMan:
That actually is the best way to escape anything unless you want to build all kinds of rule exceptions. Even C does this with the \ character; if you actually want it, then escape it with itself.
Yes, but a string literal in C isn't enclosed in slashes.

I can only guess how horrible a print-my-own-sourcecode program would look in VB.

Re: Refactoring History

2007-03-26 12:49 • by KattMan
128510 in reply to 128506
Faxmachinen:
KattMan:
That actually is the best way to escape anything unless you want to build all kinds of rule exceptions. Even C does this with the \ character; if you actually want it, then escape it with itself.
Yes, but a string literal in C isn't enclosed in slashes.

I can only guess how horrible a print-my-own-sourcecode program would look in VB.


The only real problem there is if you were doing syntax highlighting. As all lines would be read in as strings already rather then you trying to build the string you need not worry about the " character, it is already contained in the string.

What you do have to worry about is when you are doing syntax highlighting you need to look for " and read ahead one character to see if the next one is also a ", if it is print just one, if it isn't back up and change text color and move forward again. At least with C you know when you reach a \ you simply print the next character knowing it is still part of the string itself. You don't stop highlighting your string until you reach a lone ".

Re: Refactoring History

2007-03-26 13:01 • by bstorer
128513 in reply to 128499
KattMan:
JGM:
But if you allow ,, to escape , how do you specify an empty column?


Well true.
But you should have one escaping character and use it everywhere even when escaping itself. I admit my example was off the mark but it does show part of the problems with not defining an escaping character. There are other formats that come to mind, VB and SQL handling things in a similar fashion while C uses \ and \\ to escape the escape character.

Then XML comes to mind where there is no easy way to place a & in a field that needs to be parsed by XSL then used in the resulting HTML. For example you want to have a non-breaking space so the final HTML needs to show   but the XSL transform will escape it out to a space on it's own leaving you with literally a blank that might not get rendered so you have to encode this in the original XML as such:  . This means you have to know how many transforms you are going to go through from the begining before you can encode it properly. Got another level to transform through, then you need to encode it as such:  


Things just get hairy when you don't have a good escaping format.

The same would be the case if you used, say, "\ " to do a non-breaking space. The XSLT would convert it to a space, so you'd have to do "\\ " instead. And if you had two levels of transformation, you'd have to have "\\\\ ". Not exactly a better solution, eh?
The key is to keep the XSLT from transforming the escaped characters on every pass, say through using CDATA. More specifically, through the cdata-section-element attribute of the xsl:output element. I'd provide a complete example, but I don't work in XSLT unless I'm getting paid to do so.

Re: Refactoring History

2007-03-26 13:02 • by Aaron (unregistered)
The fact that a good majority of the "code WTFs" are in VB doesn't help my opinion that VB is an inferior language. :P


(catpcha: onomatopoeia -- come ON... if a captcha decoder script can read 5 letters written as normal text, what makes you think it can't read 12...is it necessary to make me type long words?)

Re: Refactoring History

2007-03-26 13:07 • by Opie (unregistered)
Using quotes to escape themselves is not readable - that's the problem. It's pretty hard to look at a bunch of quotes stuck together and know exactly what they represent.

However, slashes are extremely easy to count.
They only start to get ridiculous around 6 or so, and really how often do you encounter that many?
The most I've ever been forced to use (due to ajax and passing special strings back and forth) is 8 (\\\\\\\\), and that's a hell of a lot easier than reading """""""", especially if your text editor doesn't use a monospace font.

Re: Refactoring History

2007-03-26 13:08 • by KattMan
128518 in reply to 128515
Aaron:
The fact that a good majority of the "code WTFs" are in VB doesn't help my opinion that VB is an inferior language. :P


(catpcha: onomatopoeia -- come ON... if a captcha decoder script can read 5 letters written as normal text, what makes you think it can't read 12...is it necessary to make me type long words?)


It isn't that VB is an inferior language, it is rather that inferior programmers tend to work more in VB. This does not imply that the reverse logic is true, meaning that just because you work in VB does not mean you are an inferior programmer; you just have a higher probability of working with one.

Re: Refactoring History

2007-03-26 13:18 • by Jon (unregistered)
128522 in reply to 128518
Still, the old joke goes, "The 'B' in VB stands for 'Beginner'."

([Visual] Beginner's All-purpose Symbolic Instruction Code)

Re: Refactoring History

2007-03-26 13:25 • by no name (unregistered)
128526 in reply to 128517
Opie:
Using quotes to escape themselves is not readable - that's the problem. It's pretty hard to look at a bunch of quotes stuck together and know exactly what they represent.

However, slashes are extremely easy to count.
They only start to get ridiculous around 6 or so, and really how often do you encounter that many?
The most I've ever been forced to use (due to ajax and passing special strings back and forth) is 8 (\\\\\\\\), and that's a hell of a lot easier than reading """""""", especially if your text editor doesn't use a monospace font.


They actually make those? And people use them??? Coders????

Re: Refactoring History

2007-03-26 13:37 • by KattMan
128536 in reply to 128526
no name:
Opie:
Using quotes to escape themselves is not readable - that's the problem. It's pretty hard to look at a bunch of quotes stuck together and know exactly what they represent.

However, slashes are extremely easy to count.
They only start to get ridiculous around 6 or so, and really how often do you encounter that many?
The most I've ever been forced to use (due to ajax and passing special strings back and forth) is 8 (\\\\\\\\), and that's a hell of a lot easier than reading """""""", especially if your text editor doesn't use a monospace font.


They actually make those? And people use them??? Coders????


Yes they still make slashes. They were going to stop production of slashes thinking that only mathematicians used them and by their calculations there should have been a surplus for decades; then us coders spoke up and started using this untapped resource in mass quantities. Since then, instead of stopping production, they have had to increase production seven fold.

Oh wait, you were talking about mono-spaced fonts, sorry.

Re: Refactoring History

2007-03-26 13:41 • by Faxmachinen
128540 in reply to 128510
KattMan:
What you do have to worry about is when you are doing syntax highlighting you need to look for " and read ahead one character to see if the next one is also a ", if it is print just one, if it isn't back up and change text color and move forward again.
Now you see, that's a perfect example. What happens when you apply your algorithm to an empty string?
And since when did syntax highlighting include replacing escape sequences anyway?

Re: Refactoring History

2007-03-26 13:53 • by KattMan
128545 in reply to 128540
Faxmachinen:
KattMan:
What you do have to worry about is when you are doing syntax highlighting you need to look for " and read ahead one character to see if the next one is also a ", if it is print just one, if it isn't back up and change text color and move forward again.
Now you see, that's a perfect example. What happens when you apply your algorithm to an empty string?
And since when did syntax highlighting include replacing escape sequences anyway?


Yeah no replacements, see I'm not perfect.

But empty strings are handled easy, because you have to actually be in the string before you can escape it.

Hit the first quote, we are not in the string yet so start highlighting
Flag it now as in string.
Next character read in.
If it is a quote and PriorQuote is true keep highlighting and set PriorQuote = !PriorQuote.
If it is not a quote and PriorQuote is True stop highlighting and flag it as out of string.
All other characters, set PriorQuote to false and keep highlighting.

Re: Refactoring History

2007-03-26 14:54 • by Your Name (unregistered)
128571 in reply to 128454
rogueuser:
I hate single letter varibles! Everytime I have to search code for a varible named 'a' I want to scream!


Use "vi", then /\<a\>/ finds just that variable, and not the "a" in assert or whatever

Re: Refactoring History

2007-03-26 15:11 • by ssprencel
128578 in reply to 128536
KattMan:
no name:
Opie:
Using quotes to escape themselves is not readable - that's the problem. It's pretty hard to look at a bunch of quotes stuck together and know exactly what they represent.

However, slashes are extremely easy to count.
They only start to get ridiculous around 6 or so, and really how often do you encounter that many?
The most I've ever been forced to use (due to ajax and passing special strings back and forth) is 8 (\\\\\\\\), and that's a hell of a lot easier than reading """""""", especially if your text editor doesn't use a monospace font.


They actually make those? And people use them??? Coders????


Yes they still make slashes. They were going to stop production of slashes thinking that only mathematicians used them and by their calculations there should have been a surplus for decades; then us coders spoke up and started using this untapped resource in mass quantities. Since then, instead of stopping production, they have had to increase production seven fold.

Oh wait, you were talking about mono-spaced fonts, sorry.


I nominate this as Post-of-the-Day.

Re: Refactoring History

2007-03-26 15:36 • by JoshJ (unregistered)
128587 in reply to 128571
But it would also find "a" in the string sentence "Bob bought a car."

Re: Refactoring History

2007-03-26 15:39 • by drd (unregistered)
128588 in reply to 128571
Even easier, (assuming by 'vi' you mean 'vim'), just move your cursor to the 'a' token, then type *. You'll get the word-boundary restrictions automatically.
« PrevPage 1 | Page 2Next »

Add Comment