On Aug 20, 2013, at 8:59 AM, Martin Owens wrote:
On Tue, 2013-08-20 at 17:36 +0200, Krzysztof KosiĆski wrote:
Another problem is that the name chosen for this function is extremely long. I have no idea who invented the "Util" namespace, this should all be in the Inkscape namespace preferably as a global function (gratuitous use of namespace is a plague in our code), or what does "Quantity" mean. Ideally the code should look like this:
p0[X] = Inkscape::convert_unit(p0[X], "px", "in");
I think this is more expressive that p0[X] * IN_PER_PX.
Put this at the top of the code:
using namespace Inkscape::Util;
Then it /should/ be a case of calling:
p0[X] = Quantity::convert(p0[X], "px", "in");
Which makes a lot of sense. The only thing more convoluted would be:
p0[X] = convert(Quantity(p0[X], "px"), "in");
However... often "using namespace..." is considered not a best approach. A comparison is frequently made to "import foo.*" in Java code. Conceptually one should have a 'main' namespace for a single .cpp file, and that namespace could be designated by the "using namespace" directive. Alternatively, the same effect can be achieved by having an explicit "namespace foo {" block.
In this specific case, instead of "using namespace..." we can go with
using Inkscape::convert_unit;
So the using declaration, as opposed to the using directive, can be more helpful, calls out to the developer what might be covered in the file, avoids name collisions possible when more than one using directive is involved, etc. So a few "using xxxx;"'s instead of a single all-encompasing "using namespace yyy;"'s is usually a better choice.
On the other hand, keeping *something* in regards to a namespace explicit can significantly boost the legibility and ease of maintaining code. e.g.
std::string foo;
instead of
string foo;
(for this type of issue, it's very common to find warnings to not do "using namespace std". That's often employed for brevity in examples in class, etc., but should not be used in real code)
The main problem we hit in this specific case (as Krzystof called out) is that our namespace is too long.
Great news!!!! C++ already addresses that.
To make code in a specific C++ source file less verbose yet still keep it legible, we can just employ an alias.
namespace qnty = Inkscape::Util::Quantity; ... p0[X] = qnty::convert(p0[X], "px", "in");