It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a question regarding using some features from the System.Linq.Dynamic assembly.
I needed using queries like "#"NOT (Person.Name = ""test"")" for example, now the problem is that I do not have a certain object type, but instead need reading the property names and their values (and may be types, though I actually must deduce the types from the values) ... I solved this by using reflection (created the type and the properties as needed at runtime) .... but I still wonder whether that is possible without creating the types and properties, but using anonymuous types (I basically need to not have to enter those types, just the values ... of course I can write code to find out the type of the value entered for ex it has quotes - that means it's a string ...), or if there is a another library in .Net for accomplishing this task (I did not have very much time for looking into the Dynamic class ... how it's working etc)
.
The Dynamic Linq parser always requires an actual type to parse against because it is parsing the dynamic expression into System.Linq.Expression expression trees, which are based on types. However, the Dynamic Linq library contains a method for quickly creating anonymous types like this at run time. Here is an example of using that (taken from the html documentation file that comes packaged with the DynamicLinq.cs file):
DynamicProperty[] props = new DynamicProperty[] {
new DynamicProperty("Name", typeof(string)),
new DynamicProperty("Birthday", typeof(DateTime)) };
Type type = DynamicExpression.CreateClass(props);
object obj = Activator.CreateInstance(type);
t.GetProperty("Name").SetValue(obj, "Albert", null);
t.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null);
Console.WriteLine(obj);
Related
This question already has answers here:
What is this "err.(*exec.ExitError)" thing in Go code? [duplicate]
(2 answers)
What is the meaning of "dot parenthesis" syntax? [duplicate]
(1 answer)
Closed 5 years ago.
Go Newb here -- I've encountered the following bit of Go code that I didn't write
if tc, ok := tng.(ThingClasser); ok {
//... do some stuff ...
}
I won't understand the semantics of tng.(ThingClasser).
In some ways this looks like a method call -- i.e. there are two variables (ec, ok) sitting there ready to accept multiple return values.
However, tng.(ThingClasser) itself looks like its a property access, not a method call.
However however, the parens around ThingClasser are a wrinkle I've never seen before. Also, if it matters, the ThingClasser symbol is defined elsewhere in this project as an interface, so I think maybe this is some syntactic sugar around an does this implement an interface -- but then the two return values confused me.
Googling hasn't turned up anything concrete, but this is one of those hard things to google for.
Does anyone here know what this call/syntax is in GoLang, and possible point me at the relevant manual pages so I can RTFM?
It's a type assertion. The returned values are 1) the object, converted to the given type; and 2) a boolean indicating if the conversion was successful. ThingClasser is the type being converted to. Documentation can be found here: https://golang.org/ref/spec#Type_assertions
This question already has answers here:
How can I build an enum with Dart? [duplicate]
(4 answers)
Closed 8 years ago.
A simple question here :) I'm really happy to see a new feature in the dart language. But I just realized that I kind of never use enumeration.I don't know if there is a discussion somewhere about that but.
What is the pros and cons of this feature in terms of code writing (seems shorter), performance,etc?
Cheers
If I understand it correctly an enum is like a class with const members and some useful methods. Given that cost members are resolved by the compiler using an enum should not incur in any performance hit.
In terms of "code writing" enums are good candidates to replace classic const or static enumerations or, even better, hard-coded constants.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a if (a > b) { .. in my code, which I would like to explain why is there to someone looking at the code.
The reason for it being there is quite complicated and cannot be explained well without the use of images.
The question is, how to best document this to a programmer looking at the code? Is there a generally accepted way?
Should I create a comment in the code saying "See explanation XX", and then create a document somewhere containing XX?
Much of this will depend on what you are coding and the size of your project, but generally I would say that you should comment this particular conditional with an explanation only for what if (a > b) { .. is checking for, and why.
When you get to the content inside the if condition, explain that. Broader explanations as to the purpose of the function itself and its objectives should generally be in the declaration, although you can also add descriptions to the definition (I prefer to avoid this generally, although I sometimes describe the method in further detail on top of the definition, where it would simply clutter the declaration of the class).
For example,
class A
{
// this method performs operations on x or y as appropriate, given the input conditions of
// a and b. Its purpose is ... and in order to achieve this it does ...
void Method(int a, int b);
};
// elsewhere in a .cpp file
// Note that this method uses method abc rather than method cde in order to achieve
// such and such more efficiently (description here is more technical than in the
// declaration, and might focus on more specific issues while still remaining
// applicable to the function as a whole, and should therefore not be in the body)
void A::Method(int a, int b)
{
// check to see whether or not a > b in order to decide whether to operate on x or on y
if (a > b)
{
// a is greater than b, and therefore we need to operate on x because...
}
else
{
// a is not greater than b, therefore we need to operate on y because...
}
}
I find that by structuring my comments to address the reason why specific sections of code are the way they are, the reader is able to "follow the story" that the code is telling as she reads through it.
If it is absolutely impossible to describe what the if section is doing without a broader explanation, then by all means add a paragraph or two. There is nothing wrong with long comments as long as they are well placed and address the specific purpose of the following lines of code. You shouldn't need to add for more information, see function header because that should already be implied.
You can add broader descriptions to the enclosing function, method, or scope, but comments should always address the piece of code they are referring to as succinctly as possible. After all, if the reader wanted to know what the whole function was doing, she'd look at the declaration. The reason she's looking at the definition of the function is because she wants to know what the components of the function are doing, and so the comments should address just that.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
Are there any for purposes other than web? e.g. for use in script generators, c++ code generators or other related, generic operations.
Have a look at http://velocity.apache.org/ or http://nvelocity.sourceforge.net/
In Visual Studio there is a templating engine primarily for code generation, called T4.
Here is an entry point for documentation on MSDN.
So, to answer your question, yes they to exist for other purposes than the web.
Yes there are many out there. One that I know of that allows you to generate more templates if you need them is MyGeneration. Another, is you could always build your own xslt template engine then you could build whatever you need. However there are several opensource and commercial code generators.
E.g. StringTemplates is a Java-based template engine for generating all sorts of text artifacts, and model generator frameworks like openArchitectureWare (or GeneSEZ) use the Expand template engine.
Imatix GSL is the most impressive (and simplest) of the tools that I have encountered. Plus it has been used to generate large amounts of complex code.
Also, lua is a programming language whose initial purpose was data definition, and I have found it to be very capable in this area. So, you define your data in lua and you execute the data definition files (valid lua programs) and you can generate any code from it.
Consider the following model for a C-function in Lua.
> func {
> name { "xyz" }
> parameters {
> { name= "x" , type="uint32_t" } ,
> { name = "y" , type = "uint32_t"}
> }
>
> ret { type="uint32_t" }
>
> psuedocode {
> "getLock(lockName)" ,
> "getSessionMemory" ,
> "addSession" ,
> "releaseLock"
> }
> }
They can generate configuration files.
Puppet and Chef rely on erb templates a lot, for example.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have an interface in C# that helps retrieving of data from a custom archive on server. The interface looks like this:
public interface IRetrieveData
{
bool OkToRetrieve(SomeData data); // Method in question...
bool RetrieveToLocal(SomeData data);
}
This interface is implemented by the clients that retrieve the data to the local database. There are different flavors of clients that have access to each others data. So, when the processing component calls IRetrieveData.OkToRetrieve right before actual retrieve, the call goes to the client code where the decision is made on whether the data should be retrieved or not.
At this point the client can return false and that piece of data is skipped or return true and the processing component calls RetrieveToLocal and send the data to the client which then processes it.
Where I am getting confused is whether to rename the method OkToRetrieve to just Retrieve or CanRetrieve or leave it as OkToRetrieve.
Does anyone have any suggestion?
IsRetrievable()
I think that a method that returns a boolean value should be named as a yes-no question.
Allways name boolean methods with names similar to questions that can be answered Yes or No.
In your case, CanRetrieve would be a good name (just to use your own suggestion).
How about using the prefix 'should'?
ShouldRetrieve(SomeData data);
Methods mean action. Therefore, I prefer method names to start with a verb. How about?
CheckIsRetrievable(SomeData data)
Generally, methods/functions indicate actions so they should prefixed with verbs. e.g. check, get, make, etc.
The common naming convention for boolean variables is to prefix them with helping verbs e.g. is, does, will, can
I would think that the combination of the two conventions would lead to a pretty good, discerning pattern. So getIsRetreivable() or checkIsRetrievable() would look pretty good to me.
Depends on your use case. I like to prefix them with words such as 'is', 'does' or 'Can':
IsSomePropertySoAndSo, DoesNounSupportFeature and as your example CanVerb
In this specific case, I'd probably name it:
public bool IsReady (SomeData)
Because it more clearly demonstrates what will happen once this returns true.
In Naming conventions, it is written that method name should be verb
if you are doing more checks and isRetrievable() isn't appropriate you could use:
IsValid()
CanRetrieve sounds fine to me. I've seen the Can stem used in Microsoft APIs. The only other real option IMO is IsRetrievable (from Aziz) which somehow seems too linguistically twisted!
I would prefer isOKToRetrieve or isRetrieveOK over variants without "is" under the convention that functions and methods should be verbs.
MayRetrieve() could be a better name if the result is determined by the user's permission/access.
IsRetrievable() is more ambiguous, which may be more appropriate if there are other considerations in addition to, or other than, permission.