Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
As a way to understand the differences between OOP and Procedural languages I was looking for a sample program written in C and C++ or C# or Java. I just want to see the different approaches to the same problem to help me get a sense of the real differences. Does anyone know where I can find a tutorial like this?
I don't think this is likely to teach you much. The program has to have a certain size before the differences between different programming paradigms really show. And people aren't likely to write identical copies if the same program in different languages unless the program is trivial.
Most real-life examples would also be polluted with a lot of extra noise, things that can be done within the standard library of one language, but requires third-party libraries in another. And the programmer who wrote it may be more familiar with one language than another, so his implementation in some languages isn't representative of how it "should" be done.
You're more likely to learn the difference between these paradigms the usual way. By learning what each means, and how to use it.
I recommend the 99 bottles of beer website
You can always look at Project Euler. People solves the same problems in a many different languages. Most people will post their solutions that you can access after you solve the problem also.
Take a look at The Computer Language Benchmarks Game. It's got implementations of various programs in just about every language you could imagine.
This might be a bit simple for your purposes but the Hello World Collection is always fun to look through.
Rosetta Code has a wealth of data but very little of it is related to the procedural/object-oriented distinction. You should also see their collection of related sites.
Black Scholes in multiple languages has plenty of implementations of the Black-Scholes formula.
The formula is implemented in Objective-C/iPhone, F#, Autoit, Fortress, Lua, APL, SAS, Mathcad, J, MEL, Postscript, VB.NET, Clean, Ruby, Lisp, Prolog, PL/SQL, LyME, ColdFusion, K, C#, HP48, Transact SQL, O'Caml, Rebol, Real Basic, Icon, Squeak, Haskell, JAVA , JavaScript, VBA, C++, Perl, Maple, Mathematica, Matlab, S-Plus, IDL, Pascal, Python, Fortran, Scheme, PHP, GNU, gnuplot.
Somebody posted Evil Walrus / ReFactory on Reddit the other day:
http://www.refactory.org/
Here are two programs that implement n-body
Java implementation
C implementation
What differences do you find between them?
Consider the implementation of a snakes and ladders games
In a procedural design we might write a function like
function move(int n) {
pos += n;
switch(pos) {
case 6: pos = 10; break;
case 12: pos = 4; break;
case 15: pos = 32; break;
case 16: pos = 8; break;
// ...
}
}
in an object design language we would create a linked list of Square instances, with some Ladder and Snake instances that branch to other squares.
class Square
def initialize(next)
#tokens = []
#next = next
end
def next(n)
n == 0 ? self : next.next(n-1)
end
def move(token,n)
tokens.remove(token)
target = self.next(n)
target.tokens << token
end
end
class SnakeOrLadder < Square
def initialize(next,branch)
super(next)
#branch = branch
end
def next(n)
# goes to branch when landing on this square!
n == 0 ? #branch : next.next(n-1)
end
end
as you can see, we implement the game rules in the objects as well as the way they are composed (rather than in a switch case statement). This has the advantage that
it is simple to add new game rules at development time, you'll just write a new subclass of Square
it is simple to change the layout of the game at runtime (might sound strange for a game, but for your average business app, this is what you want)
this flexibility is what makes OO so powerful.
Related
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 8 years ago.
Improve this question
i'm learning programming on c# in my university country with French language and i wonder if i can change word to french to be more easly for me, ex:
From:
string name = Console.ReadLine();
if(name=="Aymen")
{
Console.WriteLine("Hello, Aymen");
}
To:
chain nom = Console.ReadLine();
Si(nom=="Aymen")
{
Console.WriteLine("Bonjour, Aymen");
}
What i did is i change the "string" and "if" with "chain" and "Si".
Thanks
No, that's not possible (and a bad idea anyway).
C# has a clearly designed specification that defines all language constructs, and such "translations" are not part of it. It would be possible to create a computer language with such a feature, but the impact would be huge:
the list of keywords would increase for each "language"(in your example, Si has be a reserverd keyword).
learning the language would be harder.
the majority of users of the language would not be able to read your code (just think about the fact that each basic type has multiple names.
as a professional developer, you would probably still need to learn the english version, and learning the french version first would be wasted time.
it would be much harder to write a compiler for such a language (harder == needs more time == costs more money), for very little gain.
So I would not be surprised to see a esoteric fun computer language with such a feature; but a production ready mainstream language? No.
Changing the keywords of a language is a wrong way learning the language and learning coding.
The reserved and contextual keywords of C#, and most languages, cannot be changed - the compiler expects them to be pre-defined and unambiguous. No mainstream language allows for this. This covers the string and if in your example.
The class and member names of the available libraries and frameworks are contained in the libraries themselves. Technically you could replace the libraries with your own (yes, you can write your own System.dll and mscorlib.dll, if you are very, very bored) - but some names are expected and are necessary - for example, you can't replace Monitor.Enter or GetEnumerator)() / MoveNext() / get_Current with something different - the compiler will simply break. This covers Console, ReadLine and WriteLine in your example.
However! The fields, variables, types and members inside your own code can be anything reasonable in unicode. And of course your string literals can contain any unicode you want. So you can have:
static class す {
static void Main() {
ず();
}
static void ず() { }
}
What you're asking is to change the types and the keywords of the programming language, then its structure. Since the compiler expects to work with the standard structure defined by Microsoft, your changes won't work. For this reason, what you're asking isn't possible.
If you want to change the keywords and define a new structure, read few books and learn something about compilers and programming languages. Then, maybe, you'll be able to create your own programming language with a structure defined by yourself.
Everybody, I think, wants to change the structure of a programming language according to the spoken language, in order to read it in an easier way. I thought many times something about Console.LeggiRiga() (Italian translation for Console.ReadLine()), but this would mean that my source code would be readable and understandable only by Italian speakers.
Using English, we're able to understand each other what we're doing with the code, since English ,as #OndrejTucny said, is the lingua franca of software engineering.
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 know there are lots of external packages on linear algebra but my question is really when do I use f# and when c#? I make a simple example and while I did this I realized maybe it is too simple. but lets say I want to do element by element division of two arrays:
Imperative c#:
static double[] elementdivideimp (double[] arr1, double[] arr2)
{
var res = new double[arr1.Length];
for (int i = 0; i < arr1.Length; i++)
{
res[i] = arr2[i]/arr1[i];
}
return res;
}
LINQ c#:
static double[] elementdivideL(double[] arr1, double[] arr2)
{
return arr1.Zip(arr2, (a, b) => b/a).ToArray();
}
f#:
let elementdividefunc a b = Array.map2 (fun i j -> (j / i)) a b
As said maybe this is too simple but I m really struggling to decide which language to go for when I face a programming challenge. SO when do I use which?
As already mentioned in the comments, use the right tool for the job.
Now, the question is, when is F# the right tool for the job. To get a useful answer, it is important to look at the problem from the business perspective - what business problems are you facing and can F# (or any other language) help you solve them?
I think the talk Succeeding with functional-first languages in the industry by Don Syme looks at this problem from the right perspective. It looks at the specific task of developing analytical components and explains what problems people usually face in this domain (correctness, complexity, efficiency and time-to-market) and how better languages can help you overcome those (and many of the points are based on the evidence collected by the F# foundation and numerous earlier SO questions #1 #2).
I think you will not get a clear answer just by comparing fairly simple snippets of code, but your example demonstrates that:
F# code is generally more concise which likely reduces time-to-market
Compared with imperative C#, you do not need to handle corner cases, which aids correctness
You can easily use data structures like arrays, so your code is more efficient (I have not tested this, but I think the C# code uses IEnumerable<T> and will actually be slower in this case).
If you look at other business areas, then you may find different problems and then you can base your evaluation on those. But for analytical components (computations), I think there is a good evidence for languages like F#.
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.
I decided to learn some Python (IronPython) syntax today. In doing so, I was impressed by a construct that it allows with its loops.
Python supports an else clause on its loops. An else on a loop basically says, "if this loop finished normally, then enter this clause".
Allow me to demonstrate using C#.
This code:
Something something = SomeCallToSetThisUp();
bool isCompatable = false;
foreach (Widget widget in widgets)
{
isCompatable = widget.IsCompatableWithSomething(something);
if (!isCompatable)
break;
}
if (isCompatable)
compatableSomethings.Add(something);
could become this code (not valid C#):
Something something = SomeCallToSetThisUp();
foreach (Widget widget in widgets)
{
if (!widget.IsCompatableWithSomething(something));
break;
}
else
compatableSomethings.Add(something);
Having never seen this, it struck me as cool. And once you learn it, it seemed as readable as any code I have seen.
While not universally needed (sometimes you want to affect every item in the list), I do think that it would be useful.
So, my question is: Why isn't this in C#?
I have a few ideas why:
break can make debugging harder, so the designers did not want to encourage it.
Not everything that is shiny can make it into the language. (limited scope).
But those are just guesses. I am asking for an actual canonical reason.
The usual answer is because no-one asked for it or the cost of developing and maintaining it outweights the benefits.
From Eric Lippert's blog:
I've already linked several times to Eric Gunnerson's great post on
the C# design process. The two most important points in Eric's post
are: (1) this is not a subtractive process; we don't start with C++ or
Java or Haskell and then decide whether to leave some feature of them
out. And (2) just being a good feature is not enough. Features have to
be so compelling that they are worth the enormous dollar costs of
designing, implementing, testing, documenting and shipping the
feature. They have to be worth the cost of complicating the language
and making it more difficult to design other features in the future.
After we finished the last-minute minor redesigns of various parts of
C# 3.0, we made a list of every feature we could think of that could
possibly go into a future version of C#. We spent many, many hours
going through each feature on that list, trying to "bucket" it. Each
feature got put into a unique bucket. The buckets were labelled:
Pri 1: Must have in the next version
Pri 2: Should have in the next version
Pri 3: Nice to have in the next version
Pri 4: Likely requires deep study for many years before we can do it
Pri 5: Bad idea
Obviously we immediately stopped considering the fours and fives in
the context of the next version. We then added up the costs of the
features in the first three buckets, compared them against the design,
implementation, testing and documenting resources we had available.
The costs were massively higher than the resources available, so we
cut everything in bucket 2 and 3, and about half of what was in bucket
1. Turns out that some of those "must haves" were actually "should haves".
Understanding this bucketing process will help when I talk about some
of the features suggested in that long forum topic. Many of the
features suggested were perfectly good, but fell into bucket 3. They
didn't make up the 100 point deficit, they just weren't compelling
enough.
http://blogs.msdn.com/b/ericlippert/archive/2008/10/08/the-future-of-c-part-one.aspx
Additionally, you need to weight if the feature will be easily understood by existing / new developers. IMHO else on loop is not very readable, especially since the keyword for 'execute this block if the previous one finished OK' is finally.
What is more, I think Enumerable.Any / Enumerable.All methods are much better in this scenarios.
Looping through a collection and checking a condition are different things, so they should be separate language constructs.
Because for else loops are a hack from languages like python. If you feel like you need a for else loop, you should probably put that code in a separate function.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I do not want to ask candidates questions, but rather give them several problems to resolve. The reason for this is that I've seen people be excellent with theory, but when confronted by a real world c# issue, just couldn't hack it.
These c# problems should be simple enough that it won't take more than 1-20 minutes to resolve, yet complicated enough that I'd be able to weed out candidates that can't code.
Right now, I typically ask the applicants to reverse a string and remove duplicates from a List. This alone weeds out a large number of people.
Any other examples I could use?
Edit: I should have mentioned that this is for a standard c# gig, where they'll be writing business code rather than finding the most optimal way to implement a linked list.
I like picking simple problems that I actually had to solve at some point; it doesn't get more relevant to the job than that.
When I worked on VBScript I'd ask college candidates how to write a simplified version of DateDiff, since doing so was what I did my first real day of work at Microsoft. More advanced candidates I would ask how to build a device which tracks the relationship between 32 bit handles and an associated 64 bit pointer, which again I actually had to do when working on VBScript.
More recently I tend to ask questions about tree manipulation algorithms, since the compiler is all about tree manipulation. Or about how to codegen new operators using monads, since that's how LINQ works.
My point is not that you should use questions in these areas, my point is that surely you must have had problems that you had to solve in your day-to-day work. Ask the candidates about those problems -- then you'll learn how they solve a realistic problem, and they'll learn what sorts of problems they'd be solving if they came to work with you.
dont ask for knowledge of class libraries or obscure corners of the language (unsafe, dynamic, ..); smart people can pick these up or look them up.
I would ask to design a class hierarchy to represent something real world (vehicles, animals, ...). This usually flushes out the people who dont get objects. Make them do it with interfaces too. Also make them reverse a string - no harm in oldies but goldies
I agree with you, it is surprising how many people claim to be experienced and you find out that all that they did was read the box…
I don’t know if testing for C# is as valuable as it first seems… sure you could ask them to describe an example of when they needed to use inheritance, or why casting might have a performance problem, etc. But these are easy to study for. You would be surprised at how many interviewees give the example using “car” or “color” when giving their real world example of inheritance…. Guess they are in a book somewhere.
When looking at this problem it helps me when I compare experience in development to learning Spanish. A short time into the class everyone is conjugating verbs and can pass a test on this… but nobody speaks Spanish yet. You want the guy that claims to speak Spanish and can actually do it.
So I like to be more specific with the other technologies that will tell me if they have traveled the well-worn path of development. If they say they are an ASP.Net developer I ask them simple questions, but ones that are on the path
EXAMPLES: Give me an example of where the connection string could live? If you need to pass an ID from one page to another, what are your options? If a page takes 5 minutes to load, tell me how you would go about troubleshooting it. If I had a web page that had a single button on it, how would I center that button? Tell me the difference between storing variables in the viewstate verses session state?
You don’t have to know everything, but eighty percent of the people interviewing for a senior level position will get 10% of these types of questions right. (And on 70% of the phone interviews you will hear them Googling for the answers – good thing these aren’t the types of questions you can easily Google for.)
SQL Server is about the same. They say they would rate themselves an 8 or 9 in SQL Sever development, but then get 10% of questions. The questions again are to see if you have been on the well-worn path.
EXAMPLES: If you had a table of customers and a table of orders, how would you find the customers that had no orders? What is a clustered index? If I had a table of developers and a table of projects, how would I set it up so that projects could have multiple developers on it and developers could be on multiple projects?
How could you develop in SQL Server for “years” and not have hit these concepts? A high percentage of candidates get almost none of these answers right!! (I guess the SQL Server box isn’t as informative.)
So if you say you are a senior level guy and you can say “Soy un revelador de software” (I am a software developer), but can’t say “He hecho eso antes” (I have done that before), I don’t think you are the senior level person you are claiming to be.
Now this tells you if they have been on the well-worn path, but not if they are smart and have good problem solving skills. Having gone thru a ton of these types of interviews I can tell you that by the time the process is done you will be satisfied with having enough information to have a strong opinion on both of these issues. You might also see that by then giving them a problem set to solve is unnecessary.
Show them a small section of code or architecture diagram from one of your own projects and ask them to suggest how they would refactor it. Even if you don't wind up hiring them, you might get some interesting suggestions on ways to improve your code.
Building Eric's and other answers here, but answering as an only-ever-so-far-interviewee, what I would like in an interview is a kind of pair-programming 'test', where you sit down together facing the screen, and talk through a real-world problem.
I think there would be many advantages:
For the interviewee, being in front of a screen instead of facing the interviewer makes it easier to think about the problem rather than the interview.
For the interviewer, being with the interviewee while they look through the code and ask questions about the problem space would give a much greater insight into how the interviewee thinks, how they approach problems, and how they communicate and interact with others.
I would expect that it's more important and interesting to see a candidate thinking round the edges of your real-world problem, even if they don't completely solve it, than to have them get 10 out of 10 on come algorithmic test.
Something mildly algorithmic.
Write a method that returns true if a string is a palindrome, and false otherwise.
Re-implement the String.Substring(int, int) method.
Something about object-oriented design too.
Design a checkers game (ie, define the classes and some of the methods).
One question I was asked, and subsequently ask interviewees, is"Describe how you would make this phone into an application". Have them describe the classes, their properties, methods, interfaces, etc. Then question them on why they chose to implement them in that specific way. It gives you a good idea if they understand how to code, and gives you some insight into how they approach and solve problems.
Also, if you offer a suggestion of how they could have implemented it a different way, it may show you whether they are open to new ideas, criticism, or if they are a team player or not.
Fizz Buzz
This question already has answers here:
Recommended library for linear programming in .Net? [closed]
(3 answers)
Closed 5 years ago.
I have an application in C#, I need to do some optimization calculations, like Excel Solver Add-in does, one option is certainly to write my own solver implementation, but I'm kind of short of time, so I'm looking into libraries that already exist that can help me with this.
I've been trying the Microsoft Solver Foundation, which seems pretty neat and cool, the problem is that it doesn't seem to work with the kind of calculations that I need to do.
At the end of this question I'm adding the information about the calculations I need to perform and optimize.
So basically my question is if any of you know of any other library that I can use for this purpose, or any tutorial that can help to do my own solver, or any idea that gives me a lead to solve this issue.
Thanks.
Additional Info:
This is the data I need to calculate:
I have 7 variables, lets call them var1, var2,...,var7
The constraints for these variables are:
All of them need to be 0 <= varn <= 0.5 (where n is the number of the variable)
The sum of all the variables should be equal to 1
The objective is to maximize the target formula, which in Excel looks like this:
(MMULT(TRANSPOSE(L26:L32),M14:M20)) / (SQRT(MMULT(MMULT(TRANSPOSE(L26:L32),M4:S10),L26:L32)))
The range that you see in this formula, L26:L32, is actually the range with the variables from above, var1, var2,..., varn.
M14:M20 and M4:S10 are ranges with data that I get from different sources, there are more likely decimal values.
As I said before, I was using Microsoft Solver Foundation, I modeled pretty much everything with it, I created functions that handle the operations of the target formula, but when I tried to solve the model it always fail, I think it is because of the complexity of the operations.
In any case, I just wanted to show these data so you can have an idea about the kind of calculations that I need to implement.
Here are some commercial .NET libraries containing different kind of multivariate optimization functions which can be a replacement for Excel's solver:
Extreme Optimization Numerical Libraries
Centerspace NMath.Net library
Visual Numerics library
If you find a good non-commercial / open source library for this purpose, let me know.