Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India

quote:

I've got an app that displays hundreds of thousands of dom nodes and that number is only going to increase. I update their contents with AJAX. I'm running into performance issues and from profiling it appears that I may just be hitting the limits of what you can do with a browser. Right now I'm sitting at 153k dom nodes, and there are elements, roughly 10-15,000 of which, that actually get updated every 20 seconds. This of course, runs like dog poo poo. The core of the issue appears to be the main loop that does the actual updating of the html. I tried throwing it into a staggered setTimeout but it didn't help at all.

https://github.com/Matt-Esch/virtual-dom

I'm not much of a front end guy but would something like this help?

Adbot
ADBOT LOVES YOU

EAT THE EGGS RICOLA
May 29, 2008

caiman posted:

The free version requires that you "give credit" to the image creator. Any idea if they're cool with me doing this via code comments?

If I offered a thing that required that you give me credit, and you hid the credit in the code, I would probably feel like you were not agreeing to the terms of the license.

revmoo
May 25, 2006

#basta

Gmaz posted:

https://github.com/Matt-Esch/virtual-dom

I'm not much of a front end guy but would something like this help?

Yeah me neither. That may help, I'll have to look into it.

Jimlit
Jun 30, 2005



revmoo posted:

I've got an app that displays hundreds of thousands of dom nodes and that number is only going to increase. I update their contents with AJAX. I'm running into performance issues and from profiling it appears that I may just be hitting the limits of what you can do with a browser. Right now I'm sitting at 153k dom nodes, and there are elements, roughly 10-15,000 of which, that actually get updated every 20 seconds. This of course, runs like dog poo poo. The core of the issue appears to be the main loop that does the actual updating of the html. I tried throwing it into a staggered setTimeout but it didn't help at all.

Where do I go from here to scale this thing? Do I need to use some sort of canvas/svg tech or what? I'm kind of stumped.

Out of curiosity what type of app needs that much information getting updated that frequently?

streetlamp
May 7, 2007

Danny likes his party hat
He does not like his banana hat

caiman posted:

Just this evening I came across this: https://fonticons.com/. It seems to basically be a way to create a customized group of icons without having to load an entire pack.

Also https://icomoon.io/

revmoo posted:

I've got an app that displays hundreds of thousands of dom nodes and that number is only going to increase. I update their contents with AJAX. I'm running into performance issues and from profiling it appears that I may just be hitting the limits of what you can do with a browser. Right now I'm sitting at 153k dom nodes, and there are elements, roughly 10-15,000 of which, that actually get updated every 20 seconds. This of course, runs like dog poo poo. The core of the issue appears to be the main loop that does the actual updating of the html. I tried throwing it into a staggered setTimeout but it didn't help at all.

Where do I go from here to scale this thing? Do I need to use some sort of canvas/svg tech or what? I'm kind of stumped.

Are you visualizing a bunch of data or something? Anyway to post some sort of preview/example?

streetlamp fucked around with this message at 16:49 on Jan 23, 2015

revmoo
May 25, 2006

#basta
Data visualization yes, we're tracking hardware states. The app works fine and has for a while but as we scale it's becoming more of an issue. If I need to monitor a thousand nodes, then there will be four thousand DOM elements that get updated, and probably a third of those elements will have actual updates on every polling interval.

Moving forward we'll be doing some realtime streaming stuff without a polling interval using something like websockets, so now is the time to address performance issues.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

revmoo posted:

Data visualization yes, we're tracking hardware states. The app works fine and has for a while but as we scale it's becoming more of an issue. If I need to monitor a thousand nodes, then there will be four thousand DOM elements that get updated, and probably a third of those elements will have actual updates on every polling interval.

Moving forward we'll be doing some realtime streaming stuff without a polling interval using something like websockets, so now is the time to address performance issues.

Is everything on screen at once? Is it possible to constrain DOM node rendering to the viewport, which will make scrolling/paging more sluggish, but at least make the basic page viewable.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Maluco Marinero posted:

Is everything on screen at once? Is it possible to constrain DOM node rendering to the viewport, which will make scrolling/paging more sluggish, but at least make the basic page viewable.

Also, if you are rendering a super dense graph, its probably time to look into using Canvas to render it instead of DOM nodes, which I imagine will be more efficient as you avoid the overheads that come with DOM nodes, such as layout calculations and tree relationship for CSS matching.

Also CSS targeting will probably need to be tighter. If you have rules that nest 3/4 selectors, you can end up with a lot of low efficiency rules being forced to run over your mountain of DOM elements, further contributing to slow down.

Keep in mind this is all super general advice too, the suitable optimisations really depend on what you're doing.

streetlamp
May 7, 2007

Danny likes his party hat
He does not like his banana hat
Definitely look at Canvas, could help out a lot

https://groups.google.com/forum/#!msg/d3-js/ZJ6pznVU5LQ/wLYuIGPUnvsJ

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

revmoo posted:

Right now I'm sitting at 153k dom nodes, and there are elements, roughly 10-15,000 of which, that actually get updated every 20 seconds.

Can a human even look at that much stuff at once and digest it all??

kedo
Nov 27, 2007

Does anyone have a good study/article about contrast? I have a client who is big on accessibility and I want to be able to back up my statements of "no really, that text has plenty contrast, not everything has to be black on white" with numbers.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

fletcher posted:

Can a human even look at that much stuff at once and digest it all??

Nope.

revmoo: if you have that much crap on screen, you're doing it wrong. Look into React maybe, do virtual DOM some other way (as suggested above) but the real solution is probably a UI rethink.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

kedo posted:

Does anyone have a good study/article about contrast? I have a client who is big on accessibility and I want to be able to back up my statements of "no really, that text has plenty contrast, not everything has to be black on white" with numbers.

The requirement for 508 accessibility is 4.5:1 contrast ratio. You can check contrasts with this here thing: http://webaim.org/resources/contrastchecker/

kedo
Nov 27, 2007

Lumpy posted:

The requirement for 508 accessibility is 4.5:1 contrast ratio. You can check contrasts with this here thing: http://webaim.org/resources/contrastchecker/

Awesome, thanks much!

IAmKale
Jun 7, 2007

やらないか

Fun Shoe
I'm getting back into web design after a 7-8 year hiatus. My god, I knew things had changed but after reading the OP and talking to a couple friends it's obvious that I'm going to have to toss out all of my old knowledge and start over. Frameworks, CSS preprocessors, HTML5, grid-based layouts...I feel like I'm in way over my head.

How do I pick one framework over another if I don't have any experience with any of them? I checked out some reviews of PHP-based ones and plan on giving Yii a try, but then someone introduced me to AngularJS and React and now it feels like I could spends months considering my alternatives and still not be able to decide. Should I just pick popular frameworks and learn them? Are frameworks mutually exclusive - would I use AngularJS with Laravel, for example?

Edit: I should mention that I'm comfortable with HTML/CSS/JS/PHP/etc... but I've never used a framework before and the last time I did any development it was alright to render pages on the server. I'm now coming to understand that RESTful APIs are now king, and that client-side frameworks are designed to pull content from server-side frameworks that expose the API.

IAmKale fucked around with this message at 02:47 on Jan 24, 2015

Ocean Book
Sep 27, 2010

:yum: - hi

v1nce posted:

Sure, use something like jQuery to show/hide elements easily.

http://jsfiddle.net/t1gyspzj/1/

There are many different ways to achieve this, depending on what exactly it is you're trying to do.

Thanks, that looks like it could work pretty well. What I'm looking to do is make a short DnD style combat adventure where you have randomized numbers determine combat and stats like health and gold stored as variables. It's not for any purpose other than last year I programmed something like that on my TI-8_ calculator and it was fun and want to try it in another context, particularly one where typing is easier. Yesterday I found out how to make a button assign a value to a variable so with this I have everything I need, hopefully.

syrup
Jul 18, 2003

I HAVE TAMED THE BEAST

Karthe posted:

I'm getting back into web design after a 7-8 year hiatus. My god, I knew things had changed but after reading the OP and talking to a couple friends it's obvious that I'm going to have to toss out all of my old knowledge and start over. Frameworks, CSS preprocessors, HTML5, grid-based layouts...I feel like I'm in way over my head.

How do I pick one framework over another if I don't have any experience with any of them? I checked out some reviews of PHP-based ones and plan on giving Yii a try, but then someone introduced me to AngularJS and React and now it feels like I could spends months considering my alternatives and still not be able to decide. Should I just pick popular frameworks and learn them? Are frameworks mutually exclusive - would I use AngularJS with Laravel, for example?

Edit: I should mention that I'm comfortable with HTML/CSS/JS/PHP/etc... but I've never used a framework before and the last time I did any development it was alright to render pages on the server. I'm now coming to understand that RESTful APIs are now king, and that client-side frameworks are designed to pull content from server-side frameworks that expose the API.

Honestly your best bet is to just choose one that sounds good and spend some time trying to build something with it. The big secret is that the major frameworks have more in common with each other than different, so getting your feet wet with one will give you a leg up when it comes to learning any of the others. That said, here's my two cents on a good place to jump in.

In terms of general application architecture, there's certainly nothing wrong with going down the classic path of server rendered pages. The myriad frontend frameworks out there are generally trying to make it easy to create a "Single Page App" where you have a smoother user experience that eliminates pageloads. That type of thing is becoming increasingly important for serious web applications, but it can be complete overkill if you're trying to make a more traditional website.

If you have your heart set on building a SPA, you should definitely be looking at a RESTful API for the backend (if you need non-local data persistence). For that, your best bet is probably to just google your backend language of choice + Rest API framework and take one that looks good. On the Node side, Restify is pretty great, as a suggestion. For the frontend you probably want to look at either Angular, Ember or React.

I've used all 3 and I'm partial to Ember for big projects and React for small projects, but opinions vary wildly there. I've found that Angular and Ember are roughly comparable in what they're attempting to do but Ember's core philosophies align more with my own. Notably, Ember is built around a philosophy of convention over configuration which means that a tremendous amount of the boilerplate in development is taken care of for you.

React is great for small projects because it's just so drat easy to use. It took all of an afternoon to learn React well enough to be dangerous, which is dramatically less time than it took to learn either Angular or Ember. It's missing some of the natural structure of either Angular or Ember, however, so I tend to prefer it for projects I'm not intending to have to revisit every day for months or years.

Lastly, for CSS pre-processors I would definitely recommend starting with SASS. Your other choices are Less or Stylus, pretty much. There are a lot of arguments for why one pre-processor is better than any of the others but the most compelling reason to choose SASS is that both Foundation and Bootstrap have official SASS versions whereas only Bootstrap has an official Less version. That said, if you wanted to forego a pre-processor entirely at first, you would probably be just fine.

Hopefully that helps a little -- I know Web Development can be pretty intimidating now as it seems to reinvent itself every 9 months.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

syrup posted:

React is great for small projects because it's just so drat easy to use. It took all of an afternoon to learn React well enough to be dangerous, which is dramatically less time than it took to learn either Angular or Ember. It's missing some of the natural structure of either Angular or Ember, however, so I tend to prefer it for projects I'm not intending to have to revisit every day for months or years.

Flux helps with this a lot.


Karthe posted:

I'm getting back into web design after a 7-8 year hiatus. My god, I knew things had changed but after reading the OP and talking to a couple friends it's obvious that I'm going to have to toss out all of my old knowledge and start over. Frameworks, CSS preprocessors, HTML5, grid-based layouts...I feel like I'm in way over my head.

How do I pick one framework over another if I don't have any experience with any of them? I checked out some reviews of PHP-based ones and plan on giving Yii a try, but then someone introduced me to AngularJS and React and now it feels like I could spends months considering my alternatives and still not be able to decide. Should I just pick popular frameworks and learn them? Are frameworks mutually exclusive - would I use AngularJS with Laravel, for example?

Edit: I should mention that I'm comfortable with HTML/CSS/JS/PHP/etc... but I've never used a framework before and the last time I did any development it was alright to render pages on the server. I'm now coming to understand that RESTful APIs are now king, and that client-side frameworks are designed to pull content from server-side frameworks that expose the API.

Just pick something and learn it. You're going to want to learn a bunch of different frameworks anyway, and once you get familiar with how stuff fits together, it's pretty simple to pick up the next framework, and then the next is even easier, etc.

Personally, I'd start with React as it has a small API and is easy to learn. Look in to Flux to make React more powerful and manageable for larger projects. Move on to whatever after that.

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
What about Backbone? I usually see Angular, Backbone and Ember as the big 3 for front-end frameworks.

hayden.
Sep 11, 2007

here's a goat on a pig or something
I use PHP in a procedural fashion for all my projects because my goal is to "just make it work" and not really caring whether it's a beautiful example of coding craftsmanship. I am self taught and have only had a few classes of formal programming education, so doing it in an object oriented style isn't really natural for me.

If I was ever interested in using PHP professionally, would I need to change my style to properly use object oriented principles? Or is it acceptable with PHP, even in professional environments, to be procedural?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

hayden. posted:

I use PHP in a procedural fashion for all my projects because my goal is to "just make it work" and not really caring whether it's a beautiful example of coding craftsmanship. I am self taught and have only had a few classes of formal programming education, so doing it in an object oriented style isn't really natural for me.

If I was ever interested in using PHP professionally, would I need to change my style to properly use object oriented principles? Or is it acceptable with PHP, even in professional environments, to be procedural?

If you want to work in an agency where every project is: "Aaaahhhhh.... We over-promised to the client, just shovel this poo poo out as fast as you can...aaaaahhhhhh!" Then procedural PHP is certainly an option. If you want a job at somewhere you'd actually want to work, you'll want to be comfortable doing OOP stuff.

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
There are also a ton of tiny web companies that still run on state of the art 90s technology that tried and failed to figure out those dang classes. In a sense it's a bit like working in a COBOL environment but without the job security or paycheck.

Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India

hayden. posted:

I use PHP in a procedural fashion for all my projects because my goal is to "just make it work" and not really caring whether it's a beautiful example of coding craftsmanship. I am self taught and have only had a few classes of formal programming education, so doing it in an object oriented style isn't really natural for me.
Have you ever worked on a larger project? I have a feeling you haven't reach that point in size where you say "gently caress this poo poo, there's got to be a better way".

hayden.
Sep 11, 2007

here's a goat on a pig or something
I mean, I'm not writing operating systems or anything, but I've made sites with functionality similar to reddit, or ecommerce sites with shopping carts/checking out, and things of similar complexity and have never hated myself. I'm sure the code is terrible and my functions.php file is rather large and unorganized, but it's manageable. Worth noting I'm not selling my services/code, this is all just for my own projects.

Craptacular
Jul 11, 2004

I have an idea for a small website. To be fairly vague, it would take inputs from a form and then return an answer based on what the user entered, from a short list of rules I've created. (e.g. if the user answers "A" to question 1 and "B" to question 2, return "C", else return "D".) I don't need to save the info or have anyone but the website user know the result. I've looked around and none of the 3rd party forms generator websites I've found have the ability for logic on the backend to calculate an answer. They all just dump the results in an email or a .CSV or something.

The site would just be for fun. If I have to do very much coding (or learn a new language or something and write it from scratch myself) it won't be worth my time. The last time I did anything that would qualify as "web development" was in the '90s when I was in college, so I'm really unfamiliar with what's tools are out there now. I'm a sysadmin, not a web designer.

I doubt the site would or could make much money at all (I guess I could put some ads on it or something) so I don't want to spend the money to hire someone else to write it. I already have a LAMP VPS running another personal site, so if this new site could run there it'd be best.

What would be the best method/tool to use to create this?

Craptacular fucked around with this message at 05:11 on Jan 25, 2015

lunar detritus
May 6, 2009


hayden. posted:

I mean, I'm not writing operating systems or anything, but I've made sites with functionality similar to reddit, or ecommerce sites with shopping carts/checking out, and things of similar complexity and have never hated myself. I'm sure the code is terrible and my functions.php file is rather large and unorganized, but it's manageable. Worth noting I'm not selling my services/code, this is all just for my own projects.

If you are the only working in a project you can code however you want, but the second other people get involved poo poo is going to break apart, fast.

I work in one of those "drat, we over-promised and the deadline is tomorrow" agencies and even we managed to transition to OO.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

hayden. posted:

I use PHP in a procedural fashion for all my projects because my goal is to "just make it work" and not really caring whether it's a beautiful example of coding craftsmanship.

You might realize this already, but the reasons for using modern development practices are many and craftsmanship is a side effect of those reasons, not the goal.

an skeleton
Apr 23, 2012

scowls @ u
I want to start an extremely tiny business with my roommate (and one of my best friends). He's good with photoshop/illustrator, used to be in design school (now studying to be an engineer) and generally has a great eye for design and branding. I'm a web developer with about a year of experience, my skills are very generalist (I can do a bit of everything in the stack) but mainly in Angular/jQuery as frontends, and Node/LAMP as a backend. The basic business idea is to start up very simple wordpress websites for businesses, set up hosting/domain names, and offer a bit of support (up to 1 hr/month), and package that with rebranding and graphics by my friend and custom website functionality and design implemented by myself.
I have 2 questions. First is, how awful of a business idea is this to just make a bit of extra cash/month? (Like we take maybe 1-2 clients max/month)

But MAINLY, I am interested in what I should tell my friend to look at in order to interface better with my skills. Should I let him just stick to (and improve at) his general graphics and design capabilities, or is it worth it to push him into learning the basics of CSS and HTML so he could participate in that as well? Should I tell him to look at web design resources and maybe even push him towards more frontend knowledge? I have no idea where the separation between designers and "frontend developers" truly begins, as at my current job (web development internship) we don't have any dedicated designers really.

For those that would discourage me, this is primarily a learning experience but if that learning could include learning how to make a good bit of change on the side, all the better. We can't really devote more than 10 hrs/week max to this because of other commitments so I wouldn't expect it to go anywhere serious really but that could change in the future maybe. I appreciate any random advice regardless.

pigdog
Apr 23, 2004

by Smythe

an skeleton posted:

But MAINLY, I am interested in what I should tell my friend to look at in order to interface better with my skills. Should I let him just stick to (and improve at) his general graphics and design capabilities, or is it worth it to push him into learning the basics of CSS and HTML so he could participate in that as well? Should I tell him to look at web design resources and maybe even push him towards more frontend knowledge? I have no idea where the separation between designers and "frontend developers" truly begins, as at my current job (web development internship) we don't have any dedicated designers really.
It's a really good idea for a web designer to know HTML and CSS. We've moved on from the era where designers simply drew poo poo with Illustrator and it was the developer's job to cut it down to something that works on the web. That's not something that requires a frickin CS masters degree. If he's designing for the web then he should be able to design for the web.

revmoo
May 25, 2006

#basta
This is kind of weird. I used to use the profiler in Safari as I liked the UI. Recently had a bug and fired up Safari and the profiler is missing in place of 'Start timeline recording'. I have not upgraded my browser. If I run 'console.profile()' it simply returns undefined and nothing else happens.

Anyone seen this before?

revmoo fucked around with this message at 20:47 on Jan 26, 2015

kedo
Nov 27, 2007

an skeleton posted:

I want to start an extremely tiny business with my roommate (and one of my best friends). He's good with photoshop/illustrator, used to be in design school (now studying to be an engineer) and generally has a great eye for design and branding. I'm a web developer with about a year of experience, my skills are very generalist (I can do a bit of everything in the stack) but mainly in Angular/jQuery as frontends, and Node/LAMP as a backend. The basic business idea is to start up very simple wordpress websites for businesses, set up hosting/domain names, and offer a bit of support (up to 1 hr/month), and package that with rebranding and graphics by my friend and custom website functionality and design implemented by myself.
I have 2 questions. First is, how awful of a business idea is this to just make a bit of extra cash/month? (Like we take maybe 1-2 clients max/month)

But MAINLY, I am interested in what I should tell my friend to look at in order to interface better with my skills. Should I let him just stick to (and improve at) his general graphics and design capabilities, or is it worth it to push him into learning the basics of CSS and HTML so he could participate in that as well? Should I tell him to look at web design resources and maybe even push him towards more frontend knowledge? I have no idea where the separation between designers and "frontend developers" truly begins, as at my current job (web development internship) we don't have any dedicated designers really.

For those that would discourage me, this is primarily a learning experience but if that learning could include learning how to make a good bit of change on the side, all the better. We can't really devote more than 10 hrs/week max to this because of other commitments so I wouldn't expect it to go anywhere serious really but that could change in the future maybe. I appreciate any random advice regardless.

So you started a small web design firm. I kind of doubt you're going to find someone in this thread who's going to say, "NO THAT'S AN AWFUL IDEA!"

As long as you charge enough money to make it worth your time, then I don't see what could possibly be awful about it. Make sure you take some time figuring out your hourly rate. Also you may want to figure out how you're going to deal with taxes (eg. did you form an LLC or something?)

Does your friend have any CSS and HTML experience at all? If not, and if you/he ever wants to have this be more than a side project for beer money, he needs to learn both. A designer who doesn't know any code is a lovely web designer (aka a print designer).

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
You know, while how you do the technical work is important, how you decide to work your projects is way more important..er. Seriously though, client relations are gonna be your thing so its important you have a clear idea of how you'll get sign off on phases of budgeting, design and development. Make sure you talk about your process early.

It is exceptionally easy to gently caress up your scope of work if you don't effectively manage client expectations, and before you know it you're up to your eyeballs in work you owe the clients and you're stuck on 40+ hour days because your side gig overflowed.

As for him learning HTML/CSS, he'll gain a better understanding of the medium, and from what I've seen of other agencies a pure designer can usually support 2+ developers with enough work to execute on. Basically what it boils down to is your mate might end up twiddling his thumbs if he isn't some part of the dev process, and by having him be knowledgeable of HTML and CSS you can cut down on design having to review and revise development work once its been executed.

v1nce
Sep 19, 2004

Plant your brassicas in may and cover them in mulch.

hayden. posted:

If I was ever interested in using PHP professionally, would I need to change my style to properly use object oriented principles?
Yes. You should understand the language you're working in.

hayden. posted:

Or is it acceptable with PHP, even in professional environments, to be procedural?
No. In an agency you would be junior level, and I don't think anyone else who is worth being hired by, would hire you.

hayden. posted:

I use PHP in a procedural fashion for all my projects because my goal is to "just make it work" and not really caring whether it's a beautiful example of coding craftsmanship. I am self taught and have only had a few classes of formal programming education, so doing it in an object oriented style isn't really natural for me.

From the sounds of what you're saying, you're exactly where I was about 10 years ago. I was writing a simple PHP-based bug tracking system and everything as procedural .php pages and a handful of globally accessible functions, with no framework. Looking back at the code, you can see exactly where I started to understand multi-dimensional arrays, and then the eureka moment where classes finally made sense. Thermopyle is absolutely correct in saying that "beautiful code" is usually a side-product of a well-engineered solution using modern principals.

Now we've successfully shot you down, let's help you out. You're mostly self-taught so that immediately tells me you're best at learning by example - I could spend hours talking about theory but it's not worth a drat, so let's look at a real-world scenario that my own code suffers from right now.

A legacy system I'm fixing up has an awful lot of code like this:
php:
<?
$searchFilter = array('text'=>$_POST['searchtext']);
$userFilter = getFilteredUsers();
$pagination = array('page'=>$_POST['page'], 'page_limit'=>50);
$results = getDocuments($searchFilter, $userFilter, $pagination);
?>
This sort of code is repeated is like 80 different places for about 20 different types of getX requests.

Why is this lovely? At first glance you might think this can't really be optimised further; several disparate elements (POST, getFilteredUsers, pagination) are fed to getDocuments, that can't possibly be simplified further?
Well, first problem is that $pagination array. Every time we write that array we're running the risk of making a typo in the array keys, and it's not DRY at all (Don't Repeat Yourself).

You might think you could optimise that out by writing a procedural function like getPagination;
php:
<?
function getPagination() {
    return array('page'=>$_POST['page'], 'page_limit'=>50);
}
?>
The problem however, is that the geniuses that wrote our system didn't make every pagination request use "page". Sometimes it's "pageNumber" sometimes it's "p". Sometimes some requests don't even pass a variable.

We can fix that procedurally, right?
php:
<?
function getPagination() {
    $page = isset($_POST['page']) ? $_POST['page'] : (isset($_POST['p']) ? $_POST['p'] : 1);
    return array('page'=>$page, 'page_limit'=>50);
}
?>
Hell no. That's an awful solution to a simple problem. This could be picking up variables from anywhere, in the wrong order.. just, no. Don't ever do this.

Class to the rescue! We can define a PaginationSettings class to make this way more reliable.
php:
<?
class PaginationSettings
{
    public $page;
    public $pageLimit;
}

$pagination = new PaginationSettings();
$pagination->page = $_POST['page'];
$pagination->pageLimit = 50;
$results = getDocuments($searchFilter, $userFilter, $pagination);
?>
Ok, so in code terms we basically just defined a Struct. That's not exactly advanced class usage, it's just a statically structured array.
We can actually expand on this class to replace those 80 crappy instances of procedural code by making a much larger "SearchSettings" class, which handles all the variables rather than just pagination:

php:
<?
class SearchSettings
{
    public $page;
    public $pageLimit;
    public $text;
    public $filteredUsers;
}

$searchSettings = new SearchSettings();
$searchSettings->page = $_POST['page'];
$searchSettings->pageLimit = 50;
$searchSettings->text = $_POST['searchtext'];
$searchSettings->filteredUsers = getFilteredUsers();

$results = getDocuments($searchSettings);
?>
Now over in getDocuments we can ensure our code always receives a SearchSettings object. This means our getDocuments object is always passed the right kind of object:
php:
<?
function getDocuments(SearchSettings $settings) 
{
    // Do stuff
}
?>
So, what if the $_POST['page'] value passed is not numeric? We could just cast it to an int. But what if it's negative? or zero? that's not right. How we can fix that? Let's use simple getter/setters on the SearchSettings object;

php:
<?
class SearchSettings
{
    protected $page = 1;

    public setPage($value)
    {
        // Cast the value
        $value = (int) $value;

        // Set page to 1 if page not valid
        $this->page = ($value > 0) ? $value : 1;
    }

    public getPage($value)
    {
        return $this->page;
    }
}

$settings = new SearchSettings();
$settings->setPage(-5);

// echos 1
echo $settings->getPage();
?>
Now your input data is sanitised by the SearchSettings class right away, and you don't have to faff about sanitising your data in every location you use pagination.
This is instantly better because SearchSettings is a re-usable single unit of code. It does all the repeated heavy lifting on sanitising the input data and I can just use it over and over and know that it's reliable.
I also know that within getDocuments() I'll have an instance of SearchSettings to work with. I know the exact methods and members available in SearchSettings, and if I'm using an IDE it can also resolve those values and use auto-complete to make my life easier.

Come to the PHP thread, ask questions, drop us code samples and ask us how we would do it with OO. We're glad to help you not suck at the thing you like doing.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe
I just found out recently that Github has a system in place to host static websites for free. I've been paying for personal hosting for years but since I've just been using it recently to host a static personal page I'm thinking of moving my domains over to Google Domains and switching the couple of static sites I have over to the Github pages system.

If I were to do that, though, what would be my best option for temporary web-facing server access? For example, if I develop a dynamic website locally and want to show it off to someone, should I just spin up an Azure or AWS LAMP instance and pay for however long I need it up to show off my work? Since REST is king is there some kind of website or service I can use if all I want to do is set up a DB to store data and process API calls? This scenario has been the main reason I've kept my hosting around but if I can find a way to gain server functionality only when I need it for less than $60/yr then I'll be a happy camper.

revmoo
May 25, 2006

#basta

Karthe posted:

I just found out recently that Github has a system in place to host static websites for free. I've been paying for personal hosting for years but since I've just been using it recently to host a static personal page I'm thinking of moving my domains over to Google Domains and switching the couple of static sites I have over to the Github pages system.

If I were to do that, though, what would be my best option for temporary web-facing server access? For example, if I develop a dynamic website locally and want to show it off to someone, should I just spin up an Azure or AWS LAMP instance and pay for however long I need it up to show off my work? Since REST is king is there some kind of website or service I can use if all I want to do is set up a DB to store data and process API calls? This scenario has been the main reason I've kept my hosting around but if I can find a way to gain server functionality only when I need it for less than $60/yr then I'll be a happy camper.

Check out Sagonet. I pay 30 bucks a month for a dedicated server. Had it for a couple years now and downtimes have been pretty minimal.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

revmoo posted:

Check out Sagonet. I pay 30 bucks a month for a dedicated server. Had it for a couple years now and downtimes have been pretty minimal.
I've been paying $5/mo for shared hosting, switching to a dedicated server is the opposite of what I'm hoping to achieve :v:

hayden.
Sep 11, 2007

here's a goat on a pig or something

v1nce posted:

Come to the PHP thread, ask questions, drop us code samples and ask us how we would do it with OO. We're glad to help you not suck at the thing you like doing.

Hey man, thanks for your post, I know it was a lot of effort and it was helpful. I didn't see the PHP thread so thanks for the link. I'll try to convert some of my code to OO and post for feedback - thanks!

Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India

Karthe posted:

I just found out recently that Github has a system in place to host static websites for free. I've been paying for personal hosting for years but since I've just been using it recently to host a static personal page I'm thinking of moving my domains over to Google Domains and switching the couple of static sites I have over to the Github pages system.

If I were to do that, though, what would be my best option for temporary web-facing server access? For example, if I develop a dynamic website locally and want to show it off to someone, should I just spin up an Azure or AWS LAMP instance and pay for however long I need it up to show off my work? Since REST is king is there some kind of website or service I can use if all I want to do is set up a DB to store data and process API calls? This scenario has been the main reason I've kept my hosting around but if I can find a way to gain server functionality only when I need it for less than $60/yr then I'll be a happy camper.
A VPS at Digital Ocean costs 5$/month and should be enough for your needs, you are billed by the minute (or was it hour) so you can spin up/kill instances as you please.

caberham
Mar 18, 2009

by Smythe
Grimey Drawer

hayden. posted:

I mean, I'm not writing operating systems or anything, but I've made sites with functionality similar to reddit, or ecommerce sites with shopping carts/checking out, and things of similar complexity and have never hated myself. I'm sure the code is terrible and my functions.php file is rather large and unorganized, but it's manageable. Worth noting I'm not selling my services/code, this is all just for my own projects.

Are you based in East-Asia or something? Because our internet is still terrible even if fiber internet is everywhere. South Korea insists on using IE.

Adbot
ADBOT LOVES YOU

Opulent Ceremony
Feb 22, 2012

Karthe posted:

For example, if I develop a dynamic website locally and want to show it off to someone...

For this particular situation I've used a number of free IaaS providers: Pythonanywhere for Python, OpenShift for Node, AppHarbor for .NET. There are many more out there such that you can find a free provider for any language you want. Databases are a little different, since some providers offer connectivity to a db local to their server, while other places will point you in the direction of integrating the db you want as a service, so keep that in mind when you start local development work.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply