Many people won't like this idea, but I am advocating this wherever I can: regardless of the programming language and environment, if they don't have any experience and if there are maintenance tasks which come up from real world bug reports of customers of yours, try to make sure they get assigned to that kind of task at least for 30-40% (+) of their time. "Here's the bug report, have a look at it, solve it. If you don't know what it's all about, communicate with experienced colleagues, google it, whatever". Real work on real problems, no toys, at least: not only toys. Make sure, too, that someone with lots of experience has a look at what they were doing before it gets released and shipped to the customer, of course. Make sure the new colleague gets honest feedback on what he did from colleagues and customers. Choose these tasks carefully in order to not overburden them, but keep in mind that some day you want them to do their work independently.
Doing bug fixing is learning on the job which lets them work on code which get's actually executed and has some relevance (otherwise there would be no bug reports) and will show them in many examples how to not do it.
The focus is automatically put on pain points. They'll start learning those details which are actually causing trouble. It also puts real responsibility onto their shoulders right from the beginning, which (while maintenance as such is not really attractive) can be rather motivating if they get it done to the satisfaction of the customer/end user. Going through what they did will be taken more seriously by your seniors cause they know the impact if things go wrong, and that way it will also simplify integration into the team, cause it will make them talk to each other automatically, as well.
The point is not to set them productive from the first moment (as it might look like). The point is to make sure that they know they are supposed to do something valuable right from the first moment, and to put a focus on what matters most without the need to actually create a list.
I do have some years of experience working every now and then with people coming directly from college into their new developer job, and the worst results I got to see was usually when someone without at least some experience in maintenance was asked to do new application development. Just make sure they always have someone they can ask for support if they feel lost.
Nested loops are fine as long as they describe the correct algorithm.
Nested loops have performance considerations (see @Travis-Pesetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.
Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:
Player chosen_one = null;
...
outer: // this is a label
for (Player player : party.getPlayers()) {
for (Cell cell : player.getVisibleMapCells()) {
for (Item artefact : cell.getItemsOnTheFloor())
if (artefact == HOLY_GRAIL) {
chosen_one = player;
break outer; // everyone stop looking, we found it
}
}
}
While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return
instead of break
. So a break
with a label is a faint code smell; pay extra attention when you see it.
Best Answer
There are mostly two reasons to put scripts outside HTML code:
Server-side, separation of concerns and clean code. You quickly understand why you must be very careful with that when you see some PHP code which, in the same piece of code, mixes PHP, SQL queries, JavaScript, HTML, CSS and Base64-encoded images embedded in CSS.
Client-side, external scripts are easily cached. If your HTML is a dynamic page, it may change often. By putting scripts outside HTML, you allow your visitors to download this content once, and never refresh it again (well, not never, but rarely). In the same way, if two static or dynamic HTML pages share the same script, putting it in an external file will allow to download it once.
▪ The first point is valid in all cases. You'll never be able to justify that it's cleaner to mix everything in one file. Why frameworks allow this? Because there are situations where you don't care about clean code:
If I write a small page I'll throw away for sure, and I must do it quickly, it's not so bad to put everything in one file.
If I want to show a short example to my colleague (by putting a single file to a document management system platform) or publish it to a blog¹, having everything in one file can be acceptable and useful too.
If I must create a small prototype before developing the application for a customer, best practices don't matter at all, while reducing cost is critical for source code which will be thrown in all cases.
▪ The second point, on the other hand, explains why there are perfectly valid cases to put scripts in HTML:
The script changes on every change of a dynamic page. In this case, why would you force the client to download the HTML file, then the separate file containing the script (i.e. doing two requests, which is a waste of time and resources which must not be neglected for the websites of some scale), while you can provide the same thing through one request?
The static HTML page uses the script which is not shared by any other page of the website. It's a perfect candidate too, if you want to avoid doing two requests².
Now, returning to the first problem (clean code), you can serve scripts in HTML and have clean code server-side (for example by using a template which will inject, at runtime, the script which is written separately). This, of course, requires some level of expertise, and as I said before, combining scripts and HTML is needed only on high-scale websites. This means that if you're seeing an inexperienced developer mixing scripts and HTML when doing a personal website, it's a good sign that something is wrong.
¹ Personally, I'm against this practice. More we share through our blogs code which does not follow best practices, more the less experienced developers would be encouraged to copy it and to believe that this is the right style.
² With appropriate caching configuration, two requests will happen only for users with empty cache. For others, you can set the expiration date for the far future, and the browser will avoid the request which will return the
304 Not Modified
response.