Tag: Programming

Learning Java Using JShell

Java programming is not hard to learn, but proficiency in the language takes time, effort, and a lot of practice. JShell can significantly reduce the time required to learn many of the core features of the Java programming language.

As a prototyping tool, JShell makes it easy to write and develop a code snippet without having to compile and run a complete program to test it.

What is JShell?

Java 9 witnessed the introduction of JShell, and it persists with the latest releases. It is a read-evaluate-print-loop (REPL) tool that can be used to write and execute Java code, load code from a file, save code snippets, and more, all using a simple command-line interface.

Learning Java using JShell

Learning Java using JShell, by Christoph Tornau (Packt Publishing), is an excellent introduction to the Java programming language using JShell and is especially suited for beginners. While it does not cover all the formalities of writing a full Java application, it does cover the fundamentals of the language itself including:

  • Data types: int, byte, double, float, String, char, and boolean.
  • Variables: Declaring variables.
  • Operators: Using logical, conditional, compound assignment and unary operators.
  • If and If-Else statements
  • Switch expressions: Note that switch expressions are a preview feature and are disabled by default. Run JShell with “–enable-preview” to enable switch expressions.
  • Loops: While, Do-While, For, and ForEach
  • Arrays
  • Methods
  • Object-Oriented Programming
  • Classes and Objects

Using JShell to teach the fundamentals of Java is a smart approach taken by Christoph. It eliminates the distractions of having to choose from a variety of editors or IDE’s (Interactive Development Environment) and provides a simple “clutter-free” means to focus on Java alone.

The video course is ideally suited for beginning Java programmers and serves as a soft introduction to using JShell with Java.

Interestingly, Learning Java using JShell does not spend much time covering many of the JShell commands and key combinations that make using it that much more effective. What follows is a brief introduction to JShell. I have also included some helpful links to additional articles and resources at the end of this post.

Java Version 9 or Later

To run JShell, you must have JDK9 or later installed on your computer. You can get the latest version of the JDK for your operating system from Oracle’s “Java SE Downloads” page.

You can check the installed version of Java by typing “java -version” at the command prompt in a terminal session as pictured below:

Running JShell

To run JShell on windows, start a terminal session by typing “cmd” in the search bar and click on the “Command Prompt – app.”

When the command prompt appears, type “jshell” and press enter. Some Java language features, such as switch expressions, are available in preview mode only and disabled by default. They can be enabled by typing “jshell –enable-preview” at the command prompt as pictured below:

For a brief introduction to JShell, type “/help intro” without the quotes as suggested when the jshell opens in the terminal. For a list of commands, type /help. Oracle’s Java Platform, Standard Edition Java Shell User’s Guide, provides an in-depth review of JShell’s core features.

You can set the feedback mode using the “/set feedback mode” command where the mode is replaced by either “verbose,” “normal,” “concise,” or “silent.” For example: “/set feedback silent” displays the absolute minimum amount of information. You can also use the /set command to create customized feedback and prompt settings.

For help on a specific command or subject, type /help followed by the command or subject of interest to you. For example, when you enter “/help /list” at the jshell prompt, the requested help information appears on the screen as pictured below:

JShell Commands

The JShell commands available in version 12 as displayed on our system are as follows:

  • /list [|-all|-start]
    • list the source you have typed
  • /edit
    • edit a source entry
  • /drop
    • delete a source entry
  • /save [-all|-history|-start]
    • save snippet source to a file
  • /open
    • open a file as the input source
  • /vars [|-all|-start]
    • list the declared variables and their values
  • /methods [|-all|-start]
    • list the declared methods and their signatures
  • /types [|-all|-start]
    • list the type declarations
  • /imports
    • list the imported items
  • /exit []
    • exit the jshell tool
  • /env [-class-path ] [-module-path ] [-add-modules ] …
    • view or change the evaluation context
  • /reset [-class-path ] [-module-path ] [-add-modules ]…
    • reset the Jshell tool
  • /reload [-restore] [-quiet] [-class-path ] [-module-path ]…
    • reset and replay relevant history — current or previous (-restore)
  • /history [-all]
    • history of what you have typed
  • /help [|]
    • get information about using the jshell tool
  • /set editor|start|feedback|mode|prompt|truncation|format …
    • set configuration information
  • /? [|]
    • get information about using the jshell tool
    • same as /help
  • /!
    • rerun last snippet — see /help rerun
  • /
    • rerun snippets by ID or ID range — see /help rerun
  • /-
    • rerun n-th previous snippet — see /help rerun

JShell Subjects

To learn more about a given subject, enter one of the subjects from the list below after the /help command. For example: /help intro

  • intro: an introduction to the JShell tool.
  • keys: a description of line editing support to navigate and edit snippets and commands.
  • id: a description of snippet IDs and how to use them
  • shortcuts: a description of keystrokes for snippet and command completion, information access, and automatic code generation
  • context: description of the evaluation context options for /env /reload and /reset
  • rerun: a description of ways to re-evaluate previously entered snippets
  • shortcuts

The “jshell>” prompt doesn’t offer help unless you ask for it. There is more to this seemingly “archaic” interface than first meets the eye. and it is worth taking the time to look further.

Why use JShell?

As a developer, it is convenient to test a snippet of code without having to formally compile and run it to see if it’s going to work. Working with APIs can also be a challenge. and JShell is a perfect environment to learn more about them.

JShell is even integrated into the JetBrains IntelliJ IDE so you can reap all the benefits and productivity gains it has to offer and makes knowing what JShell is and what it can do for you even more relevant.

Some seasoned Java programmers are not aware that JShell even exists. I can only stress the importance of staying current with updates when they occur as they usually introduce new features and ways to make writing code that much more efficient.

As I’ve said many times before, “There’s always a better way and more than one solution.” Learning Java using JShell by Christoph Tornau is yet another example of that.

Until Next Time – STAY lean!

Related Articles and Resources

Advertisements

Machine Learning Algorithms

The next book adventure is “Machine Learning Algorithms” by Giuseppe Bonaccorso, Packt Publishing, (Jul 2017), 360 pages.

My initial preview suggests there is a lot to learn and so little time. Perhaps its time to let machines do more of the work.

Until Next Time – Stay lean!

Related Articles and Resources

  • Python Machine Learning – Second Edition by Sebastian Raschka and Vahid Mirjalili, Pack Publishing, (Sep 2017).
  • Machine Learning for Developers by Rodolfo Bonnin, Pack Publishing, (Oct 2017).
  • Statistic for Machine Learning by Pratap Dangeti, Packt Publishing, (Jul 2017).
  • Mastering Java Machine Learning by Dr. Udav Kamath and Krishna Choppella, Pack Publishing, (Jul 2017), 556 pages.
  • Java Machine Learning for Computer Vision (Video) by Klevis Ramo, Pack Publishing, (Jul 2018), 5 hours 6 minutes.
  • Machine Learning in Java – Second Edition – by AshishSingh Bhatia and Bostjan Kaluza, Packt Publishing, Nov 2018, 300 pages.

Lean Code and Comments

When I learned to program, hardware and storage were scarce. It is imperative for the code to be tight and the speed of execution is and remains a first and foremost concern. Achieving this isn’t always easy and often requires some very sophisticated programming techniques.

If there was ever a time or place to demand comments, a clever or complicated code sequence is it. Many books and courses will tell you to comment on your code and many experienced programmers do an excellent job of doing so. Writing clear and useful comments is as much a skill as it is a discipline.

The coding style where indentation, line length (80 characters maximum), avoiding the use of global variables and writing single purpose functions also help to understand and debug the code. Make your comments relevant and don’t restate what should already be clear.

Some will argue that good well-written code is self-documenting although my experience strongly suggests otherwise. Well-worded variable names are helpful; however, their intended purpose may not always be clear. Functions, subroutines, or classes may also have well-defined names yet arguments and/or parameters and results may not be.

I recently found myself having to debug a program I wrote over a year ago. The application was working on all machines but one. I learned that the client replaced the computer with an old legacy system. Fortunately, I have been working with PC’s for more years than I care to admit and understand what was happening and why.

Needless to say, were it not for the comments, fixing the issue in the actual code would’ve been a daunting task otherwise. The very sections of code that were to occupy my time were cause for previous visits. The comments clearly describe what the code sequence is supposed to do and the potential caveats to avoid.

Good Meaningful Comments

All of my code modules have an opening comment block that, aside from the author(s), date created, purpose, and revision, provide specific details regarding the methods/techniques and how they are used in the code to follow. Even the method of versioning the module is clearly outlined.

Complete history with version/revision number, date, and description of the changes accompanies the opening block. The specific changes are dated and documented in the revised code segments as well. Dating the changes in my code serves as a frame of reference and allows me to better recall the events that triggered the changes in the first place.

/* System.out.println(“Don’t use block comments to block out code\n”); */

Rule of Thumb

A good “rule of thumb” is to provide sufficient comments to reteach yourself or to teach others what the code is supposed to do. I also provide ample warnings and advise of possible side effects that code changes may introduce.

If there is ever a place to serve yourself best in your coding skills, it’s in the comments. Everything that appears fresh today will be everything but fresh a year from now. A well-documented program requires focus and discipline, but the effort will make the debugging process so much easier when you visit your work in the near or distant future.

Until next time, STAY lean!

Related Articles and Resources

Python DoWhy

Microsoft’s DoWhy Library for Python greatly simplifies the task of estimating causal effects.  If you or someone you know is involved in data analysis, it is worth your while to see what DoWhy can do for you.

I have spent a little time working with the library and although I have no coding examples at this time, the powerful nature of this library prevents me from waiting to share it.

Visit the DoWhy github page for more details on the DoWhy library.  The information and documentation presented on the site provide sufficient detail to download and start working with the DoWhy library.

Until Next Time – STAY lean!

Versalytics-Logo

Related Articles and Resources

  • https://github.com/Microsoft/dowhy

 

 

Lean Code and JavaScript

As I’ve said many times before, “There’s always a better way and more than one solution!”  The sentiments of this statement are echoed by the many ways a solution can be programmed using any of the many available languages including JavaScript.

Although I’ve been working with JavaScript for a number of years, I continue to discover interesting nuances in the language.  The learning never stops and is an inherent part of the intrigue that is programming.

While many solutions exist, some techniques and methods of programming are preferred over others.  Once you’ve mastered the basics of JavaScript, the programming challenges you are prepared to accept will inevitably become more complex.

Learning to address various coding problems is directly dependent on the knowledge and tools with which you are already familiar.  Be reminded however that just because they work doesn’t mean they are as effective or as efficient as they could be.

On this premise, I consider programming as a learning continuum.  Books and videos tend to serve as my primary sources of learning and reference.  In the case of JavaScript, one such book is:

Effective JavaScript presents detailed examples of what NOT to do and why followed by effective solutions to resolve the concerns identified.  The examples are succinct and clearly demonstrate complex ideologies in a simple, straightforward manner.  I have learned more from this book than most could begin to offer.

Learning how to code is only one aspect of programming.  Understanding how your code (or the language) works and why is another.  Effective JavaScript does both with a greater emphasis on the latter.  You will save yourself many hours of debugging your code when you have a clear understanding of what JavaScript can do when used correctly.

Of course, there is always Google, however, the information is typically solution oriented without the full benefit of scope or context.  As I’ve said before, “Be careful who teaches you.”  Unless you understand the code you are using, resorting to a “searched” solution may be cause for more trouble than it’s worth.

Until Next Time – STAY lean!

Versalytics

Related Articles and Resources

 

Lean Code – Web Graphics

Graphics and Animation

MultipleMachines

Over the past few weeks, I’ve been working on an application to simulate multiple production processes working in tandem and decided to re-introduce myself to the world of animation.  In its present state, the application is working quite well using just the core HTML5, CSS3, and JavaScript web languages.

The HTML5 canvas makes it easy to render graphics where CSS3 and JavaScript are used to manage and manipulate various animation effects.  Aside from the canvas element, OpenGL, WebGL / WebGL 2.0, SVG (Scalable Vector Graphics) and SMIL (Synchronized Multimedia Integration Language) or VML (Vector Markup Language) are also options to render graphics to your web page.

While HTML canvas is relatively easy to understand, working with SVG can be a little more complicated.  Fortunately, a number of packages exist that can be used to create our shapes and several JavaScript libraries are available to work with the generated SVG files.

Creating Vector Graphics

Adobe Illustrator is a great product but also demands a monthly subscription fee which, unless you’re a professional web developer, isn’t something too many of us want to add to our monthly budget.

An SVG Editor (by henoc) is available for Microsoft’s Visual Studio Code that can simplify the task of working with SVG.  You may also wish to consider using:

  • Vectr, a free cross-platform vector graphics editor, that allows you to create scalable vector graphics in 2D.   You can use the online version or download a copy of the application for your operating system (Windows, Mac, Linux, or Chromebook).
  • Inkscape a professional vector graphics editor for Windows, Mac OSx and Linux.  Free and open source.
  • Gravit Designer is a very capable and powerful design tool.  You can use the online version or download a copy of the application for your platform (Mac OSx, Windows, Linux, and Chrome OS).
  • There are also a number of online SVG editors – easily found with a simple Google search –  where you can create simple 2D SVG files online and save them locally to your machine.
  • It is also worth noting that the WebStorm IDE by JetBrains, our IDE of choice for web-based applications, also provides a level of support for working with SVG files.

JavaScript Libraries for SVG

While we can manipulate and work with SVG data directly using CSS and native JavaScript, a number of libraries are available that provide significantly enhanced functionality and effects.  Some commonly used libraries for working with SVG data include:

JavaScript libraries will save you a tremendous amount of time and effort so you can focus on your application.  It is also possible to render 3D models using SVG with more highly capable JavaScript libraries.

Browser Support and Local Files

Browsers provide varying degrees of support for today’s web technologies and knowing what is or is not supported will determine whether your application will run as intended.  IE (Internet Explorer) is one browser that is typically lacking in support for capabilities such as CSS transforms on SVG elements.

Remember:  Browsers are NOT created equal.

FailedtoLoad-CrossOriginRequests

Some browsers like Chrome do not permit access to files or data on your local drive and you may receive an error similar to the one pictured above if you make the attempt.  Fortunately, I’m using the WebStorm IDE by JetBrains and have the option to run code either as a local file or using the built-in server.

Use a local server if you are using Chrome or another browser that does not provide access to data files that are local to your machine.

Responsive Websites

A responsive website is one of the more significant and noteworthy advantages of using SVG for your graphic elements.  Scaling your site to accommodate devices of all shapes and sizes is less of a challenge with SVG.   Images retain their original quality regardless of scale, can be created or edited with any text editor, and can be manipulated or enhanced using CSS and JavaScript.  These reasons alone make learning SVG that much more rewarding for you and your visitors.

The options for animating your website are many and limited only by your imagination.  As I’ve said many times before, “There’s always a better way and more one solution.”  Working with SVG’s and the number of API’s available to animate them proves this statement to be true yet again.

Until Next Time – STAY lean!

Versalytics

Related Articles and Resources:

 

 

Lean Code and API’s

API Resources

GeocodeResponse-OVER_QUERY_LIMIT

Using an Application Program Interface or API is not a new concept, is relatively simple, and easy to do.  However, developing applications using resources outside of your local controlled environment can present new challenges and opportunities.

In this instance, I was testing one of Google’s API resources and received an “OVER_QUERY_LIMIT” error message.  Although it’s not too often that this happens, apparently it can on occasion.

You have exceeded your daily request for this API.  We recommend registering for a key at the Google Developers Console:  https://console.developers.google.com/apis/credentials?project=

You typically need an API key before you can use an API such as the Google Maps Geocoding API.  For those of you thinking or wondering about security, you can place restrictions on the use of your API key.

Registering an API Key

APIs Credentials

If you already have a Google account, go to the “https://console.developers.google.com/apis/credentials?project=” link as shown in the error message above, sign in to your account and accept the terms and conditions.  You can then begin the process of creating your project and credentials.  Google will then generate an API key for your project to use in your application.

As of this writing, there are at least 184 API’s available where some of the more popular API’s include Google Drive API, Gmail API, Google Maps Android API, Google Cloud Translation API, and Google Maps Geocoding API.

Once you’ve enabled the API’s required for your project and set up your API Key, you can begin using and monitoring the services requested from your GoogleAPIs dashboard. The “Web Services > Geocoding API” page provides a simple example of getting and using the API Key in your application.

API’s can greatly simplify your application development and enables you to deploy features that would otherwise consume a lot of time and effort to implement.  The guides and documents in the API Library describe the scope of the API and provide sufficient information to begin using them to your advantage.

API Access Limits

Be aware that some API’s may have limits to the number of free “access requests” received and/or processed in a given day or over a given time period.  Users may encounter problems if requests from your site exceed this limit.  Increased access requests may be available if you are using a “fee-based” service or premium account.

For example, as of this writing, the Google Maps Geocoding API imposes the following Standard Usage Limits:  2,500 free requests per day and 50 requests per second.  A “pay-as-you-go” billing option or Premium Plan is also available with a maximum of 100,000 daily requests.

API Services

API services or resources may offer significant advantages for your application and are certainly worth an investment of your time to see what is available.  If your programming language, such as JavaScript or Python, provides a means of working with “http” requests, API’s are definitely worthy of your consideration.

API’s enable working with external resources both as a data source and for manipulating data.  The ability to programmatically work with data reduces the possibility of data entry errors and other human fallibilities.

Remember to use API key restrictions to control and prevent unauthorized access requests!  Making an attempt to use an API key from a site other than the one authorized in the restriction settings will return a “REQUEST_DENIED” status as pictured below:

API-Restricted-Access

Until Next Time – STAY lean!

Versalytics

Brackets Update

Brackets-Corner-ScreenShotVersion 1.12 of Brackets was released on January 29, 2018 and is now available for download.  This release introduces some exciting new features including JavaScript Refactoring, intelligent rename in file/scope feature, try catch block wrapping, one click conversion of anonymous expression / function block to arrow expression, and getter/setter creation.  More information can also be found in the release notes.

As a lightweight editor, Brackets serves as the “Goldilocks” solution for my smaller JavaScript and web page development projects.  Web page development is greatly enhanced with the live preview feature.

Although JetBrains Web Storm is my IDE of choice for larger applications, Brackets is an ideal editor for those looking to start off with something simple and easy to use.  Numerous extensions are readily available, easy to install, and will greatly enhance your experience while using Brackets.

To take advantage of the latest and greatest features, it’s important to keep your applications up to date.  These updates may fix bugs that you’ve discovered (or have yet to discover) and may also introduce some features that can save you a lot of time and effort in your development process.

Until Next time – STAY lean!

Versalytics

 

 

 

 

Lean Code and JavaScript IDE

BracketsJavascript is one of the more popular programming languages in use today and its use and capabilities are further enhanced by a variety of available frameworks, such as Node.js and AngularJS, and other libraries.

Although you can use almost any text editor to write Javascript, a language based Interactive Development Environment (IDE) or Code Editor can greatly improve your ability to write code more efficiently and effectively.  This is especially true where syntax and case sensitive languages – like Javascript – are concerned.

I use WebStorm by JetBrains for the majority of my web-based applications, however, a number of less capable cross-platform IDE’s and editors are also available for writing JavaScript, HTML, and CSS.  One such editor is Brackets created by Adobe Systems.

Review:  The 10 best JavaScript editors” by Martin Heller, Contributing Editor, InfoWorld (May 17, 2017) presents a number of JavaScript editors for consideration of which Brackets is among them.  I prefer to use cross-platform tools wherever possible and I use a variety of IDE’s and editors that are best suited for the project at hand.

Brackets is a light-weight open-source cross-platform editor equipped with sufficient functionality to efficiently write HTML, CSS, and JavaScript code.  A number of extensions are also available to further enhance your experience using Brackets.

Extensions are often written by third-party programmers or developers and should be installed with caution.  I highly recommend researching and reading reviews by others to ensure they are worthwhile and trustworthy.

If you are just learning JavaScript or writing a quick a script, consider the simplicity of using an editor like Brackets where you can focus on writing your code rather than get bogged down with learning all the details and nuances of a full-featured IDE.

If you’re wondering what kind of programs can be written using JavaScript, HTML, and CSS, you’ll have Brackets to serve as your source of inspiration as one of the many possibilities.  For more information, visit the Brackets web page.  As of this writing, Brackets 1.11 is available for download.

The excitement of learning a new language is often met with varying degrees of chaos and confusion when selecting the right tools to get started.  I recommend starting with something simple and less distracting that will allow you to focus on the task at hand.  Brackets is one such editor.

In time, you will learn which tools will best serve your needs and you can adjust your working and coding environment accordingly.  Feature filled IDE’s and Editors will do little to serve your needs if you have yet to learn or don’t know how to program.

Until Next Time – STAY lean!

Versalytics

Related Articles, Resources, and References

High-Performing Python

I was pleasantly surprised to see some very useful downloads when I opened the November 2017 issue of the Intel Developer Products Newsletter:

  • Intel Distribution for Python – Accelerate Python Performance – Powered by ANACONDA, and
  • Intel Performance Libraries – Download these free libraries today to create better, more reliable, and faster software applications.
    • Intel Data Analytics Acceleration Library (Intel DAAL)
    • Intel Math Kernel Library (Intel MKL)
    • Intel Integrated Performance Primitives (Intel IPP)
    • Intel Threading Building Block (Intel TBB)
    • Intel MPI Library (Windows Package)

Rather than write about these free offerings from Intel, click on the links above to see and choose the tools that will serve you best.  The files are relatively large and you may want to reserve your time for when and how you download them to your machine.  Versions are available for Windows, MacOS, and Linux.

Anaconda is my Python installation of choice if not simply for the ease of maintaining the many Python packages that are available.  That the Intel distribution for Python is powered by Anaconda is not surprising.

Although I’ve expressed my fair share of caution when using code libraries written by others, I have no concerns with Intel as the documentation is more than thorough.  If you’re truly interested in a comprehensive Python installation, consider this package offering from Intel.

Until Next Time, stay LEAN!

Versalytics