Why does Terraform not provide if-statements - if-statement

Is there a deeper reason, why Terraform is not providing an if-statement to declare resources like this
resource "aws_ssm_parameter" "foo" {
if = var.create_foo # this is not available
name = "foo"
type = "String"
value = "bar"
}
To me this feels like a very common use-case.
I know that one can write
resource "aws_ssm_parameter" "foo" {
count = var.create_foo ? 1 : 0
name = "foo"
type = "String"
value = "bar"
}
but this feels more like a workaround. This is also not the end of the story. In addition to this less readable count-construct, one also needs to refer to the first parameter everywhere else using aws_ssm_parameter.foo[0] everywhere.
So my question is: Why does Terraform not implement such a keyword? Is there a reason this does not fit into the design? Is it just technically complicated to implement and might come some time soon?
I am honestly thinking about writing some kind of terraform-pre-processor which would automatically replace all if-statements by the corresponding count statement as well as all references to this resource with an added [0] (or something slightly more robust). Any reasons that might not be a good idea?

Apparently there is simply not much interest in such a convenience feature at the moment. There is a issue created for Terraform, that is already quite old and barely voted for.

Related

Modern way to access struct fields by name in C++

I wanted to check if there's an intuitive and easy way to access struct fields by name in modern C++.
I am aware that similar questions have been asked and answered, and C++ reflection is a well investigated subject.
I've came across libraries like:
boost-hana
boost-reflect
visit_struct
magic_get:
But the common point in all these approaches is that, they only allow you to get the total number of fields within the struct or do a certain operation in for_each manner for all the fields of the struct.
Yes, I can obviously check for the specific "name" of the field I'm looking for, by using the for_each functionality provided by these libraries. But I just wanted to check if there is any other trivial/well-know library that already does this.
I would like to be able to deal with arbitrary number of nested structs, which is why I'm looking for something out of the box.
As Louis Go indicated, it would be great to have an accessor like:
auto field = namespace::getField<mystruct>("fieldname");
You can access class members by name using the member access operator. Example:
struct foo {
int bar;
} instance;
instance.bar = 42; // access by name
If you mean to access a member based on a string variable rather than by compile time identifier, then no. C++ still as of C++20 does not have reflection features necessary to achieve this.
Quite often when programmers want this, what they actually need is an associative container such as std::map.

What kind of pointer should I use here?

I'm working on an old large code base with a colleague. The codebase uses a significant number of std::shared_ptr and previous developers had a fondness for long property names (m_first_username for example).
Some methods in our code access a number of those properties so our code tends to be very verbose:
if (m_first_username->isSomethingOrOther() || m_second_username->isOtherOrSomething()...
So to make the code more readable my colleague wants to use more std::shared_ptr & with local scope:
const std::shared_ptr<...> &tmp = m_first_username->returnsASharedPtr()
tmp->isSomethingOrOther();
Something I disagree with because of the shared pointer use count.
What is the best way to make this code more readable? Keeping using constant references to shared_ptr, use std::weak_ptr or live with the long lines of code?
As per #nwp's comment -the proper way to alias a variable name locally would be:
auto& v1 = m_first_user_name;
If you want to go the route of the "returnAsSharedPointer" you posted in the question, what you'd want to use in the classes of m_first_user_name and m_second user_name is the standard C++ enable_shared_from_this.
On the whole, though it's primarily opinion-based, I believe you'll find that most experienced C++ developers will find the new code less readable than the old code. There is nothing wrong with long, descriptive variable names.

How do I add an if statement to the postcondition of a schema?

I'll simplify things for this scenario (It's in Perfect Developer, it gets complex quite quickly). Let's say I have a simple schema in my class, called Succeed, which takes a Course (which is a previously defined class) as parameter.
Basically, I want to be sure that the course is in my courses set as a precondition, and then add it to my coursesCompleted set in my postcondition. This simple schema works great, and looks like this :
schema !Succeed(c:Course)
pre
c in allCourses
post
coursesCompleted! = coursesCompleted.append(c);
However, I want to add a quite simple if condition: If my coursesCompleted cardinality is 30 or more, I want to set a Diplomation enum to, let's say, "Ok". If the cardinality is less than 30, I will set it to "NotOk"
According to Perfect Developer's documentation, and all the rare examples I've seen, the if syntax should look like this :
if [condition1] : do stuff;
[condition2] : do other stuff;
fi
However, if I plug that directly in my schema, as is :
schema !Succeed(c:Course)
pre
c in allCourses
post
coursesCompleted! = coursesCompleted.append(c),
if [#coursesCompleted >= 30] : diplomation = Ok#DiplomationEnum;
[#coursesCompleted < 30] : diplomation = NotOk#DiplomationEnum;
fi
it does not work, I always end up with a "very descriptive"
Error! Syntax error at keyword 'if', expected one of: '!' '(' '?'
'c_address_of'
I've tried adding some ; everywhere, adding a via keyword after the post, changing it's position, trading ;s with ,, and a lot of other trial and error stuff.
So my question is: How can I add a if condition to a postcondition of a schema, in Perfect Developer?
Please answer in Perfect Developer. I (sadly) know my formal methods, I only need the if to compile in the worst tool in the world.
I can provide a solution in formal-methods using the zet notation, since I am not using Perfect Developer, however, that program should be based on formal-methods. As far as I know in formal methods, if conditions are not used when the system has different behavior over some preconditions, but rather you create 2 methods with different
preconditions(the way i implemented it) or a nested method covering both scenarios, seperated with a Logic OR in between. When it comes to error handling, you first provide a best case scenario and then define a robust method definition(schema calculus).
In formal methods the notation ! is used to describe the output while ? is used for the input.
I hope that this will help you understand possible issues, since its not a direct perfect-developer solution. However, considering the fact that in formal methods mathematics are used, you can use the following specification and jump to any program/language for the implementation part.
Note that: Anything that you will see in the following schemata on the preconditions/how the system changes part, is connected with a logic AND, however I am not using the symbol because is implied as far as a logic OR is not used.
So here is how it will look like in formal-methods(Z-notation).

INotifyPropertyChanged Setter Style

In order to reflect changes in your data to the UI you have to implement INotifyPropertyChanged, okay. If I look at examples, articles, tutorials etc most of the time the setters look like something in that manner:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField == value)
{
return;
}
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
No problem so far, only raise the event if you have to, cool. But you could rewrite this code to this:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField != value)
{
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
}
It should do the same (?), you only have one place of return, it is less code, it is less boring code and it is more to the point ("only act if necessary" vs "if not necessary do nothing, the other way round act").
So if the second version has its pros compared to the first one, why I see this style rarely? I don't consider myself being smarter than those people that write frameworks, published articles etc, therefore the second version has to have drawbacks. Or is it wrong? Or do I think too much?
Thanks in advance
I find the second example more readable personally. I think one of the reasons the first example has become widespread is that Resharper will prompt to use this style (and automatically do the conversion), with the rationale that it "reduces nesting". Which is correct, but in simple cases like these I think readability is more important.
It comes down to a fundamental difference of opinion - there are those programmers out there who think that there should only ever be one "return" - one single exit point at the end of the method. Then there are those who think that there should always be an "early exit" if at all possible, which can lead to multiple "returns" throughout the method. Which do you prefer? :)
I see the second style much more often than the first... but anyway it doesn't matter, the behavior is exactly the same. And no, I don't think there is any drawback with the second approach. It's just a matter of personal preference, choose the one you find most readable.
Same as Thomas Levesque. I use myself the second one in my code, and I have seen it quite often. Both approach should perform the same.
It reduces nesting.
In your example, its pretty clear, and its just a matter of taste, but it is much clearer when used in consecutive manner, as you can see here:
Invert "if" statement to reduce nesting
Usually I don't care if I get an event when the value is the same, so I leave that part out:
public string MyProperty
{
get { return _correspondingField; }
set { _correspondingField = value; OnPropertyChanged("MyProperty"); }
}

C++ strcpy(Struct.Property, "VALUE") Use in C#?

i created a Wrapper for a C++ dll. While reading the documentation
i reached to a point using this function strcpy(StructName.strPropGetter, "A STRING");
I'm not kinda C++ guy, i can't figure how to transfer this code in C#.
My wrapper gave me this property without a setter.
Any light would be nice. Thank you
It's simply StructName.strPropGetter = "A STRING";
edit
If you mean how should you implement strPropGetter, then this is difficult wihout any information on what strPropGetter is. But it may be something like:
class StructName
{
string strPropGetter { get; set; }
}
StructName.strPropGetter = "A STRING";
(This is a literal copy of the names to make it easier to see how it relates to the original snippet of code, but obviously "strPropGetter" etc should be named something more sensible. The "Getter" in the original name possibly refers to a "get" mehod for a property, in which case, this is generated automatically by C# for the "get;" part of the property in the code above.
I can't really help much more without a better idea of what code you're looking at wrapping/converting.