As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've been trying to follow a loose TDD workflow for one of my open source projects. It's an API for other programmers to use.
As such, one key aspect as well as making the API "work" is also designing how it will be consumed. I've heard some people say that writing tests before they'll compile is a waste of time and prone to constant rewrite until the API is stable. I've also heard that it should follow a workflow like so:
Write the tests which won't compile
Make it compile
Make it green
I've been trying to follow this workflow, but I end up with some weird things. For instance, in my API I have these two methods:
Handles(string pattern); //had this one already
Handles(IPatternMatcher pattern); //needed this one
I needed to get the second form of the method added to my API. So, I ended up with a dead simple test like so:
public void Handles_SupportsIPatternMatcher()
{
var api=new MyAPI();
api.Handles(new TestPatternMatcher());
}
Which seems like a waste after it gets implemented.
Should I continue following this workflow, or are there ways to improve it? How do I keep from writing tests that basically just check for compiler errors? Since it's a publicly consumable API, should I worry about tests like this?
No.
Do not write code that tests whether the compiler is working. Those kind of tests make a lot of sense if you are using dynamic languages (or dynamic features in a static language), where they will actually tell you that you forgot something, or refactored something into a failing unit test.
The point of the unit test execution is to fail the build if it is in error. If you have a compiler error in your code, the build will already fail. There is no need to second-guess it.
I you use resharper you can create the empty Handles method which will get IPatternMatcher. TDD is powerfull thing, and you should continue trying. I was trying both of ways test before code and test after code, and I found that test before code is powerfull thing. You can debug code errors very quckly. And test is warranty that your code will work as you expect.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Is there any special jargon word for a class that has no functions but is used to store data?
One of the examples is Data Transefer Object (DTO), although it, of course, still can have methods.
Plain old data structure (POD) seems to be an appropriate term. Though rarer than POJO/POCO, from what I've seen, it seems to be the best fit for your criteria.
There is no standard term for C# because this practice is pretty rare. I call such classes (or structs) "records", for no particularly good reason.
I see a lot of flamewars and a high rated incorrect answer here. So I'll chime in with my not entirely correct but close enough answer.
A JavaBean is a special data encapsulation object in Java. In C# I'm not entirely aware of the name but they do have a structure (rather than a class) which I'm accustomed to using for similar types of tasks.
Another term you may wish to use is Entity. Java has "persistance entities" which are effectively JavaBeans with an annotation. My advice would be to be consistent with whichever you choose to use.
As mentioned this isn't a perfect answer but it should be close enough.
http://en.wikipedia.org/wiki/JavaBeans
http://docs.oracle.com/javaee/5/tutorial/doc/bnbqa.html
http://msdn.microsoft.com/en-us/library/vstudio/ah19swz4.aspx
http://msdn.microsoft.com/en-us/library/system.data.entity(v=vs.103).aspx
I mostly refer to them as container classes. Maybe the term you're looking for, because it doesn't sound very functional. But they often have getters/setters.
Utility class is also a nice term. Utility class which stores xyz data for use with bla.
Do you mean something like this?
public class Foo {
public int a;
public String b;
}
I don't think there's a specific term for a (public) class like that in Java. Except maybe "bad practice".
If your platform has a decent JIT compiler, there's no good reason to write code like that. At least make the fields private and provide getters and/or setters. A decent JIT compiler will optimize simple getters and setters so that there is no performance overhead.
The key point is that you should never let code like that appear in a API that is exposed outside of a single compilation unit. Why? It exposes the implementation details of the class and forces other code to depend on them.
If the class is an private inner class the above code could be reasonable, though I'd be more comfortable if the fields were final and there was a constructor. Especially if the compilation unit was large.
Your question is tagged C# and Java, but I've found most people understand if you call them structs (from C).
Note that in C++, structs may have functions too, but I don't think this is idiomatic.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've developed a website (jQuery + .NET/C#) and it's 90% ready. Before a publish it I decided to face the truth and I admitted to myself the code isn't totally hack-safe. To be honest it's barely hack-safe. It's because I've been practicing some bad web programming habits (like relying too much on client scripting instead shelling it into server-side).
To be more precise, I've being using too much jQuery I guess and now I see all my webmethods are exposed in .js files. I've minified and obfuscated but it doesn't prevent a determined cheater to get all my stuff. That's one point.
Other issue is the fact I have some webmethods that return a lot of data from my database. Not confidential data, but some nice data my system generates and I don't wanna any external website to call it and display it like it is their material.
One of solutions I thought about was to generate all html in server-side and return it to .js and then append it to his respective element (in this way I'd hide all the intelligence beyond it) but it doesn't solve all the problems I related above.
So my question is... do you guys think it's safer to rewrite everything using ASP.NET controls or is there any way to make it REAL SAFE using the code I have (like avoiding external callings to my webmethods)?
I know without looking to the code it's hard to make an opinion but it's huge! And it's all about what I said (too much client-scripting, little server-side).
Thanks in advance.
First of all, you can check on serverside, which domain is calling your backend -> deny for unallowed domains. (Check this ticket)
To reduce javascript magic, you can move the logic into backend as you said.
Maybe some tricks to save your javascript code ( Check this out )
If you want a totally hack-safe Site, then you will be disappointed. you can just make it the hardest possible.
This is easiest with sites processed server-sided, and then served to the client. but i would also recommend not moving all to the server, because after all the server will have to process each and every request you make to it.
I have experienced, that it is the "easiest" to make all data-serving and evaluation logic serversided and rendering, as well as easy processing logic client-sided. Thus combining the strong sides of both technologies at minimal security risk. e.g. after an AJAX-Call to serve some data (if you even want to do that) you can call render, update and processing logic client sided with the help of success: or failure:, as well as after: or similar keywords.
Try some new things out. if you move it all to one technology/process you will have to take the disadvantages of this process. split things up!
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Can someone recommend a simple c# code generator. I just looking something with methods like:
GenClass = CreateNewClass(AccessModifier,Name......)
GenClass.Add(new Method(AccessModifier,RetType,Name....){code=#"....."}
GenClass.Add(new Property(AccessModifier,Type, Name....)
...........
etc
and after creating all classes\methods and other members we call Code Generation function(where we can specific some parametrs)
Is there such opensource code generator?
Check out Using CodeDOM to generate CSharp (C#) and VB code.
You may want to have a look csscript that relies on CodeDOM.
It allows you to write things like:
var PrintSum = CSScript.LoadMethod(
#"public static void PrintSum(int a, int b)
{
Console.WriteLine((a+b));
}")
.GetStaticMethod();
PrintSum(1, 2);
Be sure to read the doc, it's pretty detailed and you'll find you can do a lot more than what I just copied before.
T4 or Text Template Transformation Toolkit might be worth looking into.
Another option is to create your own simple generator, which contains functionality more suited for your situation than the CodeDOM. In a recent code generation project that's what I did, however I have encapsulated the code generation to make it possible to later transition to CodeDOM.
If you want to be able to generate a class given some arbitray string containing C# code, you need a C# compiler. At the moment the .Net framework does not ship with a compiler that you can pass snippets of C# to and get compiled code back. If you have more specific needs, you should specify exactly what you're looking to do.
Since you explicitly searching for an opensource code generator I suggest MyGeneration. Another, template based approach (which is not what you are looking for since want "GenClass.Add...." syntax rather than templates) would be Codesmith Tools it's really powerful but closed source.
take a look at my open source generator http://code.google.com/p/magicapps/
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I recently applied for a developer position and the director there asked me to send some samples of code.
How should I approach this? Once, I sent a sample of code that I wrote for myself to a company and they didn't get back. This time, I want to be prepared and send appropriate samples. I want to know what I should send them, create a sample website/code it and send a link along with code files, or create a sample project, or some other approach.
I cannot send code that I have written for my previous and current employer, so I'm not sure what to send.
They asked for C#, HTML, CSS, JS, SProcs, triggers samples, so I thought: would it be nice if I create a project that includes all the above and send it to them, or should I send individual samples?
You've pretty much answered your own question. Why not create a project including all of the above? Small games are fun to write and you can demonstrate a lot of knowledge by creating one. Websites are good too, if you want to demonstrate things like good UI design and dynamic HTML and scripting knowledge.
It's essentially your portfolio to demonstrate what you're capable of, so apply all your knowledge. This includes not just what the program does but also how it does it -- good choice of algorithms, code architecture, proper naming of variables and appropriate comments, etc.
You can write code that's of just as high quality as what you'd create for an employer -- higher, in fact, since you have complete control over the methodology and standards used. Install one of the free revision control systems (git, mercurial, subversion etc), use bugzilla to track your bugs and todo list, and write a full suite of unit tests. Produce professional documentation and demo screenshots.
Yeah, that's a lot of work. But I can guarantee you that if such a project crossed my hiring desk, I'd have you in for an interview in a hot minute.
I've been in this situation before, and I usually just refuse to send them the code samples.
The reason is this: when I work for an employer, I'm working professionally, and the employer has exclusive rights to all the code. If I tried to copy the code and send it to another potential employer, I'm at risk of major intellectual property theft. On the other hand, if I create some code for my own use, it is by definition amateur code (even if produced by a professional), and doesn't have the same level of project management stricture, test stricture, etc., and is therefore not representative at all of my professional coding skills.
Companies that ask for this are effectively asking for the impossible. I find it best to point this out to them.
I'd take something I already worked on - preferably three-tier including sprocs, etc., and factor out anything of a sensitive nature. If it's something you wrote yourself then you will be comfortable discussing it and able to show the breadth of your knowledge - that's what will land you the job. A complete project shows that you know how everything fits together. One more thing: if they ask you where you go to find good code samples, do not immediately say Google: tell them you would look in their code base ;-)
Besides what you send them (a project, a website), I suggest you also give some thought as to what you want to convey with it, and give a written introduction to the sample: why you think it's an interesting piece of code? What does it showcase? What pieces are you proud of? What were the challenges?
They should be able to figure that out themselves, but there is no harm in helping people see what you want them to see...
WHen I intervoiew people I often ask them to give me some code. Usually I asked them to code infront of me though but I guess my point would be the same if the send it to me. I don't look at what they've done (the functionality) but how they've done it. What design principles guide their work. How tidy is the code. What's their test strategy. Have they used any patterns and if so are they used in a well thought way. Depending on the language I'd look for specific aspects of the language. In c++ I'd love to see them be capable of using template specialization elegantly (E.g. for traits/policies) in c#3.0 I'd look for propper use of lambda expressions, generics, LINQ I'd be looking for both correct use as well as over use.
Hope it gave you an idea of what kind of scrutiny you might be put through
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Does anyone know if there is a c# Console app, similar to the Python or Ruby console? I know the whole "Compiled versus Interpreted" difference, but with C#'s reflection power I think it could be done.
UPDATE
Well, it only took about 200 lines, but I wrote a simple one...It works a lot like osql. You enter commands and then run them with go.
SharpConsole http://www.gfilter.net/junk/sharpconsole.jpg
If anyone wants it, let me know.
Given your mention of "C#'s reflection power", I am unsure whether you're looking for an interactive C# console for small code snippets of your own (à la Ruby's irb), or a means of interacting with an existing, compiled application currently running as a process.
In the former case:
Windows PowerShell might be your friend
Another candidate would be the C# shell
Finally, CSI, a Simple C# Interpreter
Found this on reddit: http://tirania.org/blog/archive/2008/Sep-08.html
Quote from the site:
The idea was simple: create an interactive C# shell by altering the compiler to generate and execute code dynamically as opposed to merely generating static code.
If you don't have to use the console, and just want a place to test some ad hoc C# snippets, then LinqPad is a good option. I find it very cool/easy to use.
I am not sure what you are looking for this application to accomplish. If it is just to try some code without having to create a project and all the overhead to just test an idea, then SnippetCompiler could be a good fit.
I just wanted to give you another option.
It appears Miguel De Icaza was stalking me:
http://tirania.org/blog/archive/2008/Sep-08.html
Google reveals a few efforts at this. One in particular illustrates why this is less straightforward than it might seem. http://www.codeproject.com/KB/cs/csi.aspx has a basic interpreter using .NET's built in ability to compile c# code. A key problem is that the author's approach creates a new mini .NET assembly for each interpreted line. C# may have the reflective power to have a python or ruby style console, but the .NET framework libraries are geared toward compiling C#, not dynamically interpreting it. If you are serious about this, you may want to look at http://www.paxscript.net/, which seems like a genuine attempt at interpreted C#.
I believe you are looking for Snippy =)