Variable abuse RANT

Kev

Lifer
Dec 17, 2001
16,367
4
81
I started a new job where I inherited a lot of code that was written by an agency who just apparently loved to "overcode" everything. It's ridiculous. For example, they made a variable called search to display the word "Search" -- and there is crap like this all over the site. It's a nightmare.

I give myself a 0/10 for this rant. I just had to express it somewhere.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Wait, as in the variable Search was the string "Search"?

That's not really that bad, is it? At least, if the app in question was ever going to be localized.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Originally posted by: PhatoseAlpha
Wait, as in the variable Search was the string "Search"?

That's not really that bad, is it? At least, if the app in question was ever going to be localized.

With older compilers, strings where not matched so to speek, so every time in code that you put "Search" you would generate a new string with exactly the same characters. Perhaps for a old project the idea was to save space by having one commonly used string as its own variable.

Sometimes, making something into a local variable will make the program go faster as well, this might have been another aspect of the programmers logic.

Either way, I'm going to guess that it fell in the category of "optimizations". Generally you can have clean code or optimized code, but not both.
 

Kev

Lifer
Dec 17, 2001
16,367
4
81
Originally posted by: PhatoseAlpha
Wait, as in the variable Search was the string "Search"?

yes

That's not really that bad, is it? At least, if the app in question was ever going to be localized.

It was not going to be localized. As far as I can tell




 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
One of my predecessors apparently did not know about variables, he used hidden textboxes to store any information he needed. Given he was a new college student, but still. It sucks to work on his code, There is still a little left I need to clean up.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Originally posted by: Kev
I started a new job where I inherited a lot of code that was written by an agency who just apparently loved to "overcode" everything. It's ridiculous. For example, they made a variable called search to display the word "Search" -- and there is crap like this all over the site. It's a nightmare.

I give myself a 0/10 for this rant. I just had to express it somewhere.

Thats actually good coding practices...
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
Originally posted by: WannaFly
One of my predecessors apparently did not know about variables, he used hidden textboxes to store any information he needed. Given he was a new college student, but still. It sucks to work on his code, There is still a little left I need to clean up.

In other words a n00b VB programmer who read "VB in 7 Days."
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,065
75
Yes, that's good coding practice. For instance, if you want to translate your app to Spanish, all you have to change for all the Search words is to make the string "Busca" instead. Or imagine if you hard-coded the number of states in the United States, say, as 50; and then Puerto Rico became a state.

There are certain better things that could be done, like prefixing the variable name:

const String cstrSearch = "Search";

or pulling all the string values out of a file. You might have constants or #defines (you didn't specify the language) like:

#define CSTR_SEARCH 1

then read lines into an array, so later you'd say cstr[CSTR_SEARCH], instead of cstrSearch.

But overall it's good practice.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
i once inherited a php website, roughly 100,000 lines of code, most of which was echoing html
echo '<br>';
echo '<a href="page.php">link</a>';
echo '<br>';

sometimes it would vary and it would all be in one echo

the previous developer also loved to format his sql in a really lovely manner.
$sql = "select field from";
$sql .= "tablename";
$sql .= "WHERE mycode=tehsuck";
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: Kev
I started a new job where I inherited a lot of code that was written by an agency who just apparently loved to "overcode" everything. It's ridiculous. For example, they made a variable called search to display the word "Search" -- and there is crap like this all over the site. It's a nightmare.

I give myself a 0/10 for this rant. I just had to express it somewhere.

We're here for you mate. Yours is not the only story.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: WannaFly
One of my predecessors apparently did not know about variables, he used hidden textboxes to store any information he needed...

w. o. w.

 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
Originally posted by: troytime
i once inherited a php website, roughly 100,000 lines of code, most of which was echoing html
echo '<br>';
echo '<a href="page.php">link</a>';
echo '<br>';

I inherited a system back in the day where the original developer - a highly paid consultant - created a VB DLL and put a shit ton of HTML in it in the same way. The idiot then created an ASP frontend that essentially said

<%=dllobject.WriteCustomerPage()%>

Changing the text of an email or web page (which happened quite frequently) took a long time and required a recompile and then of course because the IIS server was using the DLL it required downing the web service to update it. It took me months to extract everything and put it into a proper ASP format. It's bizarre, because the guy almost had it right. He already had public objects that exposed all the properties and methods required to create the page using a mix of ASP and VB objects, only he put the HTML right in the DLL and used the objects from there.

That was really only a part of the problem though, because the guy had also used a BAS file in the DLL project and used it to create a global instance of the DLLs main object. I learned an awful lot about the threading model of VB6 and IIS/ASP trying to track down bugs. Turned out every session was sharing a single copy of the object. As long as traffic was low things wouldn't step on each other, but as soon as the number of users outnumbered the active asp threads, sessions would start overwriting each others data.

And this was the same consultant who wrote the companies relatively complex billing system as his first VB project ever, about which he said "When you're learning a new language, it's important to do it on an unimportant project because you're going to make mistakes."

*cry*
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Originally posted by: BoberFett
Originally posted by: troytime
i once inherited a php website, roughly 100,000 lines of code, most of which was echoing html
echo '<br>';
echo '<a href="page.php">link</a>';
echo '<br>';

I inherited a system back in the day where the original developer - a highly paid consultant - created a VB DLL and put a shit ton of HTML in it in the same way. The idiot then created an ASP frontend that essentially said

<%=dllobject.WriteCustomerPage()%>

Changing the text of an email or web page (which happened quite frequently) took a long time and required a recompile and then of course because the IIS server was using the DLL it required downing the web service to update it. It took me months to extract everything and put it into a proper ASP format. It's bizarre, because the guy almost had it right. He already had public objects that exposed all the properties and methods required to create the page using a mix of ASP and VB objects, only he put the HTML right in the DLL and used the objects from there.

That was really only a part of the problem though, because the guy had also used a BAS file in the DLL project and used it to create a global instance of the DLLs main object. I learned an awful lot about the threading model of VB6 and IIS/ASP trying to track down bugs. Turned out every session was sharing a single copy of the object. As long as traffic was low things wouldn't step on each other, but as soon as the number of users outnumbered the active asp threads, sessions would start overwriting each others data.

And this was the same consultant who wrote the companies relatively complex billing system as his first VB project ever, about which he said "When you're learning a new language, it's important to do it on an unimportant project because you're going to make mistakes."

*cry*

Listening (or reading) to this story makes me think that you must have had a good green-horn there. Someone who's learning the right stuff, just doesn't know how to polish. Still, cleaning up after, IS a pain. :roll:
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
Originally posted by: VinylxScratches
How do these people get jobs to code and I can't even get my first job as a developer

It can be tough starting out. Once you have enough experience you can call yourself a consultant and charge $80+/hr to write bad code.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: BoberFett
Originally posted by: VinylxScratches
How do these people get jobs to code and I can't even get my first job as a developer

It can be tough starting out. Once you have enough experience you can call yourself a consultant and charge $80+/hr to write bad code.
And $100/hr to fix bad code

 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Ken g6
Yes, that's good coding practice. For instance, if you want to translate your app to Spanish, all you have to change for all the Search words is to make the string "Busca" instead. Or imagine if you hard-coded the number of states in the United States, say, as 50; and then Puerto Rico became a state.

There are certain better things that could be done, like prefixing the variable name:

const String cstrSearch = "Search";

or pulling all the string values out of a file. You might have constants or #defines (you didn't specify the language) like:

#define CSTR_SEARCH 1

then read lines into an array, so later you'd say cstr[CSTR_SEARCH], instead of cstrSearch.

But overall it's good practice.

Of for the love of good do not encourage anyone to use that useless notation.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,065
75
Originally posted by: smack Down
Of for the love of good do not encourage anyone to use that useless notation.
What, you want to search through 10000 lines of code to translate everything to Spanish, French, and German instead? Or is there a better solution I've overlooked?

 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Ken g6
Originally posted by: smack Down
Of for the love of good do not encourage anyone to use that useless notation.
What, you want to search through 10000 lines of code to translate everything to Spanish, French, and German instead? Or is there a better solution I've overlooked?

I'm talking about adding useless cstr_ to the name.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,065
75
While I don't agree with the specific prefixes used in Hungarian notation, I believe that the prefix concept in general is helpful in identifying a variable's contents. For instance, if you have a search box, there might be a label, a textbox, and a button. You can call them lblSearch, txtSearch, and btnSearch to differentiate them, while linking them all to the same area and function. Behind that, there might be a constant string for the label (especially if the label isn't often shown), which one might conceivably call cstrSearch. Or a better prefix might be cslbl (a constant string that goes in a label), especially if there's another string, which I might call csitxtSearch, a constant string to initialize the textbox txtSearch.

The point is, prefixes are useful, especially when you have several different objects related to one "thing".
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Full-blown hungarian is pretty lame, I agree, but prefixes are extremely useful (and post-fixes, if your style guide prefers them) if they're used throughout a codebase.

m_nThings: I automatically know that this object is counting the number of things as an integer.
g_p_somethingHorrible: The name implies a global pointer to something horrible.

...etc. The trouble arises when some parts of the code use prefixes religiously, and others use them occasionally, leading to fairly subtle confusion.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: degibson
Full-blown hungarian is pretty lame, I agree, but prefixes are extremely useful (and post-fixes, if your style guide prefers them) if they're used throughout a codebase.

m_nThings: I automatically know that this object is counting the number of things as an integer.
g_p_somethingHorrible: The name implies a global pointer to something horrible.

...etc. The trouble arises when some parts of the code use prefixes religiously, and others use them occasionally, leading to fairly subtle confusion.

m_nThings the "m_n" adds nothing. The fact that a variable is an integer is usually self evident from the code.

as g_p_ please let me know where you work so that I know to avoid any company that came up with a coding notation for one of the worst practices ever. Do you have a coding standard on how to use goto?
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Ken g6
While I don't agree with the specific prefixes used in Hungarian notation, I believe that the prefix concept in general is helpful in identifying a variable's contents. For instance, if you have a search box, there might be a label, a textbox, and a button. You can call them lblSearch, txtSearch, and btnSearch to differentiate them, while linking them all to the same area and function. Behind that, there might be a constant string for the label (especially if the label isn't often shown), which one might conceivably call cstrSearch. Or a better prefix might be cslbl (a constant string that goes in a label), especially if there's another string, which I might call csitxtSearch, a constant string to initialize the textbox txtSearch.

The point is, prefixes are useful, especially when you have several different objects related to one "thing".


Modern languages have come up with the concept of objects so that different things can be related together with.

Beside stop being lazy and type out the word.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: smack Down
m_nThings the "m_n" adds nothing. The fact that a variable is an integer is usually self evident from the code.
I disagree strongly. Lots of objects and primitives can have integer-compatible syntax. I generally think of the 'n' as short for 'number' (some folks just use num), and m_ for member variables is very valuable for figuring out side effects.

as g_p_ please let me know where you work so that I know to avoid any company that came up with a coding notation for one of the worst practices ever.

Use of globals should be made explicit somehow. The very naming of a global variable should invoke pain. Hence the g_. You'll find when you try to disentangle code from its original home that it helps a LOT when use of globals is explicit (if you're unlucky enough to have code that uses globals in the first place).

There's a similar argument for pointers to be made: more pain with p_, etc.



 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |