Developers Beginner Guide to Public Static Foo

We recently asked out students to create a simple method in Java return if a given number is a prime number or not.

The task included a list of requirements the students had to fulfill. However, after correcting I realized that for many beginners some concepts are not fully clear – even if those concepts have been taught before. These are things an experienced developer sometimes often forgets because one has used these concepts over the years and became second nature of the developers.

Let’s have a look at what we asked for:

  1. Implement a method which returns if a given number is prime or not.
  2. The method has to be accessible from callers outside of the current class.
  3. The method has to be called without the corresponding class to be instantiated.
  4. Name the given number ‘n’.

These points mainly addressed the understanding of basic language concepts of the programming language taught so far.

There are different approaches, how to test number for being prime. In the given example we use the simple 6k +/- 1 optimization method.

public static boolean isPrime(int n) {
if (n <= 3)
return (n > 1);

if (n % 2 == 0 || n % 3 == 0)
return false;

int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i+2) == 0)
return false;
i = i + 6;
}
return true;
}

While we have been very indulgent with the algorithms “invented” we have been very strict about the language concepts and syntax used in the solutions. number four of the previous task list should be, however, clear. The thing is called ‘n’.

I reflected this quite a while, trying to come up with explanations to the various issues I have seen in the solutions.

Methods as Return Values

Return values. We asked for a method that returns if a given number is prime or not. First of all, you need to define the type of the return value. In the given example it was boolean. Simply because you want to return a yes or no or – as we say in programming jargontrue or false.

public static boolean isPrime(int n) {
...
return false;
...
return true;
}

Also you make sure that all execution paths in your method provide a return statement. The value returned is either true or false.

For someone new to programming, the concept of methods, functions and so on is not always intuitive. In this case I usually try to come up with basic math. I show a mathematical function like f(x) = x + 1. As long as you know something about math it should be clear what this function does. You provide a value for x and you will receive a value increased by one. Then I write this down as code.

int f(int x) {
return x + 1;
}

You have written your very first method. It eventually does exactly what the mathematical function did before. Well done. Usually I celebrate this with my students.

It doesn’t matter if we are talking about object oriented or functional programming languages. The idea behind methods and functions is always the same. The syntax differs from language to language but the concept stays the same.

In addition I extend this example to the mathematical formula f(g(x)) = x + 1 with g(x) = x *2. Again I show how both methods would look in code.

int g(int x) {
return x * 2;
}

int f(int x) {
return x +1;
}

Now I write down a few lines to show how this would work.

int x = 5; 
int y = f(g(x));

While most compilers would refuse to compile this without some additional syntactical sugar, to the students the concepts should be quite clear by now.

Accessibility – Private Property vs. Public Services

If you are not used to programming yet, accessibility can give you a major headache. Who can access what? Over time you will understand concepts like information hiding, encapsulation or abstraction. All of these concepts will make use of accessibility. Although there are slightly different meanings and keywords in a variety of programming language, the idea is always the same. While there is a tutorial available for Java, this is one thing very hard to understand for those learning programming.

I usually try to find some real-world examples: public phone, a phone within the flat with is protected (to be used by family members only) by walls and doors and my private iPhone.

But also public services to be used by everybody, protected classes to be used only for those members in the course and private property not to be accessed by anybody else than me.

I realized that these concepts are slightly more difficult to non-native English speakers until I talked about the examples above which might include translation to some point.

Static – Just use it

I even don’t know where to start how to explain this to somebody not familiar with object-orientation and higher programming languages. I came from Assembler, C, Pascal, BASIC, LISP to Perl, C++ and finally to Java and .NET (VB, C# some F#), some Ruby, Python and various other languages I just don’t have in mind.

If found

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.

and

In C# terms, “static” means “relating to the type itself, rather than an instance of the type”. You access a static member using the type name instead of a reference or a value, e.g. Guid.NewGuid().

and hell ya, this explains everything to a novice. So I explain it a very simple but inaccurate way: When using the static keyword on a method, you can just run the code. If using it on a variable, you can just use the variable. To some point you can think of static as “just use it”.

If you are new to programming and come across this article, I hope this helps .

tl;dr

There’s no short version. Just trying explaining public static foo to someone without a clue about programming.

Pro Git Kindle Edition

Just in case you own a Amazon Kindle an you are working a lot with Git. And just in case you struggle with Git the same kind as I do e.g. when it comes to a rebase, have a look at Pro Git. It’s available as Kindle edition for free at the moment from amazon.de.

progit

Although the book is from 2014, it still covers many of these daily things that are beyond a simple push or commit when using Git. Should be in every developer’s book shelf.

Customize Windows 10 File Type Icons

I just realized, that recent Windows version make it very hard to customize some aspects of your desktop.

As I am using Sumatra PDF as my default PDF viewer, I do not have the typical Adobe icon on my desktop. While having these icons on my Mac as well as on my work PC, this is rather confusing.

I came along File Types Manager which actually takes away the registry fiddling.

File Tymes Manager Main Window

It works with Windows XP, 7/8 and Windows 10. if you have any .ico files available it is pretty easy to change an icon for a particular file type just by assigning it.

Changed PDF file type icon

Also there are tons of other features, I haven’t used yet.

tl;dr

If you want to change file type icons on a Windows 10 system, you ca use File Type Manager to do so easily.

Snip & Sketch

Right after I just writing about Windows Snipping Tool in my previous post, my Windows 10 notebook just surprised me by telling there will be something called Snip & Sketch in the future. Snipping Tool is moving dialog

It seems it has the same capabilities as Windows Snipping Toll while adding the possibility to annotate your screenshots directly using various pencil and sharing capabilities via Bluetooth and WiFi.

Snip & Sketch Toolbar

In case you don’t have Snip & Sketch on your Windows machine yet, you can download it from the Windows Store for free.

tl;dr

Windows Snipping Tool will be replaced by Snip & Sketch providing additional annotation capabilities.

Simplifying Shortcuts using Windows Snipping Tool

At my current company laptop, I am very limited when it comes to installing additional tools to improve my productivity. From a company view understandable, it is very frustrating to me. One of the tasks I do several times a day, is creating a screenshot. Unfortunately, I am not able to install tool such as my beloved SnagIt.

I use Windows Snipping Tool instead a lot. However, pressing Ctrl + Esc, typing Snip and then pressing New gets annoying over time. So I decided to create an Instant Snip instead when.

So head to the Snipping Tool and right click it to navigate to its location on the disc.

Snipping Tool Context Menu in the Windows Start Menu

Copy the shortcut in this folder and rename it. In my case, I copied the shortcut to a Prg folder in my Home directory where I save programs, batch files and shortcuts.

Copying the Windows Snipping Tool Shortcut from its original location to another folder

Now right click the shortcut and do the following small changes:

  1. Add the parameter /clip to the command line in the shortcut
  2. Optional: Add a keyboard shortcut in case you are not happy with the Windows default (in my case Windows+Shift+I)

Properties of the Snipping Tool Properties UI

At the very end, I have put this shortcut to my taskbar to create instant clips with a single mouse click.

That way you can use the shortcut to create a clip directly. Using the /clip patemter you can use Snipping Tool in semi-automated processes but also the keyboard shortcut you have just defined.

In case you are only looking for the default keyboard shortcut you can use Windows+Shift+S to start the clip mode of Snipping Tool.

One point to bear in mind is the fact, when using the /clip mode, the clip is directly saved to the Windows clipboard and the Snipping Tool UI cannot be accessed right after the screenshot is made.

tl;dr

Windows Snipping Tool has a parameter /clip to create a instant clip from the command line without starting the UI dialog but also can be started using Windows+Shift+S to create such a clip.