Category Archives: Books

Book Review: Introduction to Javascript Object Notation by Lindsay Bassett

Whilst attending the UKOUG Tech16 conference, several of the talks I attended mentioned the use of JSON (or to give it’s full name JavaScript Object Notation) These talks made me realise how little I actually knew about this Data Interchange Format.

There are many resources for learning JSON, from websites of varying quality to paid for video courses on Pluralsight or for free on YouTube. However my favourite method of learning something new is by reading book(s) and then conducting experiments using what I have learnt. So I chose the following: Introduction to Javascript Object Notation by Lindsay Bassett.  I chose this title as it had a good review and at just over 100 pages was not going to be a door stop that I will never finish.

The book begins with an overview of JSON, it’s syntax, the available datatypes and validating your JSON documents using JSON Schema before switching gears and moving on to demonstrate how JSON can be used in client and server side frameworks and NoSQL databases.

The book was a pleasure to read, new concepts are concisely introduced and no assumptions of your knowledge are made and having now read it I am far more confident in my understanding of this latest Interchange Format.

If you are looking to get up and running with JSON it is easy for me to recommend this book.

 

 

 

Technical Books I have read in 2016

I have always enjoyed reading books about Programming. From books that lead you to take your first tentative steps with a new language to ones that take you on a deep dive into the world of particular feature. I especially enjoy ones that discuss language agnostic programming concepts such as debugging, estimating etc. Books like Code Complete, The Pragmatic Programmers, The Mythical Man Month and Don’t Make Me Think.

To me technical books are such a bargain. For £20 – £30 you can gain knowledge and insight that can make you so much better at your job, such as taking different approaches to solving the daily problems that we as programmers face. Without a doubt there is a lot of published rubbish out there but fortunately in these days of reviews and questions on the numerous Stack Exchange sites it is a lot easier to avoid the charlatans and their ammo pouches stuffed with silver bullets. Although as you will see from my own list, one or two may still slip through the net!

Here are the programming related books I have read this year, listed in the order that they were read.

cplayersThe C# Player’s Guide (2nd Edition)

This is my favourite book that I have read whilst learning C#. Immediately accessible. The large format of the book along with the lucid and easy to grasp descriptions of Object Orientated topics make this my recommended book to anyone that is interested in learning C#.

Django By Example djangobe

Unfortunately this book is still on the “bought but not read” pile. It is no reflection on the book I have been focusing my attention on learning C# this year.

C# 6.0 and the .NET 4.6cnet46 Framework

At 1600+ pages this was certainly the biggest technical book I bought this year. For me it is too unwieldy to use on a day to day basis so, for the first time I have abandoned the printed version of a book and have spent the last 8 months using the e-book. Usually the ebook is open on one monitor whilst Visual Studio is open in the other. Not sure if it’s such a good book for beginners but as a reference I can see myself returning to it to look things up.

The Psychology of Computer Programming: Silver Anniversary Editionpcp

I have been wanting to read this book for several years and finally got round to it. It is by a very long way my favourite read this year and it is in the top 5 all time technical books I have ever read. Although 45 years old, the ideas discussed then are still very relevant today; How we don’t read existing code to see how others have solved problems, the critical importance of having code reviews, egoless programming, estimating and setting expectations around delivery times. I could go on and on. If you haven’t read it, order it today you will not regret it. It will make you a better programmer or manager!

learnciadLearn C# in One Day and learn it well

The worse book I read this year. I have already written what I think of it here.  Not much more to add so moving on to the final book…..

Working Effectively With Legacy Code wewlc

The final book for this year is another classic and I have high expectations for it. Currently I am a third of a way through but I will have finished it by the end of the year. At this point I think it should be called “Working Effectively with Legacy Object Oriented Code” because a lot of the ideas in the book code are centred around legacy Object Oriented code. I will update this once I get to the end of the book.

Summary

This year marks a slight change from previous year lists in that I haven’t read any Oracle database or Application Express books. There are two reasons for this. First I don’t think there have been any unmissable Oracle books published this year (I am interested in Real World SQL and PL/SQL that was published in September 2016 however I awaiting reviews or to actually have a look through it) –  and secondly most of my spare time has been spent learning C#.

I have taken something from each of these five books this year, yes even Learn C# in a day. I know that as a result of reading these books, I will start 2017 a better programmer.

Learn C# in One Day and Learn It Well – Review

I have been learning C# and the .NET framework for a while now and have been working my way through several books; The C# Programming Yellow Book, The C# Player’s Guide (2nd Edition) and C# 6.0 and the .NET 4.6 Framework All of these books have helped me to varying degrees to get comfortable in Object Oriented programming, the C# language and the .NET framework.

When learning a new programming language, I always look to improve my knowledge of the fundamentals, so seeing an introduction to C# book that was getting good reviews piqued my interest. That book was Learn C# in One Day and Learn It Well although I am very suspicious of Learn X in Y days\hours\minutes titles (see Peter Novig masterly description) I ordered a copy.

learncinaday

 

 

 

 

 

 

 

At 153 pages the book is slim and can be divided into two parts. Chapters 1 through 11 cover the various building blocks that make up a programming language such as variables, arrays, condition statements as well as briefly touching on Object Orientated concepts. The second half of the book, starting on page 128 brings together what you have learnt in a project by building a Payroll programme.

I think the book is self published, obviously not an issue in itself however I felt that it could have done with a review\editor to catch the typos and misaligned paragraphs but these are minor irritants. The real point of this post is, can you use this book to learn C# in a Day?

In my opinion no. The main problem with this book is how brief the topics are covered. Take for example Interfaces which are discussed on pages 107 – 109. The text compares Interfaces with Abstract classes, however no where in these two pages does it tell you what an interface actually is and why you would want to create one.

In summary I am not sure who the target audience for this book is. Perhaps someone that just needs to get some course work or module “working” For everyone else it is far to brief and does not go into enough detail especially explaining why you would want to use a feature of the language. If you are interested in learning C# my advice would be to put the £8 towards a better resource.

 

 

The best lesson I learnt from Steve McConnell

CodeCompleteI was made aware of the book Code Complete by Steve McConnell almost by accident. I was attending an Oracle presentation back in 2001 and the presenter, Steven Feuerstein mentioned the book. I can’t remember what context it was mentioned in but I remembered I had to get a copy.

Reading the first edition of Code Complete became a series of wow moments. As a self taught programmer each chapter heralded a new revelation. I say proudly that this one book has had the biggest positive impact in my professional life as a programmer.

It is therefore very difficult to pick the best lesson I learnt from Steve McConnell. It finally came down to a choice of two; Valid Reasons to Create a Routine and the chapter on Debugging.

In the end I decided that it was Valid Reasons to Create a Routine. I won’t reveal the most important reason here but the answer will surprise you. It’s not to avoid duplicate code BTW.

If you want to know the answer I recommend you buy the book, I already know you will not regret it.

Instrumenting your PL/SQL code

Introduction

As an exceptional Oracle Developer you already know the value of instrumenting your code. You have read enough of Tom Kyte, Cary Millsap & Steve McConnell to understand that with the correct instrumentation diagnosing problems especially time critical problems (are there any others?) becomes much easier.

So the question is not why you should instrument your code but how. In this article I will explain the installation and use of the PL/SQL Logger which was originally developed by Tyler Muth and is now available on Github.

Installation

Once you have downloaded Logger from Githib and unzip the contents to a directory that can be seen by your Oracle client (I use SQL Plus). navigate to the directory “releases” and expand the folder for the Logger release you wish to install. At the time of writing version 2.1.2 was the latest so that will be the version used in this article.

Whilst you can install logger into an existing schema, I am going to use the supplied @create_user.sql to install Logger into it’s own schema. So start SQL Plus and connect to the database as system or a user with the DBA role and run the following script

@create_user.sql

When prompted to do so, enter the username, tablespace, temporary tablespace and password for the new schema. In this example I have accepted the suggested defaults of logger_user, users and temp respectively.

Now connect to the database as the newly created logger_user and run the installation script:

@logger_install.sql

If you run into problems with the installation it is worth starting with the supplied documentation.

In this article I will be accessing the Logger objects from the standard Oracle HR user so the following grant is required:

GRANT EXECUTE ON logger TO hr
/

Once installation is complete, you can view the status of the logger by running the following command

exec logger.status;

which displays the following:

SQL> exec logger.status;
Project Home Page :
https://github.com/tmuth/Logger---A-PL-SQL-Logging-Utility/
Logger Version : 2.1.2
Debug Level : DEBUG
Capture Call Stack : TRUE
Protect Admin Procedures : TRUE
APEX Tracing : Enabled
SCN Capture : Disabled
Min. Purge Level : DEBUG
Purge Older Than : 7 days
Pref by client_id expire : 12 hours
For all client info see : logger_prefs_by_client_id

PL/SQL procedure successfully completed.

How to use

Below is a procedure that updates the salary of a given employee. Now lets pretend it is part of a overnight batch job and is just one of thousands of procedures that need to run. To the outside world this procedure is a blackbox. Without querying the employee after the procedure has run (and committed) you have no idea what has happened when it was called.

CREATE OR REPLACE PROCEDURE raise_salary
(
   p_emp_id     IN employees.employee_id%TYPE,
   p_new_salary IN employees.salary%TYPE
)
IS
BEGIN

   UPDATE employees e
      SET e.salary = p_new_salary
    WHERE e.employee_id = p_emp_id;

END raise_salary;
/

Lets change that by adding some instrumentation.

CREATE OR REPLACE PROCEDURE raise_salary
(
   p_emp_id     IN employees.employee_id%TYPE,
   p_new_salary IN employees.salary%TYPE
)
IS
BEGIN

 logger_user.logger.log('Start of raise_salary');

 UPDATE employees e
   SET e.salary = p_new_salary
 WHERE e.employee_id = p_emp_id;

 logger_user.logger.log('Number of rows updated: {' || TO_CHAR(SQL%ROWCOUNT) || '}'); 

 logger_user.logger.log('End of raise_salary'); 

END raise_salary;
/

The revised version of the procedure captures some basic information as the start of the procedure, how many rows were updated and finally that the procedure completed successfully.

Even in this simple case it is easy for the real code to be submerged by the instrumentation code. There is no one size fits all solution. Careful consideration is required to decide how much instrumentation is required.

When the procedure is run, one of the Logger views can be queried and the output reviewed:

SELECT text
  FROM logger_user.logger_logs_5_min
/

Here is the output from the revised procedure after it has been run:

TEXT
----------------------------
Start of raise_salary
Number of rows updated: {1}
End of raise_salary

 Level Up

The output from Logger is controlled by the level constant. The level can be set by calling the logger.set_level procedure. In the previous example I have used the log procedure to instrument my routine. This means that the output will insert an entry into the logger_logs table when the logger_level is set to debug.

There are a number of additional log_x routines that you can use with the various logging levels. For instance, log_warning will insert an entry into the logger_logs table when the logger level has been set to “warning”, likewise if you are only interested in capturing errors, you can set the Logger level to error and use the logger_error procedure.

One of the nicest features of the level facility is the ability to turn logging on for specified clients rather than system wide. In the following example, I have turned on debug logging for user 1 whilst user 2 continues to use the system but without instrumentation messages being inserted.

Logger_user session

The following commands are run:

exec logger_user.logger.set_level(p_level => 'OFF')
/

This system wide command turns Logger off. This means currently no instrumentation is being recorded.

exec logger_user.logger.set_level(p_level => 'DEBUG', p_client_id => 'user_one')
/

This command turns on debug logging for the client identifier user_one. (see user one session below for how this is set). This means any instrumentation that uses the logger.log procedure will be inserted into the log table.

The following query will show you the current clients that logging is enable for:

SELECT *
  FROM logger_prefs_by_client_id
/

User_one session

The following command sets the client identifier (this is used by the call to enable logging for this client by the Logger_user session above)

exec dbms_session.set_identifier('user_one');

I then execute the raise_salary procedure and commit.

User_two session

Almost a carbon copy of the user_one session with the exception of the identifier.

exec dbms_session.set_identifier('user_two');

I then execute the raise_salary procedure and commit.

Now when I query the logger table I see the following output which shows that only user_one’s actions have been captured.

SQL> SELECT client_identifier, text
2      FROM logger_user.logger_logs_5_min
3 /

CLIENT_IDENTIFIER TEXT
------------------------------------
user_one Start of raise_salary

user_one Number of rows updated: {1}

user_one End of raise_salary

But wait there is more…

All of the main Logger procedures allow you to give context to the information you are recording. The context would normally be where the message has originated from, i.e. Application, Package etc. The facility is provided by the p_scope parameter. Here is the raise_salary procedure used earlier refactored to include the p_scope parameter.

CREATE OR REPLACE PROCEDURE raise_salary_scope
(
   p_emp_id     IN employees.employee_id%TYPE,
   p_new_salary IN employees.salary%TYPE
)
IS

   l_scope logger_user.logger_logs.scope%type := 'raise_salary_scope';

BEGIN

   logger_user.logger.log('Start', l_scope);

   UPDATE employees e
      SET e.salary = p_new_salary
    WHERE e.employee_id = p_emp_id;

   logger_user.logger.log('Number of rows updated: {' || TO_CHAR(SQL%ROWCOUNT) || '}', l_scope);

   logger_user.logger.log('End', l_scope);

END raise_salary_scope;
/

Each of the calls to the log procedure now has the name of procedure contained within the l_scope parameter passed to it.

The following query shows results of using the scope parameter

SQL> SELECT scope, text
2      FROM logger_user.logger_logs_5_min
3    /

SCOPE                TEXT
------------------------------------------------
raise_salary_scope   Start

raise_salary_scope   Number of rows updated: {1}

raise_salary_scope   End

The logging of parameter values is handled within Logger by using the p_params object. This is easier to show with an example rather than even more text!

Here is the final revision of the raise_salary procedure.

CREATE OR REPLACE PROCEDURE raise_salary_params
(
   p_emp_id     IN employees.employee_id%TYPE,
   p_new_salary IN employees.salary%TYPE
)
IS

   l_scope    logger_user.logger_logs.SCOPE%TYPE := 'raise_salary_scope';
   l_params   logger_user.logger.tab_param;

BEGIN

   logger_user.logger.append_param(l_params, 'p_emp_id', p_emp_id);
   logger_user.logger.append_param(l_params, 'p_new_salary', p_new_salary);

   logger_user.logger.log('Start', l_scope, null, l_params);

   UPDATE employees e
      SET e.salary = p_new_salary
    WHERE e.employee_id = p_emp_id;

   logger_user.logger.log('Number of rows updated: {' || TO_CHAR(SQL%ROWCOUNT) || '}', l_scope);

   logger_user.logger.log('End', l_scope);

END raise_salary_params;
/

The parameters are added to a pre-defined Logger associative array and that array is included in the first call to the log procedure.

The parameters are stored in the extra column and the following query shows how to display them.

SQL> SELECT text, extra
2      FROM logger_user.logger_logs_5_min
3    /

TEXT                         EXTRA
---------------------------------------------------------------------------------
Start                        *** Parameters *** p_emp_id: 100 p_new_salary: 4500

Number of rows updated: {1}

End

Summary

There is no getting away from the fact that the instrumentation is an overhead to your application. Trying to find the balance between too much and too little information that is to be captured, the extra code you need to develop and then the extra resources required to execute the larger code base all need to be carefully considered.

My opinion is that this is a price I am willing to pay. When bad things happen and you are under pressure to track down the cause you will find the problem far quicker with code that is telling you what exactly is happening. Compare this with the alternative. Peering into an enormous black box armed only with guesses.

Acknowledgements

Tyler Muth

Martin Giffy D’Souza

What are my favourite Oracle books? and why?

This post was inspired by a comment on Martin Windlake’s blog.

I am an Oracle Developer who actually likes reading technical books, not only about Oracle technologies but also the wider Software Development world. (Does that put me in a minority?)

As a self taught programmer I have read many Oracle books since I started working with the Database. Whilst most of the books I have read have been at best, perfunctory,  the books that have made it on to this list, which are in no particular order, all have had a very positive impact on I how work with the Oracle Database and it’s related technologies.

Effective Oracle By Design by Tom Kyte

This book is the Code Complete for Oracle developers.

It was the book that first made me aware, when working with PL/SQL less is definitely more and to start thinking in sets. It highlighted the importance of instrumentation within your code, to be wary of universal best practises and also included the only road map I have yet to see for the Oracle Documentation.

PL/SQL From SQL a chapter by Adrian Billington from the book Expert PL/SQL Practises

I found this book to be very hit and miss but I believe Adrian’s single chapter “PL/SQL from SQL” is by itself worth the price of the book.

For many years every relevant Oracle tome I have read had the dire warning “beware of context switching” Oracle Developers know it is has to affect performance when you switch from SQL to PL/SQL within the same statement but exactly how bad it actually is was rarely, if ever disclosed. Adrian’s chapter is the first I know which shows the true cost of context switching. The first part of the chapter explains the term “Context Switching” and goes on to show’s the cost with easy to follow “Then and Now”  SQL.  The second part of the chapter then moves on to explaining how you can start reducing the cost of PL/SQL functions when called from SQL along with some non – PL/SQL alternatives.

Troubleshooting Oracle Performance by Christian Antognini

The book on Oracle performance.

It covers the whole spectrum of Oracle Performance tuning. From identifying and the prioritisation of problems from a business perspective to in depth discussion of the DBMS_XPLAN package.

Oracle PL/SQL Programming by Steven Feuerstein

The seminal book on working with the PL/SQL language. I hesitate to recommend it for learning the language simply because I struggled to learn PL/SQL using the 2nd Edition but once you are up and running there is no better resource for PL/SQL.

Expert Application Express by John Scott, et al

This is a book that should be within reach if you are working with Oracle Application Express. With thirteen chapters written by many of today’s Application Express luminaries it covers topics from the myriad of choices you have for selecting the webserver to how to develop tabular forms effectively and working the Apex 4 features such as Dynamic Actions. My favourite is Doug Gault’s chapter on Debugging.

Where are the Jonathan Lewis books? Whilst I never miss a Jonathan Lewis presentation at the UKOUG events, I have struggled with his books and so at the moment they do not appear on my list. However this list is very much live so one may appear as I periodically review this post.