What Are the Hallmarks of Good / Bad Code?

Page 4 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

brianmanahan

Lifer
Sep 2, 2006
24,637
6,016
136
do not use underscores in variable names ... that is bad code!

(semi serious about this too, i can't stand it - camel case ftw!)

i am a java programmer, but i honestly find underscores to be more readable

i dont do it in production code, but use it in test case method names

what is more understandable?

1. public void calculatePremiumReturnsCorrectFactorCoefficients()

2. public void calculatePremium_returns_correct_factor_coefficients()

i vote for #2
 

Sgraffite

Senior member
Jul 4, 2001
203
140
116
i am a java programmer, but i honestly find underscores to be more readable

i dont do it in production code, but use it in test case method names

what is more understandable?

1. public void calculatePremiumReturnsCorrectFactorCoefficients()

2. public void calculatePremium_returns_correct_factor_coefficients()

i vote for #2

I prefer #1 myself. I used to use #2 but I have since changed my tastes I guess. At work we use StyleCop to enforce variable naming conventions so the code is consistent, which is probably more important than #1 or #2. I imagine there is a similar plugin for the more common IDEs that support Java but I have never looked.
 

Schmide

Diamond Member
Mar 7, 2002
5,745
1,036
126
Which version are you using? I'm using 2013 Express. If I type this:
Code:
if (condition) {
and hit enter, it automatically turns into this:
Code:
if (condition)
{
All of the settings are on default.

As much as I understand your qualms with this, I understand why the later is preferable. When you're editing code, it is advantageous to have your brackets as far to the left so when you ctrl-[] , you don't have to press the end key to get to the starting bracket.

However, as I edit and mimic other peoples code, it doesn't slow me down much.

Some other keys everyone should be using.

ctrl-ml and ctrl-mm

Try it. You'll like it,
 
Last edited:

purbeast0

No Lifer
Sep 13, 2001
53,664
6,546
126
i am a java programmer, but i honestly find underscores to be more readable

i dont do it in production code, but use it in test case method names

what is more understandable?

1. public void calculatePremiumReturnsCorrectFactorCoefficients()

2. public void calculatePremium_returns_correct_factor_coefficients()

i vote for #2

#1 is easier on the eyes for sure, also easier to type.
 

brianmanahan

Lifer
Sep 2, 2006
24,637
6,016
136
#1 is easier on the eyes for sure, also easier to type.

no way is #1 easier on my eyes than #2. all that stuff runs together.

its not so bad for a class name with 1 to 5 words in it, but i dont like it for sentences
 
Last edited:

cytg111

Lifer
Mar 17, 2008
26,320
15,734
136
Hello,

I'm new to computers, and at this point my metric for what constitutes good programming is functionality. That is, if a piece of code simply does what it's supposed to, in my eyes it's good.

However, I'm aware enough to understand that even though code may be functional, it may still be poorly constructed.

So, experienced programmers, how do you know when looking at source code whether or not the writer knew what he was doing?

What are the hallmarks of good programming? What are the hallmarks of bad programming?

Can you recommend certain coding styles to emulate? Can you admonish me from emulating others?

Thanks.

Bad code == spaghetti or over-engineered, equally frustrating to deal with.
Good code == KISS and documented in code if you are doing something you have to think twice about.
 

cytg111

Lifer
Mar 17, 2008
26,320
15,734
136
i am a java programmer, but i honestly find underscores to be more readable

i dont do it in production code, but use it in test case method names

what is more understandable?

1. public void calculatePremiumReturnsCorrectFactorCoefficients()

2. public void calculatePremium_returns_correct_factor_coefficients()

i vote for #2

I am on #3 .. at this level I dont give a flying .. they're equally acceptable to me.
 

brianmanahan

Lifer
Sep 2, 2006
24,637
6,016
136
I am going to print this out and put it up at work. LOL!

lol yeah i just heard it at codemash and laughed for a good minute when the guy said it. i think he might have said it better though, maybe something like:

"code is like a joke - if you have to explain it, it's not very good".
 

reallyscrued

Platinum Member
Jul 28, 2004
2,618
5
81
There are contexts in which all-caps is not only unobjectionable, but increases comprehension. Specifically, case-insensitive languages like SQL. The reason that the upper-case convention was adopted for SQL keywords early on is simply that when the language is not case-sensitive you end up with everyone using their own personal approach, i.e. select, Select, and SELECT.

True, there are contexts in which all caps can aid in comprehension, such as in street signs...advertisements, examples that Ken gave - but him implying that carries over to every other aspect of typography is not true, especially not in most coding environments.

But as you point out, certain case insensitive languages such as SQL prefer keywords in all uppercase, and guess what? The designers of the code editor of SQL Management Studio went ahead and added a lot of spacing on uppercase key words. It's anticipated, and that's what makes it easy to read.

Below is a comparison of the DECLARE keyword typed out at 100% zoom, all fonts default in Visual Studio 2013 vs SQL Management Studio 2008

pic.png


I'm not sure how anyone can defend Ken's claims that all uppercase is always easier to read and shouldn't ever bother anyone because of street signs and the such - that's a gross oversimplification.

There *is* something wrong in using all caps when not convention, you're just making it tougher for everyone else if you don't see why.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,711
4,671
75
I'm not sure how anyone can defend Ken's claims that all uppercase is always easier to read
I did not claim this. Though I find all uppercase no harder to read 95-99% of the time.

There *is* something wrong in using all caps when not convention
I can agree with this. Especially when the convention is to not use all caps.
 

smackababy

Lifer
Oct 30, 2008
27,024
79
86
Which version are you using? I'm using 2013 Express. If I type this:
Code:
if (condition) {
and hit enter, it automatically turns into this:
Code:
if (condition)
{
All of the settings are on default.

I have Visual Studio Express, whatever the latest version is before it went completely free with the open source announcement.

I can understand them "forcing" the second option, as it is easier for readability if you're used to the former. While, I believe the { is implied, there are plenty of times I've not used them at all. Forcing it on the line below, notifies me immediately there is more encapsulated for this statement than a single line.


And for those arguing that having serifs on I's and 1's makes it harder to read all caps, you need to get your eyes checked and update your glasses. It isn't.