How To Clean Code Js Composer

Posted By admin On 23/08/21
  1. Js Composer Wordpress

'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.'

By default Composer installs all package files under the vendor directory. If you want to install asset files in the Web document root directory, you need to resort to another solution. This Asset Manager package is a plugin that extends Composer to install any package files outside the vendor directory. Additionally, it can also read the user names and passwords from a configuration file, so. Organize and tidy up your style sheets with this free online CSS editor with syntax highlighting and many useful code cleaning options. Load the demo text and try the style cleaning options. Perform cleaning options one by one pressing the desired items in the list and set indentation of the lines with the Organize button to make the text more. Javascript Web Development Front End Scripts Reset or clear a form using JavaScript, with the reset method. The reset method sets the values of all elements in a form like clicking the reset button. A powerful clean-code website builder coupled with knowledge-packed API – integrate the plugin to your WordPress theme to sell faster and earn more. Go Premium Visual Composer Hub.

As eloquently noted by Robert Martin in his book “Clean Code,” the only valid measurement of code quality is the number of WTFs per minute as represented in the below diagram:

“Are we debugging in a panic, poring over code that we thought worked? Are customers leaving in droves and managers breathing down our necks? How can we make sure we wind up behind the right door when the going gets tough? The answer is: craftsmanship.”

There are countless examples of bad code bringing companies down or making a disaster of an otherwise good product. Some famous and (fairly serious!) examples include space rockets being mislaunched due to a badly transcribed formula (a single line was missing that meant the rocket had to be terminated 293 seconds after launch ultimately costing around 20 million dollars), or a race condition bug in a medical machine that caused the death of three people, when they were exposed to lethal radiation doses – and you think you’ve had a bad day!

The quality of the code is directly correlated to the maintainability of the product. Programming productivity has an arccotangent relationship with time.

As features are added and changes are made, time passes and the original developers move on or forget some of the project details, or if the quality of the code is not good, changes become increasingly risky and more complex.

Programmers are really authors, and your target audience is not the computer it is other programmers (and yourself). The ratio of time spent by a programmer reading code to writing code is normally 10 to one. You are constantly reading old code in order to write new code.

Writing clean code makes the code easier to understand going forward and is essential for creating a successful maintainable product.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”

Remember the second law of thermodynamics? It implies that disorder in a system will always increase unless you spend energy and work to keep it from increasing.

Similarly, it takes a hell of a lot of more effort to write clean code. Writing clean code is hard work. It needs a lot of practice and focus during execution.

To be able to write clean code you should train your mind over a period of time.The hardest part is simply making a start, but as time goes by and your skillset improves, it becomes easier. Writing clean code is all about readability, so even the smallest things like changing you habits for naming variables make the biggest difference.

We should be consciously trying to improve code quality and decrease code entropy. The boy scouts of America have a simple rule:

“Leave the campground cleaner than you found it.”

The same rule applies to programmers. Always leave the checked in code cleaner than the code that is checked out.

1. Variable and method name

1.1. Use intention-revealing name

This is bad:

protected $d; // elapsed time in days

This is good:

protected $elapsedTimeInDays;
protected $daysSinceCreation;
protected $daysSinceModification;
protected $fileAgeInDays;

1.2. Use pronounceable name

This is bad:

public $genymdhms;
public $modymdhms;

This is good:

public $generationTimestamp;
public $modificationTimestamp;

1.3. Use namespaces instead of prefixing names

Most modern programming languages (including PHP and Python) support namespaces.

This is bad:

class Part {

private $m_dsc;

}

This is good:

class Part {

private $description;

How to clean code js composer for beginners

}

1.4. Don't be cute

This is bad:

$program->whack();

This is good:

$program->kill();

1.5. Use one word per concept

Be consistent. For example, don’t use getand fetchto do the same thing in different classes

1.6. Use solution domain names

People reading your code will be other programmers so they understand solution domain terminology, so make the most of it. - For example, jobQueue is better than jobs

1.7. Use verbs for function names and nouns for classes and attributes

class Product {

private $price;

public function increasePrice($dollarsToAddToPrice)

{

$this->price += $dollarsToAddToPrice;

}

}

2. Better Functions

2.1. The smaller the better

2.2. A function should only do one thing

2.3. No nested control structure

2.4. Less arguments are better

More than three arguments are evil. For example:

Circle makeCircle(Point center, double radius);

Is better than

Circle makeCircle(double x, double y, double radius);

2.5. No side effects

Functions must only do what the name suggests and nothing else.

2.6. Avoid output arguments

If returning something is not enough then your function is probably doing more than one thing.

For example

email.addSignature();

Is better than

addSignature(email);

2.7. Error Handling is one thing

Throwing exceptions is better than returning different codes dependent on errors.

Asking for forgiveness is easier than requesting permission. Use try/catch instead of conditions if possible

2.8. Don’t repeat yourself

Functions must be atomic

3. Comments

3.1. Don’t comment bad code, rewrite it

3.2. If code is readable you don’t need comments

Composer

This is bad:

// Check to see if the employee is eligible for full benefits

if ($employee->flags && self::HOURLY_FLAG && $employee->age > 65)

This is good:

if ($employee->isEligibleForFullBenefits())

3.3. Explain your intention in comments

For example:

// if we sort the array here the logic becomes simpler in calculatePayment() method

3.4. Warn of consequences in comments

For example:

// this script will take a very long time to run

3.5. Emphasis important points in comments

For example:

// the trim function is very important, in most cases the username has a trailing space

3.6. Always have your PHPDoc comments

Most IDEs do this automatically, just select the shortcut.

Having doc comments are especially important in PHP because methods don’t have argument and return types. Having doc comments lets us specify argument and return types for functions.

If your function name is clear, you don’t need a description of what the method is doing in the doc comment.

For example:

/**
* Return a customer based on id
* @param int $id
* @return Customer
*/
Public function getCustomerById($id)

3.7. Any comments other than the above should be avoided

3.8. Noise comments are bad

For example:

/** The day of the month. */
private $dayOfMonth;

3.9. Never leave code commented

Perhaps this is the most important point in this whole article. With modern source control software such as Git you can always investigate and revert back to historical code. It is ok to comment code when debugging or programming, but you should never ever check in commented code.

4. Other code smells and heuristics

There are a lot more that you can do to identify and avoid bad code. Below is a list of some code smells and anti-patterns to avoid. Refer to the sources of this blog for more information.

  • Dead code
  • Speculative Generality - no need “what if?”
  • Large classes
  • God object
  • Multiple languages in one file
  • Framework core modifications
  • Overuse of static (especially when coding in Joomla!)
  • Magic numbers - replace with const or var
  • Long if conditions - replace with function
  • Call super’s overwritten methods
  • Circular dependency
  • Circular references
  • Sequential coupling
  • Hard-coding
  • Too much inheritance - composition is better than inheritance

Conclusion

As I mentioned before it takes a lot of practice and effort to write clean code. However it is definitely worth the extra effort. Writing clean code is integral to the success of the project.

Adopting coding standards such as (PSR-2) will make it a lot easier to write clean code. If you are using PhpStorm (IntelliJ) you can set your coding style to PSR-2 and the IDE’s internal code inspector will warn you when your code is not good.

Also adopting Code Sniffer will help a lot. Other subjects.com. You can install Joomla’s CodeSniffer and only commit when it is green.

How To Clean Code Js Composer

Perfect code is not always feasible but It is important to try and write code as cleanly as possible. Programming is an art. Let other programmers enjoy your code.

To finish, I would like to share a telling quote from Robert Martin:
“[Most managers] may defend the schedule and requirements with passion; but that’s their job. It’s your job to defend the code with equal passion”

How to clean code js composer free

Sources

  1. “Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin

Contents

Making Composer Better with Special Plugins

Installing Asset Files Outside the Vendor Directory

Using the Composer Asset Manager Plugin

Implementing New Types of Installation Actions

Automatic Login for Repositories that Require a Password

Conclusion

Making Composer Better with Special Plugins

Composer is an excellent tool for installing packages needed by a PHP project. However, it is not perfect. There will always be needs that some projects have, which may not be addressed by the current Composer version.

Fortunately, Composer developers had the great idea to make it extensible with third party plugins. This way, Composer can address the needs of a much greater range of PHP projects.

Composer plugins are basically like regular packages, but they implement the Composer plugin API. This API allows providing new features by altering the package installation process in useful ways.

Installing Asset Files Outside the Vendor Directory

One need that currently Composer does not address is the installation of the so called asset files.

Assets are files that Web applications need, for instance to run on the browser side. For instance, Web applications need to serve JavaScript libraries, CSS files, images, etc.

Currently, Composer installs all package files inside the vendor directory. However, asset files need to be installed below the current site Web document root, so they can be served to the user browser. The Web document root is usually not inside the vendor directory.

If you need to install packages with JavaScript files, for instance from JSClasses Composer repository, an alternative solution needs to be used to make the JavaScript files from those packages be installed in the right directories under the Web document root.

This is where this PHP Composer Asset Manager package comes into action. One of the main needs that it addresses is the installation of asset files in the right Web document root directory or sub-directory.

Using the Composer Asset Manager Plugin

The first thing you need to do to use this Composer Asset Manager Plugin package is to add it to your project composer.json file.

As you may see below, you need to added the packages phpclasses/assets in the require section of your project. That is a dummy package that triggers the loading of this Composer Asset Manager plugin package.

For instance, the actions below tell the plugin to install all JavaScript files in the directory web/js . The plugin will create any sub-directories if they do not exist.

As you may see above, the definitions of where you want your assets go should be included in the project extra section. It must contain an entry named assets that defines whatever installation actions you need to be performed.

The assets section needs to specify which packages of all you are using in your project have the asset files you want to be installed. In the example it just installs assets from the package jsclasses/fast-content-loader.

Each package may have specific installation actions associated. Those actions need to be defined in a section named actions inside the package definitions. In the example that section is empty for the package jsclasses/fast-content-loader. This means there are no package specific installation actions to be performed.

However, you can define actions for all packages in the assets section named actions. That is a section may list one or more installation actions to be executed.

All actions must have a type entry defined explicit or implicitly. It defines the name of the action. Currently only the action copy is supported. Other actions may be supported in the future.

The copy action tells the plugin to copy asset files in a given directory defined by the target entry. The pattern entry defines a regular expression that matches the names of the files to be copied.

To simplify you can define that action with a simple entry named js-target. You may as well use a css-target entry for copying CSS files or image-target entry for copying JPEG, PNG and GIF image files.

Implementing New Types of Installation Actions

As mentioned above, in the future it will be implemented other types of actions for installing asset files, like copying and minifying JavaScript or CSS files, optimizing image files, or creating sprite images from a set of image files.

Meanwhile you can implement your own custom installation actions by creating a new plugin that extends this one. Just create a new package with a class that extends the class mlemosComposerAssetInstaller and override the function installAction.

Make sure the name of the installation actions you implement have an unique prefix, like some-action-namespace/ in this example. This is meant to avoid action name collisions in case you implement actions that have the same name as future actions implemented by this plugin.

Automatic Login for Repositories that Require a Password

Another need that currently Composer does not address well is automatic login on repositories that require authentication. Currently Composer requires that you manually enter a user name and password every time it accesses those repositories. That is a tedious task.

In the past it was necessary to create a patched version of Composer to read repository user names and passwords from a separate configuration file. However, that is not an ideal solution because not everybody can use a patched version of Composer.

Fortunately this plugin also provides a solution for that problem. It can read a configuration file and set the necessary user names and passwords for repositories that require authentication. This way the user is no longer prompted for the user name and password in the shell (unless the user name and password are incorrect).

Currently the configuration file needs to be located in the project directory with the name auth.json. For security reasons you may want to remove that file after you install your project in a production server, to minimize security damages in case your server is compromised.

Here is a sample authentication configuration file:

It is possible that this feature of automatic authentication of this plugin is no longer needed in the future because there is a patch submitted as pull request by Stephan Hochdörfer to implement a similar feature built-in Composer. Until this patch is merged into Composer, this plugin feature will continue to be useful.

Conclusion

As you may have learned from this article, it is possible to extend Composer to make it even more useful than it is today. Many thanks to Composer authors Jordi Boggiano, Nils Adermann and many others that contributed to develop Composer and make it extensible with third party plugins.

I would like to thank in particular to Stephan Hochdörfer of bitExpert for being so helpful with his knowledge, which helped making this plugin possible.

This plugin is Open Source and is available for download and installation without requiring you to be registered in the PHP Classes site. Feel free to use it and copy it to your own environment if necessary. If you redistribute it, it would be appreciated if you link to this article, so other users may learn more how to use the plugin.

Js Composer Wordpress

Other than that, please post a comment to this article now if you have comments, questions or suggestions you would like to make about the plugin.