OOP with JavaScript [closed] - node.js

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 10 years ago.
I'm coding with JavaScript on Node.js for the moment, and I was asking myself if there is a convenient way for using classes with private and public properties (and methods). I'm coming from PHP so are there any similar structures?
I've been doing it for the moment with modules, I export the public methods and vars en keep the rest private, is that the right way? And are there any good guides on going OOP with JavaScript?

In Javascript the concept of "private" doesn't really exist, although it is possible once you get the hang of the language a bit more.
Here's a good introduction to OOP in Javascript: https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript

Javascript can emulate (or rather replace) some/most class behavior using prototyping, but to someone used to classes, the syntax can be a bit confusing. If you're going to work with Javascript professionally, learning prototyping is more or less a must though.
ECMAScript 6 which will probably eventually make its way into Javascript is rumored to add support for classes. It is not available yet though :-/
Typescript is a rather highly debated Javascript extension by Microsoft that adds some class support and strong typing for Javascript. It compiles down to standard Javascript, or can be added as a module to node.js.

You can achieve a "class structure", although what you would really be doing is making an object literal behave like a class.
I can understand why it might be confusing, especially when you read for example Backbone JS's documentation - they define their objects as classes - when really there are no classes! Only object definitions.
Using a framework such as Backbone makes things a lot easier to understand, because you are working with a class like structure. Might be a good place for you to start.

Related

Non-deterministic CSP programming tool? [closed]

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 10 years ago.
Hi i need a non deterministic constraint satisfaction problem tool, because i need different solutions with the same input of the problem. Someone knows about a tool with this characteristic?
I only know tools like Gecode (c++), Choco (Java) and Curry (Haskell) that i think work in deterministic way.
If what you want is to get some random solution, most CP tools have some support for using randomised heuristics. For example, the standard Gecode branchers have options for this, for example INT_VAR_RND and INT_VAL_RND for integer variables. To get a different search each time, make sure to set the seed uniquely.
Note that using random heuristics will not give you any guarantee of the distribution. For example, your problem might have only two solutions, but almost all random choices might lead to one of the solutions giving a very skewed distribution.
Are you trying to do Pareto optimization (aka multi-objective optimization) and let the user choose one of the pareto optimal solutions?
People have done this with Drools Planner (java, open source) by simply replacing the BestSolutionRecaller class. See this thread and this thread. Planner 6.0 or 6.1 will provide out-of-the-box pareto support.
Similar to what Zayenz said, you can try Minion with the flag -randomiseorder.

Is code clone a common practice in C,Java and Python? [closed]

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 10 years ago.
Code clones, also known as Duplicate code is often considered harmful to the system quality.
I'm wondering whether these duplicate code could be seen in standard APIs or other mature tools.
If it is indeed the case, then which language(such like C,Java,Python,common lisp etc.) do you think should introduce code clone practice with a higher probability?
Code cloning is extremely common no matter what programming language is used, yes, even in C, Python and Java.
People do it because it makes them efficient in the short term; they're doing code reuse. Its arguably bad, because it causes group inefficiencies in the long term: clones reuse code bugs and assumptions, and when those are discovered and need to be fixed, all the code clones need to be fixed, and the programmer doing the fixing doesn't know where the clones are, or even if there are any.
I don't think clones are bad, because of the code reuse effect. I think what is bad is not managing them.
To help with the latter problem, I build clone detectors (see our CloneDR) that automatically find exact and near-miss duplicated code, using the structure of the programming language to guide the search. CloneDR works for a wide variety of programming languages (including OP's set).
In any software system of 100K SLOC or more, at least 10% of the code is cloned. (OK, OK, Sun's JDK is built by an exceptionally good team, they have only about 9.5%). It tends to be worse in older conventional applications; I suspect because the programmers clone more code out of self defense.
(I have seen applications in which the clones comprise 50%+ of code, yes, those programs tend be awful for many reasons, not just cloning).
You can see clone reports at the link for applications in several langauges, look at the statistics, and see what the clones look like.
All code is the same, regardless of who writes it. Any API that you cite was written by human beings, who made decisions along the way. I haven't seen the perfect API yet - all of them get to redo things in hindsight.
Cloning code flies in the face of DRY, so of course it's recommended that you not do it. It's harmful because more code means more bugs, and duplication means you'll have to remember to fix them in all the clones.
But every rule has its exceptions. I'm sure everyone can think of a circumstance in which they did something that "best practices" and dogma would say they shouldn't, but they did it anyway. Time and other constraints don't always allow you to be perfect.
Suggesting that permission needs to be granted to allow such a thing is ludicrous to me. Be as DRY as you can. If you end up violating it, understand why you did it and remember to go back and fix it if you get a chance.

How difficult is to develop a text editor in windows? [closed]

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.
How difficult is to develop a text editor in windows?
edit: something like this for a target: http://intype.info/home/index.php
That depends on what sort of text editor you want. For a really simple editor, you can just host an EDIT control (or RICHTEXT control), in which case you have a working editor in well under an hour. If you want to write a full-blown programmer's editor, you'd probably want to measure the time in months or years instead of minutes.
Depends on what tools you use. To develop something offering basic functionality (like Notepad) using C# and WPF or Windows Forms (or VB if that's your poison) would be pretty easy if you knew the language and the API well. Doing something similar with Python, Ruby, or something else and Tk, GTK, Qt, etc. wouldn't be terribly hard either. Same goes for Java and AWT or Swing. Any graphics toolkit with a high level language is going to give you most of the scaffolding that all you really need to do is define the layout and add some logic (opening files, saving, etc.) Developing with C or C++ (unmanaged) and the Win32 API would be much more difficult, but still relatively simple.
You could probably put together a working Notepad clone rather quickly. It gets harder as you add features. I know that with my language and toolkit of choice (Python + Tkinter) I could probably do a feature for feature clone of Notepad in a few hours, right down to appearance and UI.
Pretty easy actually:
use whatever GUI toolkit you want
add a multi line textbox control
add load/save options
compile/build/link
and you just made a poor man's notepad :)
Now seriously, what do mean with a text editor? What kind of features are you looking for? Do want something simple like notepad, something totally awesome like notepad++, or something enormous like word?

Is there any programming language that lets you redefine its type system? [closed]

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 12 years ago.
I'm looking for programming languages that let you redefine their type system without having to hack into the compiler. Is there anything out there that allows you to do that?
Thanks
In C you can use DEFINE to redefine everything.
#DEFINE int double
Whether it's good or bad you can find out here:
What is the worst real-world macros/pre-processor abuse you've ever come across?
If you're talking about redefining an actual type system, like making a statically typed language dynamic or making a weakly-typed language strongly-typed, then no.
Practically every language lets you define your own types, so I don't think that's what you meant either.
The only thing I can think of that might fit into what you're asking about are Macros in Common Lisp, which let you extend the syntax. This might be able to acheive what you are looking for, but until you state what it is exactly you're looking for, I can't really elaborate.
Also OCaml and its related languages allow you to do some pretty cool things with types. You can basically define any kind of type you can think of and then match against it with pattern matching, which makes it especially good to write compilers in.
Javascript, Ruby, and Smalltalk, just that i know of, allow you to do all kinds of stuff, even redefining on the fly what an Object can do. Perl allows you to redefine practically the whole language. Basically any decent scripting language, especially one that allows duck typing, should have equal power. But it seems to be really common among functional languages and those with functional abilities.
If I remember correctly, Ada have neat type-creation possibilities, specially for measures (for instance, defining a minimum and a maximum, checking operations between differents measures...). I've seen it quoted as an example to avoid very stupid bugs.

Am I the only one who makes spelling and grammar mistakes when programming? [closed]

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.
I don't know if I am a bad programmer because I often make mistakes when outputting information on a site, things like "thanx for subscribing to our service" instead of "Thanks for subscribing to our service".
I think this is because I usually don't concentrate on the spelling, my main focus is to get the functionality running perfectly. Please give me your opinion, do you concentrate on the spellings or the functionality?
If I'm writing a message which will be visible to users, I'll make sure it's clear and correct. If I'm writing a message which will only be visible to other developers, I'm slightly less careful - in particular, typos aren't really a problem, so long as I express myself clearly.
Fortunately my spelling/typing/grammar is reasonably good anyway, so I don't need to think too hard about this, but I think it is important for customer-facing text.
Developers often aren't very good at writing messages for users. It can be hard to put yourself in the position of someone who really has no idea about what's going on in the background: they just want to get their email (or whatever it is) working. If you're lucky, you may be able to get a technical writer to help compose appropriate text.
IMO attention must be paid to both. Cool logic and reliability are no excuse for crappy texts.
You could separate checking the resource from changing the source. When you do changes first change the code, test everything, then proofread the resources.
The CTO at my last job was dyslexic, and a completely brilliant programmer and manager. Every now and then I would go and make a spelling correction to one of his method or variable names (C# handles the refactoring pretty well) and it didn't really matter that much.
When there's user interface work it's much more important to spell things correctly coz it looks very shabby to have a misspelled UI.

Resources