0

std::unordered_map<std::string, std::unordered_map<std::string, float>> variable_name;

It's too long for one line. So, what is the recommended code style for this. I use the following now.

std::unordered_map<std::string,
  std::unordered_map<std::string, float>> variabl_name;
Steve Yang
  • 225
  • 2
  • 9
  • 3
    I would just put it on 1 line even though it's more than whatever you have as the character limit. With that said I believe this is an opinionated question and as a result off-topic. – drescherjm Oct 19 '21 at 14:52
  • using std; ... unordered_map< string, unordered_map< string, float > > variable_name; – Mark Lavin Oct 19 '21 at 14:54
  • 6
    @MarkLavin: If you're going to have a `using` statement anyway, make it a alias for the map type itself. `using namespace std;` should be avoided. – Nicol Bolas Oct 19 '21 at 14:55
  • 1
    Break it up in to multiple `using` statements where you can give meaningful names to each part. – Richard Critten Oct 19 '21 at 14:56
  • 1
    @MarkLavin See [here](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 19 '21 at 14:56

2 Answers2

5

Having good variables names is way more important than shortening the length of a line of code. Compromising the former for the sake of the later is not a good idea.

So if we don't want to touch the name of the variable, that leaves us with the name of the type. It's the main reason why the line of code is long in the first place anyways.

Type aliases are a good candidate here. You can use them to break things apart like so:

using float_map = std::unordered_map<std::string, float>; 
using my_map = std::unordered_map<std::string, float_map>; 

my_map variable_name;

Obviously, you should replace my_map with a name that describes the purpose of the type in your code.

Also, if you use the same types multiple times, the aliases can be defined only once at the innermost scope that encompasses all uses:

namespace my_project {

using float_map = std::unordered_map<std::string, float>; 
using my_map = std::unordered_map<std::string, float_map>; 

void some_function() {
  my_map variable_name;

  // ...
}

class SomeClass {
  my_map some_member_;
};

}
  • I agree. Also, set up clang-format. Use clang-format. Never worry about stylistic line-wrapping questions again. – Ben Oct 20 '21 at 02:35
0

Me personally, if needing to break, keeps all corresponding parts on the same level, i.e. in your case:

std::unordered_map<
    std::string, std::unordered_map<std::string, float>
> variable_name;

// or

std::unordered_map<
    std::string,
    std::unordered_map<std::string, float>
> variable_name;

If needing to break the inner map as well, then same pattern:

std::unordered_map<
    std::string,
    std::unordered_map<
        std::string, float
    >
> variable_name;

// or

std::unordered_map<
    std::string,
    std::unordered_map<
        std::string,
        float
    >
> variable_name;

Of course these last examples are just for illustration, I do with as few breaks as possible and go from outer to inner structures as long as needed.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59