You are not logged in.

#1 2008-08-06 18:33:21

pogeymanz
Member
Registered: 2008-03-11
Posts: 1,020

Which is worse: awkwardly long or vague/arbitrary variable names?

I'm programming in C++, but that shouldn't matter.

I'm working on a small chunk of code that is a module for a much, much larger project. Part of my code is to make several hundred histograms and then run some statistics over them.

My dilemma is that there are so many, that naming them is becoming difficult. So, I opt for incredibly long, cumbersome names rather than short arbitrary names. Heck, even I wouldn't know what was going on if I just named them "histo1, histo2." Instead I name them things like "MomentumTimesDeltaPhiEta1" which MIGHT make sense to someone who is working on another part of the project.

And actually, that wasn't a real example. Some of my names are longer than that. Is that normal? I have programmed in C++ for a year, but I'm still not good at programming etiquette, so my code looks clumsy.

Offline

#2 2008-08-06 18:47:42

freakcode
Member
From: São Paulo - Brazil
Registered: 2007-11-03
Posts: 410
Website

Re: Which is worse: awkwardly long or vague/arbitrary variable names?

Local vars can be short, if you find the need to give further explanation, comment. Try to give meaningful names more for class atributes, interfaces, and where a variable name can help documenting the code itself. E.g. (Pseudo-code, as I don't remeber a lot of C++):

Instead of:

z = sqr(a^2 + b^2)

This is better:

// Pythagorean theorem
z = sqr(a^2 + b^2)

This is even better:

hypotenuse = sqr(catheti_a^2 + catheti_b^2)

If you find yourself using incredible long variable names, it's a signal that your program is too flat, and you need to start refactoring it into a function, class, or using a namespace.

So, e.g:

namespace Geometry {
    function Hypotenuse(float a, b) {
         return sqr(a^2 + b^2)
    }
}

The code expresses a lot more meaning, while staying concise.

Edit: I forgot square root big_smile

Last edited by freakcode (2008-08-06 19:49:58)

Offline

#3 2008-08-06 19:40:55

pogeymanz
Member
Registered: 2008-03-11
Posts: 1,020

Re: Which is worse: awkwardly long or vague/arbitrary variable names?

Thank you. That does make a lot of sense. In the future I will try using those techniques.

Offline

#4 2008-08-07 04:56:52

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Which is worse: awkwardly long or vague/arbitrary variable names?

You need to have several hundred histograms? This sounds like a job for a data structure. How about using one of the containers in the STL to store histogram objects, each of which has a name slot and a data slot? Any time you find yourself making hundreds of variables that store the same type of object, you need to cut it out and put them into an appropriate data structure. Actually ten variables would be too many.

In general, I'd rather see over-long variable names than meaningless ones—at least then I know what the code was intended to do, making it a lot easier to debug. But if I understand what you're doing, you need to stop doing it right away. Is it even necessary that the histograms have names? If they're used only for statistical purposes, could you just create a list and add hundreds of nameless histograms to that?

Last edited by pauldonnelly (2008-08-07 04:57:39)

Offline

Board footer

Powered by FluxBB