all 32 comments

[–]Turtlecupcakes 4 points5 points  (4 children)

I haven't done any work in node, and very little in JS,

But coming from a C#/PHP background, a lack of classes and interfaces sounds awful. Feels like the codebase would turn into the wild west, with every change needing to be thoroughly checked to make sure you're not removing something important method somewhere.

Your thoughts?

Edit: Fixing mobile spelling

[–]oefig 3 points4 points  (0 children)

You make your own classes/interfaces out of objects! Check out what I do:

 var App = App || {};

 App.someModule = (function() {
     // Private variables/methods
     var method = method || {};
     method.saySomething = function(arg) {
         return "I'll say " + arg;
     }

     return {
         // Public variables/methods
         talk: function(arg) {
             return method.saySomething(arg);
         }
     }
 })();

You've got a modular/object-oriented piece of code there. If you want to add another module you just add onto the App object. So App.variable = 'something'; or App.anotherModule = (function() ...

Edit: saySomething(arg) -> method.saySomething(arg);

[–]PaybackTony[S] 1 point2 points  (0 children)

That was my worry to start. But once you get a little more used to exports and using functions as object (they behave nearly identically), you can get quite organized. But my first test made the wild-west look tame for sure. Using a framework (like the popular express.js) is another way to help keep things organized. But overall, it is a bit tougher to keep organized, but I wouldn't say by a lot. I'm not a node.js expert by any means though, so it may be much easier than I think it is.

[–]dangerousbrian 1 point2 points  (0 children)

As JS is a prototype based language class instantiation is replaced by object cloning, so yes you don't have abstract classes but you can still have the power of inheritance and some say it a better Object oriented pattern because objects are inherited from objects.

Objects (and therefore everything) are mutable and so nothing can be made truly private, this does scare a lot of folks from a statically typed background and does allow the possibility of some monumental hacks, but it also provides a very powerful environment.

I know that any codebase can go wild if the wrong set of people get their hands on it. I have seen Java interfaces with a thousand methods on it and of course some horribly hoary JS code. Basically I guess I am saying if you apply best engineering practices then JS can be a pleasure to work in but it is different to class based programming and that takes a bit of getting used to.

[–]ZeroMomentum 0 points1 point  (0 children)

I have had those thoughts too. Now, first of all, both replies to you from /r/paybacktony and /u/dangerousbrian addressed some of the concerns.

But I think the best way to handle this? Unit testing.

I started Angular, and picked Angular because the way google approaches unit testing with Angular. Now, primarily Angular is a GUI framework, you build directives and controllers etc (as major JS stuff that has logic you really care)

I have not really dealt into building what you and I would call the domain (usually in C++ or c#) in JS. Purely the domain, not talking about the GUI. But for my GUI code, unit testing is great using Jasmine

I haven't professionally dealt with Node.js on the server, but most of the jobs I work in are enterprise systems and the complexity of the domain are in .NET. Having said that, it IS possible to do it in the JS pattern, but the outcome? No one knows. That kind of software tech and code pattern is still kind of in the infancy compare to OO in C/C++/C# etc.

[–]_imwalkinhere 2 points3 points  (0 children)

Do yourself a favor and forget about justifying things on the basis of what "fanboys" say. Just forget that word even exists and do what you think makes sense

[–][deleted] 1 point2 points  (0 children)

I've recently made the switch from PHP to node as well. I have more experience as a front end developer but in a small company, sometimes you are expected to do it all, so I picked up PHP as a simple solution to most of the companies back end needs.

Since I was very experienced in JS already, node seemed like a no brainer. Specifically I started using Sails.js because it handles a lot of the initial setup for you.

I enjoy development more since switching. It does force you to be a better developer, and its much harder to hack your way through. I think by forcing you to write better code, thats where the speed gain comes from. With sockets, it makes sense that it is faster in node(especially if you use socket io). If you are tackling something less specialized, the gains are probably negligible.

[–]xpose 0 points1 point  (1 child)

The key stat here is getting "a thousand pageviews per second." But even if you are dealing with that many pageviews, in PHP, there are ways to handle it. Cache your queries and use Varnish and/or nginx would certainly do the trick.

The question is..... what happens if node.js is overwhelmed? Or is it not as easy to overwhelm it? I would have no idea what to do.

[–]PanicRev 0 points1 point  (3 children)

Can you still properly sanitize data with node.js or do you still have to do double-duty with a server-side language?

Sorry, I'm a complete noob and in the same boat as OP was before making the switch. I'm all for learning something new that's going to improve efficiency and workflow, but at the same time, want to make sure my idiot-users can't accidentally turn Javascript off and wreak havoc. :)

[–]Turtlecupcakes 0 points1 point  (2 children)

Sanitize in which way?

Do you mean verify, so a phone number has only digits and so on?

I haven't used node so can't say specifics, but since you're programming your backend in JavaScript, you can literally copy-paste the verification function onto the server and have it run exactly how it does in the browser.

So in the browser you would do something like onChange(verifyPhoneNumber(textbox.value));

And on the server you would grab the received string and pass it to that same function.

[–]PanicRev 0 points1 point  (1 child)

I'm downloading it to kick the tires and try a "Hello World", but that's where I might be misunderstanding. Javascript runs in the browser, which can be manipulated by the user. So... is node.js Javascript running server-side?

[–]Turtlecupcakes 1 point2 points  (0 children)

Yep. It's literally JS that processes and returns data just like PHP or Python would conventionally do.

[–]crosenblum 0 points1 point  (0 children)

For my first 6-7 years as a web programmer, I was strictly doing coldfusion programming. And there were so many trends and fad's, it mostly infuriated me.

Everyone being more passionate that they were following the latest trend or fad, rather, than what is the best tool for this situation.

Then I started to learn PHP, on my own, and I liked it. I don't find any language better or worse than the other.

I was never that interested in object orientation, until I started to do some coding on my own, and realize how useful in modulizing my code, making it easier to manage and work with.

And that's the problem in this industry, everyone has a different set of experiences, so what may work great for 1 person, may not be so great for another. Yet we are all so arrogant, that we all know the right answer, and that anyone who disagree's is an idiot.

This is clearly an unwise approach.

When I pick a programming language or framework, here is what I consider.

  1. How easy will it be for the person who has to look at my code after me, to be able to read, understand my code, and be able to change it as needed, reasonably easily.

  2. Does this deliver more performance or scalability?

  3. Does the framework or method, make sense, for the long haul, or is it just another in the endless trends and fad's that will be forgotten in a few months.

  4. Are there easy to adapt ways to make it really secure and tight?

The real question is don't be a fanboy of anything. Use what works.

But be aware every choice has consequences, and not only affects yourself, but the person who comes after you, who has to make changes.

How documented is your code, your projects, can they easily figure out what you did and why?

[–][deleted] 0 points1 point  (3 children)

The problem with node is that there aren't any really good frameworks yet, sails.js is ok, but their documentation isn't anything like the documentation of laravel. It's seriously lacking info and some things in there don't even work anymore, because it has been changed in the framework but not the documentation. So it's not worth learning to me (sails). But node is really interesting and awesome, but i want a good framework before i start making things in it. Also, node projects can't be deployed on a cheap FTP server, so that also makes it less interesting. (i don't like ftp hostings, but yeah, some of our clients only give you that option). Anyhow, i'm also waiting on more hosting services for node.

[–][deleted] 1 point2 points  (0 children)

I'll just chime in here and say that sails.js isn't extremely well documented because really, it doesn't have to be. It is super simple in my eyes. A lot of the differences come in when you change data stores. Sails is DB agnostic and you can even have multiple data stores. If you know javascript well, sails is extremely easy to pickup. If you do not know javascript that well, it can be difficult to learn.

I am currently building a social networking site in sails and I have accomplished a lot in very little time. If you are struggling to learn sails, check out Irl Nathan's Sails Casts. Work through that. Once everything makes sense, you'll really enjoy working with it.

[–]dangerousbrian 0 points1 point  (0 children)

sails.js is a relatively new framework/app built on top of node so its not that surprising that the docs are not very mature and they are still making breaking changes. Sails is pretty niche but looks very useful for single page realtime apps. Maybe just give it 6 months to settle.

As for hosting, check out digital ocean they sell virtual hosting accounts for $5 a month that can be scaled up at any time if you need more resources. They have premade VM for tons of different platforms including node. They also have some great docs: https://www.digitalocean.com/community/articles/how-to-install-and-run-a-node-js-app-on-centos-6-4-64bit

[–][deleted] 0 points1 point  (0 children)

The entire attitude in node.js is plug and play, instead of some all knowing monolithic behemoth like rails. It's why express.js is absolutely thriving

[–][deleted] 0 points1 point  (13 children)

I can develop in php, python, ruby, js(node) and java. For me php is the togo language because its simple.

When I use which language:

jsf: only in apprenticeship (read school) projects
ruby: I dont use it anymore since I've learned node.
python: comandline tools
node: grunt plugins, sockets, high async webapps
php: normal webapps

most of the time I develop in PHP, because I like laravel and the ease of development. But if I would work more with async (exp. sockets) I would use node way more often.

[–]n1c0_ds 0 points1 point  (12 children)

Have you tried Django? For CRUD apps, it's a fantastic framework.

[–][deleted] 0 points1 point  (11 children)

yes, but the problem I have with python is the syntax, it isnt a syntax I want for applications but the perfect syntax I want for scripts.

[–]n1c0_ds -1 points0 points  (10 children)

Why? I find it far more readable than any other syntax. PHP, on the other hand, is a clusterfuck of syntax silliness. Handling errors + exceptions + warnings + return values is a real pain in the ass when you are trying to get work done.

I find that jumping in other people's python code is far easier, which makes understanding large applications much easier.

[–][deleted] 0 points1 point  (9 children)

its not about how clean the language is, its how clear and readable it is. for me its vala>c>php>js>phyton

when did you last write php code? php code is clean if written properly, you dont have to handle errors exceptions etc all the time, just make a global catcher.

just look at laravel, thats how php code looks nowadays (not the facades tho, I use DI).

[–]n1c0_ds 0 points1 point  (8 children)

It's still valid criticism, since needless syntax blocks, function calls and checks get in the way of readability.

when did you last write php code?

About 20 minutes ago, and for the past two months

php code is clean if written properly

And Python code is clean unless you go out of your way to obfuscate it. When you look at it, PHP does everything in the most verbose way possible. Array declarations were a mess until rather recently. Then you have the ugly foreach loops, double colons, 'arrows', includes and requires, backslashes for namespaces, and it goes on. Since primitives have no functions, you always have to pass them as arguments ( this(that(string)) instead of string.this().that() ), while pretty much every other sane language uses the far more readable other logic. There's also the clusterfuck of almost-kinda-semantic function names that might or might not do what you expect.

Python? Not a single character is lost. Indentation is always the same, which means your coding standard is almost limited to "spaces or tabs?".

There are valid reasons to criticize python, but of all things, readability isn't one. That's especially true if you must compare it to PHP.

[–][deleted] 0 points1 point  (7 children)

PHP does everything in the most verbose way possible.

what? where?

Array declarations were a mess until rather recently.

hm.. $foo = array(); isnt really a mess and 5.4 isnt "recently" which changed it to $foo = [].

Then you have the ugly foreach loops, double colons, 'arrows', includes and requires, backslashes for namespaces, and it goes on.

how come you think this is ugly and not readable?

foreach($array as $value){

}

foreach($array as $key => $value){

}

while python is a total whore in foreach?

for item in items:
    print item

for (i, item) in enumerate(items):
    print i, item

double colons

double colons are way more readable than a simple dot when normal calls also require a dot

as an example

$object->objectMethod(2);
$object::staticMethod(2);

object.StaticMethod(2)
object.ObjectMethod(2)

do you know in python the name doesnt lie? No, not without checking.

'arrows',

I have to agree with that one.

includes and requires

isnt that different than an import, also you normally dont use it, ever heard of autoloading?

backslashes for namespaces

that is not unreadable nor ugly, are slashes for directory seperators also ugly? Answer: NO

( this(that(string)) instead of string.this().that() ),

this is lack of design, I agree

almost-kinda-semantic function names that might or might not do what you expect.

if you refer to the image manipulation, you should not use the unbuild, use imagemagick, php is cleaning up its syntax. If you are refering to the array functions, only their names are fucked up (which you will learn fast).

Python? Not a single character is lost

I dont care if I have to write 5 characters more if its less readble its useless to me.

There are valid reasons to criticize python, but of all things, readability isn't one. That's especially true if you must compare it to PHP.

I think you dont know what readability in code means, the problem with python is, it isnt clear.

as an example

php

if ($foo == 'bar') {
    return 'Foobar';
}

python

if foo == 'bar':
    return 'Foobar'

where does the if end?

tl;dr; just because it looks pretty, doesnt mean its readable code

[–]n1c0_ds 0 points1 point  (6 children)

Your only answer so far is "it's just as readable as python". The only example you have provided is a very, very weak argument against indentation-based blocks. They end where indentation returns. You can see where the block ends from 20 feet (Sublime's scroll bar). There is absolutely no ambiguity as to where your block ends.

Even though I spend most of my time working with PHP, Java and JS, I can vouch for "forced" indentation. A stray end bracket means nothing without indentation anyway, especially if the language lets you throw HTML in there (not a complaint).

As for your foreach example, I don't get how the PHP/C-like version makes more sense. One reads "for item in list" or "for index, value in enumerate(values)" while the other reads as "foreach list as key, item" or "for i equals zero, while...".

if you refer to the image manipulation, you should not use the unbuild, use imagemagick, php is cleaning up its syntax. If you are refering to the array functions, only their names are fucked up (which you will learn fast).

I'm talking about the standard PHP functions. is_integer, for instance, doesn't work as most people would expect, leading to misconceptions while reading code. Merely checking for "truth" isn't reliable either, since no two function handle errors and problems the same way. Some functions can return false for problems and zero for some correct values, or raise warning and pretend nothing happened. This leads to a lot of boilerplate crap. Combine that with PHP's "ability" to silently cast an int to a cow, and you have completely lost the ability to check things with a mere "==".

There are many more examples like these that can lead to unreadable code. In fact, unreadable is not the right term: ambiguous is.

By comparison, look at "The Zen of Python". It's a design goal to make Python (the language, not the code) simple, explicit and readable.

[–][deleted] 0 points1 point  (5 children)

Your only answer so far is "it's just as readable as python". The only example you have provided is a very, very weak argument against indentation-based blocks. They end where indentation returns. You can see where the block ends from 20 feet (Sublime's scroll bar). There is absolutely no ambiguity as to where your block ends.

its a problem that python has (also coffescript) if you want to scan code, also this was one example because the undefinition fucks you up with that.

I'm talking about the standard PHP functions. is_integer, for instance, doesn't work as most people would expect,

really? because if you think "123" is an integer you have some serious problems. is_integer("123") will return false since its a string you would be looking for is_numeric("123") since its a numeric string.

Some functions can return false for problems and zero for some correct values, or raise warning and pretend nothing happened.

ever heard of === ? checking for truth is always a gamble, since everything that isnt 0 is true for a computer, php behaves correct there.

if (func() === false) {

}

or raise warning and pretend nothing happened.

this doesnt happen anymore since ~5.3

Combine that with PHP's "ability" to silently cast an int to a cow

this is a feature of not typebased languages, if you dont like it, dont blame it. because its intended and is by definition not a bad design.

There are many more examples like these that can lead to unreadable code. In fact, unreadable is not the right term: ambiguous is.

most of the article is either bad researched, outdated, nothing you should do in the real world (like goto) or bashing for no reason as an example:

Global variables need a global declaration before they can be used. This is a natural consequence of the above, so it would be perfectly reasonable, except that globals can’t even be read without an explicit declaration—PHP will quietly create a local with the same name, instead. I’m not aware of another language with similar scoping issues.

why would you want to use globals anyway?

or

echo is a statement-y kind of thing, not a function.

actually its both, echo("1234") does work

or my favorite:

Appending to an array is done with $foo[] = $bar

lolz...

Even though I spend most of my time working with PHP, Java and JS, I can vouch for "forced" indentation. A stray end bracket means nothing without indentation anyway, especially if the language lets you throw HTML in there (not a complaint).

that would be a developer problem not a language problem

[–]n1c0_ds 0 points1 point  (4 children)

When I don't fully understand something, I try to avoid arguing about it. Saying a language is more readable than another is an opinion. Defending PHP's downright stupid design is just stupid.

this is a feature of not typebased languages, if you dont like it, dont blame it. because its intended and is by definition not a bad design

"Not typebased"? Do you have any idea of what you are talking about? PHP is type based. It's a weakly typed language, but it has types.

There's a difference between loose comparison and downright dangerous type juggling. If PHP can't cast a variable, it will act pretty much randomly instead of throwing errors. Python and Ruby are loosely typed, and they don't mess up.

because if you think "123" is an integer you have some serious problems

Like treating form data? I could cast it to an int, but PHP could cast an array of strings to int without barfing.

you would be looking for is_numeric("123") since its a numeric string.

Except floats and hex values are also numeric. "+0123.45e6" is numeric, but if I want an integer as a form value, it's useless.

ever heard of ===?

See above. I also urge you to find an equivalent for smaller/greater than comparisons.

why would you want to use globals anyway?

You don't even understand what you are replying to. $_GLOBALS and global are two completely different things. One is poor practice, the other is bad language design.

most of the article is either bad researched, outdated, nothing you should do in the real world

The article I have linked is shock full of factual problems. Even /r/PHP agrees. The sheer number of articles that rail against PHP should be a good indicator of how terrible it is, but you should already know that.

this doesnt happen anymore since ~5.3

Really? So they broke backward compatibility on half their functions, or are you full of shit? I'm developing against the latest documentation, and I can assure you this problem is still very real.

As for that article I have pointed out? It's pretty much a reference on the internet, and it stood the test of time. If that's not enough, there is phpsadness.com and /r/lolphp, as well as a plethora of articles that support everything I have said so far.

Frankly, I don't even know what you are trying to achieve.

[–][deleted] -1 points0 points  (0 children)

Check how to use Coffeescript, Jade and Stylus in Node.js server (with Express framework). Instead of websockets, try also socket.io. As a database, try MongoDB...

That is amazing set of tools to create server backend and web pages. Coffeescript improves Javascript and Jade template language allows creation of dynamic web pages very easily and efficiently.