Simple Text Search

Search

I often need to search a directory of code for instances of a specific word. I like tools that I can use from the command line so that I can execute them from an SSH session across the network. Here’s how I do it.

#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

if [ $# -ne 1 ]
then
        echo Call is: `basename $0` string
else
        for file in `find . -type f | cut -c3-`
        do
                count=`cat "$file" | grep -i $1 | wc -l`
                if [ $count -gt 0 ]
                then
                        echo "******"$file"******"
                        cat "$file" | grep -i $1
                fi
        done
fi
IFS=$SAVEIFS

Line Counting

Codes

A common metric used in software development is lines of code. While it’s not always a useful metric, it’s sometimes nice to know just how large a project is. I have a script I use to count lines of code in a folder. It will iterate through a variety of common file extensions and count all lines of code excluding blank lines. It’s written using sh, so it works on Unix, Linux, or MacOS, and should work on Windows if you have the Bash subsystem installed.

#!/bin/bash

# add additional extensions here
extensions=(bas c cc cob cpp cs cshtml fth f90 go h html java js jsp m pas php pl py sc sh sql ts xhtml)

for extension in ${extensions[@]}
do
  lines=`find . -name "*.$extension" -type f -exec cat {} \; | tr -d '[:blank:]' 2> /dev/null | grep -v '^$' | wc -l `
  if [ $lines -ne 0 ]
  then 
    echo $extension: $lines
  fi
done

Languages vs Algorithms

Programming

I was recently discussing a project with a potential client. His project was using a language I haven’t used heavily in quite some time. He was disappointed because he was struggling to find developers to work on his project. I indicated that I would not have trouble getting back up to speed on the language and being able to move his project forward.

This really highlighted in my mind what really matters in development – it’s not languages, it’s algorithms and software design. All programming languages are, at their core, pretty similar. For example, all languages include:

  1. Syntax for creating variables,
  2. Selectively executing a block of code based on one or more conditions
  3. Repeating a portion of code multiple times
  4. Encapsulating code into functions

These basic structures are responsible for all the software you will ever use. Whether it’s a complex operating system like Windows or MacOS, a cell phone application on iOS or Android, or an application on the cloud, they all rely on the basic elements above.

Once you understand the basic syntax of a language, you move into the more important stuff – algorithms. Algorithms are a set of instructions to perform a given task. Regardless of the language, the algorithm will be largely the same. Next up on the hierarchy is software design. Knowing syntax and algorithms is great, but to really develop quality software you need to be able to design the interactions between components. None of these things vary substantially between languages. Languages are merely the syntactic rules you must follow to define your algorithms and implement your design.

I think people need to worry less about what language a developer uses and look for higher-order skills including algorithms and design. That’s what really defines the skill of a programmer!

Coding Style

Writing

One of the most important skills for any team of engineers is to develop a consistent coding style. Without it, development and debugging times increase and productivity decreases. Coding style is much like the rules of writing a story. Sentences start with a capital letter and end in a period. New ideas are started in new paragraphs. Spaces exist between letters of words. If someone were to write a book without following those conventions, it would be substantially more difficult to read. Coding style works the same way.

One of the projects I worked for years was a complicated telephony system. It included low-level code for interfacing with the phone boards as well as high-level code for displaying data to the user. This was a lengthy project that took substantial time to develop. During development, John and I worked to develop a consistent coding style. A mentor on the team, Mark, would review our code, provide feedback, and fanatically insist on standard coding conventions and style.

At first, this seemed silly to me. I’m a developer, let me do what I do! But, as time went on, it became increasingly apparent how valuable it was. After a while, I was unable to tell the code I wrote from what John wrote. While this may seem silly, this meant that when bugs were found, either of us could feel equally comfortable understanding and changing the code.

Today, when I learn a new language, one of the first things I try to do is learn standard coding conventions for that language. Some languages, like Java, have well established and followed conventions. Unfortunately, many other languages do not. When that happens, the team needs to determine what conventions they will follow for their team. If not, expect bugs to take longer and development to be slower.

Development Environments

Workbench

As a developer, it’s a great time to be alive. We have countless options for development languages and frameworks. But, sadly, many of them leave much to be desired in terms of tools and environments.

The Model Environment

As a veteran Java developer, I’ve been spoiled with excellent tools. We have numerous IDEs including Eclipse, NetBeans, and IntelliJ. Everyone has their preference, but all three are excellent. Likewise, we have numerous build and dependency management packages such as Maven and Gradle. We have test frameworks, automation frameworks, debuggers, profilers, and anything else you could want to engineer even the most complex of application. In short, Java provides the best development environment there is. Seriously. Few languages have IDEs as good as what Java has. Even fewer have tools that can complete with Maven or Gradle.

When Will Other Languages Catch Up?

I continue to see how Python is displacing other languages. But when will it catch up to Java in terms of development environment? Are there any IDEs that complete with Eclipse for Python? (NOTE: Eclipse can actually be used for Python, but it always seemed to me more of a hack than a real Python IDE) How about debuggers? Go ahead and try debugging Python or JavaScript code and you’ll quickly see how far behind Java they are. Not to mention dealing with libraries and environments!

A Call to Action

As we continue to shift to newer languages, I’m concerned that the next generation of developers will actually have a more difficult time than we do today. Why, as a development community, are we only focused on creating new languages but not on creating solid environments? How are Go, Python, Rust, and the myriad of other languages ever supposed to achieve the same widespread adoption of Java if we never had the rest of the tools we need to get the job done? I want to enjoy the benefits of some of the newer languages to come along. But, until they can complete with Java, I’m unable to stake the success of my customers on them.

Multilingual Programmers

One of the biggest difficulties in the computer programming world today is the sheer number of languages out there to use for application development. Which language should you learn? The reality is, that’s the wrong question. I recently spoke with a developer who indicated that he only knew C# – he was not comfortable with Python or any other language. For the professional developer, this it not an acceptable answer. In today’s world of programming, you can no longer get by knowing only a single language. For example, while the bulk of the code in a Java web application will be in Java, the front-end will require JavaScript and HTML. Build scripts may be written in Groovy or XML, and Shell scripts may be required for build automation. This is not unique to Java applications – just about any application will require some knowledge of other languages.

How do you pick which languages to learn? At a minimum, every professional engineer should know at least one compiled language such as Java, C, C++, or C# in addition to JavaScript, Python, and SQL. Additionally, he or she should be comfortable with another language such as Ruby, PHP, Go or Rust.

If you’re an application developer, do you only know one language? If so, I would strongly urge you to learn additional languages. Mastery of every language isn’t necessary, but any decent developer should feel capable of writing code in several languages.

Language Popularity

A subject that repeatedly comes up on websites, blogs, and conversations is which language is the most popular and which ones are the best to learn for future job opportunities. I keep an eye on this list myself as it’s important to understand trends within the software industry. However, people seem to ignore the most important aspect of language popularity – the context.

What do I mean by context? I mean we have to look deeper to understand why a language is as popular as it is. We also need to understand the market for a language and how that impacts popularity. For example, JavaScript is always near the top on any language popularity chart. Is it any coincidence that JavaScript is also the only front-end scripting language in use today? Whether or not you like JavaScript is utterly inconsequential. If you want a website with any manner of front-end scripting, you will use JavaScript. Likewise, if you want to interact with a database you use SQL and if you want a web page, it will be HTML. As such, these technologies will always rank more highly than those with a smaller market.

So what about Python? Everywhere you look, you see that Python is at the top of the list. Should we all switch to Python? Not really. What all those rankings fail to point out is the user base for Python. Certainly there are all kinds of production projects out there running Python. But if you look around, you see that Python’s popularity is largely caused by people outside the traditional developer community. AI researchers, academics, data scientists, system administrators, makers, etc. For non-programmers, Python is easy and incredibly powerful. But that hardly makes Python superior to other languages nor does it suggest that professional programmers should start porting their eCommerce sites.

Remember, while rankings of popularity may help you see trends, look to the underlying reasons. Why is it popular? Who is it popular with? Does this language look poised to overtake in other realms? When picking a language for a problem, the most important question is which language solves the problem best – not which language is the most popular.

Angular & MaterializeCSS

During the last year, I’ve developed numerous websites and mobile applications using the Angular framework. I’ve used numerous CSS frameworks as well such as Bootstrap, Ratchet, and Topcoat. But recently, a customer asked me to use MaterializeCSS. MaterializeCSS is a library of CSS and JavaScript code to easily create a material design experience for web applications. It’s quick, easy, and visually appealing. Unfortunately, getting some of the components to work with Angular proved difficult as the online documentation does not show how to setup Materialize for Angular. While most of the components do not require any JavaScript interaction by the developer, some components (such as select option lists or tabs) require initialization. The documentation says that tabs can be initialized by using:

var instance = M.Tabs.init(el, options);

Unfortunately, this won’t work in Angular as you’ll get numerous errors regarding undeclared variables. How can you hook this up properly?

First, declare the ‘M’ variable inside the page component below the imports. This will allow you to use the ‘M’ interface to MaterializeCSS from within your Angular code.

declare var M: any;

Next, get a reference to the object you are trying to interact with. This can be obtained by using a querySelector for the class of object you’re trying to initialize. For instance, tabs will be declared in HTML by assigning a class=”tabs” attribute to object. Once you have the object, you can initialize it by calling the appropriate method. This should all happen on the init method of your page object.

ngOnInit() {
  const elem = document.querySelector('.tabs');
  const options= {};
  M.Tabs.init(elem, options);
}

This is identical for any other object that requires MaterializeCSS initialization and can also be used to call any other functions in the MaterializeCSS framework.

With this information, you should be able to enjoy the benefits of Angular as well as the awesome UI elements of MaterializeCSS to their fullest!

Killer Robots

During the last year, countless tech leaders have talked about the danger that artificial intelligence could pose in the future. Like most people, I laughed at them. After all, do I really think that The Terminator or The Matrix were prophetic? Hardly. But the more I read and the more I pondered it myself, the more concerned I became. Now, I wonder if there’s any way to prevent it from happening at all.

Is it really reasonable to think AI could take over the world? Do we really think code will be so poorly written and that software testers will be so incompetent as to let AI robots kill humanity? Unfortunately, I do. Not intentionally, of course, but bad code that wasn’t properly tested will make it into the wild on robots. Consider all the system updates that have been performed on your computer or your cell phone. Think about all the app updates that happen every single day. Consider all the one star reviews for apps on the mobile stores. AI will be no different.

Consider all the potential causes of AI issues. Developer errors, inadequate testing, corporate release requirements, poorly defined ethics, unforeseen events, etc. Each one of these issues could cause AI to perform in ways it was not intended with potentially catastrophic consequences. Consider government AI being developed by the lowest bidder – wow, that’s scary.

The more I think about that, the more certain I become that AI will eventually cause huge problems to the world. As such, it’s imperative that we – the tech community – consider the limits of AI – not in regards to technology, but rather in regards to safety and security. Do we want AI police officers or soldiers? That sounds dangerous. Could Russian hackers embed “Order 66” into our own robot army? Do we trust robots with firearms to make the appropriate decision in a life-or-death situation?

My intent is not to sound like an alarmist, but rather to begin thinking about the issues now. If not, we may find it’s too late to do so later.

Picking a Server Platform

Many small businesses want websites or mobile applications that require server-side functionality. Often, this functionality includes a database to store user information. What options exist for a user? What are the pros and cons for each one? I will examine three different frameworks – Node and SQLite3, PHP and MySQL, and Tomcat. These represent just a small number of options available to a business – from small scale applications to enterprise solutions.

For small applications, I like Node and SQLite. Node is a simple platform to run server-side programming. Since it requires virtually no infrastructure, Node services can be installed and deployed in minutes.  Likewise, SQLite requires no installation. SQLite databases are a simple file that can be easily backed up or restored by copying the database file. While this framework is great for small applications, enterprise applications would benefit from more robust environments. Node and SQLite can work really well for small internal applications or to implement a small number of services using a small database.

Next up, PHP and MySQL. This combo is widely deployed on a variety of platforms. In fact, that’s one of the reasons I like it. Typically, service providers like GoDaddy have support for PHP and MySQL out of the box, so applications and services can be deployed without much effort.  PHP/MySQL is also more robust than Node/SQLite. On the negative side, PHP has a variety of versions that are substantially different and PHP code can easily become unmanageable if developers aren’t careful.  I like this solution for smaller customers needing a small number of services on an existing PHP server.

Finally, there’s Tomcat. This option is an excellent Java-based server bringing all the advantages of the Java programming language into a robust server environment. Tomcat can integrate with any database, but MySQL is a common solution. Tomcat is an excellent option for Java web applications or services, but suffers from one big problem – it’s the most complicated option to setup.  This option is best when a large number of users or a large database must be supported. This is the option I like to recommend for enterprise customers.

Numerous other databases and server platforms exist. Microsoft’s .NET platform can work great for customers who prefer Microsoft products and Ruby may be a desirable option for some customers too. As with all technology choices, server platforms must be selected based on customer requirements. Small customers appreciate rapid, low cost development while larger customers will want more robust solutions while being less price sensitive.