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 3 years ago.
Improve this question
I am building a three.js library and found placing comments and nesting content below the comment in {} made it easy to navigate lots of code. I was wondering if there are other better ways to do this.
Why I would like to do this
the main reason: I can quickly collapse code based on the comments I see above the block scopes. Thereby making it easy to focus on the code I want to change.
I'd only use this in the class initialisation, or very complex methods. Or rather, any areas where there is significant code use with applicable labels for those areas.
I would never scope a section without an appropriate comment accompanying it.
This helps me scale these different sections, especially if they are not something that would scale into a new class or object within reason
Im aware a common problem with code bases is they can be extremely overwhelming, especially when learning them. I think this way of organising code could help make it more approachable and less overwhelming to look at
I would love if there was a feature in IDE's to do something like this
Example of its use:
Collapsed:
Un-collapsed:
Collapsed:
Un-collapsed
I also see this as a compliment to refactoring, whether it's abstracting into functions or more classes. This seems to be agile and deals with uncertainty in the code base especially if you need to neatly layer problems solutions into themselves in ways that would not qualify a new class, or new method.
I would say it's better to break down into separate functions. Adding unnecessary blocks could have surprising consequences for future maintainers. Many editors support using a special syntax within comments to aid code folding and navigation without altering the syntax tree.
[Edit]
Whilst I am really an Emacs-only guy (TM), the docs (https://code.visualstudio.com/docs/editor/codebasics) for Vscode suggest something along the lines of
//#region
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 3 years ago.
Improve this question
I’ve been working with JavaScript for around 4 years. In all the applications I’ve worked on, I’ve never once seen someone use a linked list. Why is that? Data structures are so prominently featured in most learn-to-program courses. Yet I almost never see them used in the real world.
This may not be the perfect explanation of this but I think will give you a fair idea about it.
The scenarios which will make sense to use Linked list on JavaScript
are very rare. You have been working on JavaScript for 4 years when
did you feel a need to use linked lists and not JavaScript Array
The main purpose of using linked list is performance improvements,
when there a is a huge collection of records and frequent changes to
them linked lists help in optimizing that. But that case may not be
true for most of the JavaScript applications. Arrays are pretty much
bale to do that for JavaScript.
Most of the frameworks rely on array's be it React, Vue etc.
In case of linked list methods like unshift, etc. are much faster.
while methods on Arrays, such as push, run considerably faster than
Linked Lists.
The use of linked list is justified when you need to make a lot of
modifications to a huge list — especially when adding or removing
items somewhere other than the end of the list and such cases are rare in
JavaScript/front end.
Last but not the least JavaScript has great support for Arrays but
you will have to code your own implementation or use some library
that provides linked lists implementation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have some limited experience with web scraping using tools like Beautiful Soup and Nokogiri.
My approach thus far when looking for information is to first inspect the HTML elements and CSS tags, then applying the selector. While this works, slight differences/changes among web sites would render the code useless. Also, there have been situations where sites simply don't add the selector tags to their HTML elements, so I once had to resort to the hacky approach of selecting the style property of the element.
How would one devise a scraper that would work across multiple sites? I'm aware that the solution would depend on the context, but is there a general good practice in doing it? I was actually asked in an interview before this question and I had no idea.
I have tried googling but much of what I found doesn't go past the basics, and I don't know where to look. Any help would be appreciated.
It's not clear from your question what exactly you are trying to accomplish. If you want the content of the page (like in an article) - you should try goose, which should give you a leg up. You can also try searching for conventional web page approaches like meta tags.
Either way, you should remember that this is the World Wild Web, and the HTML is a very forgiving language, which lets people design pages which are very hard to read by a machine. Even big sites sometimes have their proprietary breaks from conventions, which forces exceptions in your code in order to read them. Site logic may also conflict with conventional logic, or other major site.
This means that your code would probably consist of a lot of use-cases and exceptions.
My suggestion to you is to keep samples of pages of sites you want to scrape, and have a unit test which iterates over them and verifies the scraping results. This way, each time you find a new quirk, you can add it to your collection, and be certain that if the change you made broke some other site's scraping, you would know about it.
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 5 years ago.
Improve this question
I'm a javascript developer for a year now but I lack some architectural/engineering background as I was lazy in college.
In the beginning I was writing small scripts with relatively small and simple objects.
As my projects are getting bigger, I tend to use bigger and more complex objects which reflect 4-5 basic data structures which I use in various parts of my application.
I'm looking for the best way to document/keep track of the structure of these objects because when I make a new module that will use one the them, I have to make use they will respect their structure and interface so the previous/next modules using them won't break.
What I was doing until now was to document an object like this:
/** description of the object/data structure
var myObject = {
property1: "", //explain here what is this property and what type it is (number,string etc)
property2:blabla,
etc...
}
*/
This ended up to huge parts of comments which are ugly in the code and distructing too.
Could you suggest some ways of either visualize my objects/data structures or document them so I can fast recall how they should look like?
There are several tools for that out there, but here's a few:
njsdoc - "NJSDoc is a Documentation tool for JavaScript that
works by executing code."
JSDuck
jsdoc
YUIDoc
Natural Docs
DocumentJS
I think you might be interested in the information provided here:
http://javarevisited.blogspot.gr/2011/08/code-comments-java-best-practices.html
It's about java but they can also be applied in Javascript (some of them)
Summarizing the key points:
Focus on readability of code
Don't write what code is doing
Always write why you are writing this piece of code
Always try to finish your comment in as few words as possible
Always put comment while commiting code in source control repository
Give your code to fellow developer to understand
Regarding your code, I have to say that from personal experience I find that writing comments on the previous line rather than next to the code, is more clear and easy to read.
E.g.
var myObject = {
//explain here what is this property and what type it is (number,string etc)
property1: "",
property2:blabla,
etc...
}
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
This might seem like an odd question, but I find that javascript is either easy or hard, depending on how you've coded the HTML. Is there a book or website that goes into detail about successful patterns and guidelines for coding HTML, so that it's very workable with jQuery, css and complex ajax applications? Like solid rules to live by.
Again, seems like a weird question maybe, but I don't know a better way to ask it. I just find myself always having to change the markup as new things come up - like switching between a hidden input element to a data attribute... or putting more ids or taking away ids - and I guess I arrive at the right way to do it, but I'm curious if someone has bothered to analyze this and came up with some great guidelines, standards and patterns so that the resultant HTML is right the first time.
Thanks
The first thing if you want to code some clean HTML that will be easy to work with is to make sure that your code is valid against an official DTD, HTML4 (here) or XHTML (here).
Then use id and class in a proper way (id only for unique section and class for repeatable ones) and name them correctly according to the context so they are easily reachable.
From my experience, I would actually suggest that, when it comes to large projects and professional JavaScript coding, the goal actually becomes to decouple the JavaScript code from whatever HTML it lives in.
As mentioned already, as long as you are using well formed HTML (DTD compliant), a library like jQuery shouldn't have any trouble operating on it. However, as best practice, I would recommend striving to isolate and encapsulate dependencies, whether they be because of HTML structure or just other chunks of JavaScript code.
the best way is to develop the html and javascript together. That way you can adjust the document structure to whatever you need.
This article seems to answer my question:
http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.
Notes:
I can't use any JS library such as jquery etc..
I am a big fan of Peter Michaux's guide on how he writes javascript widgets
Also useful are Christian Heilmann's script configuration and the module pattern
Those are generic javascript articles and aren't specific to a single library
Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.
(function() {
//Your code goes in here
})();
Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.
If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford
Finally, remember to run your completed code through a code quality tool