In my job, I have the opportunity of watching how people think.
I believe that a piece of code is a great demonstration about the cognitive skills of a person.
The question of the picture above says a lot... BeastUK "problem solving" skills are quite limited.
During my profession, I have to instruct developers about what is needed to do and review also their code.
Here is a short collection about the most stupid piece of code I have ever seen (believe me, it is real!).
Of course, this code has been re-adapted and made anonymous.
Anyway, I wanted to add some information about the origin of the genius, therefore you'll find below the flag of the creator's homecountry.
Some of these sniplets come from my former colleagues, freelancers, certified experts, colleagues from my customers, developers with several years of experience.
Top. 10
Ok, let's say that was a too fast copy and paste...
[…]
person.setFirstName(salutation);
person.setLastName(firstName);
person.setSalutation(lastName);
[…]
Top. 9
What about one loop and inside 5 if ?
for i in my_array loop
if (my_array(i) == 1) then
-- do something
end if;
end loop;
for i in my_array loop
if (my_array(i) == 2) then
-- do something
end if;
end loop;
for i in my_array loop
if (my_array(i) == 3) then
-- do something
end if;
end loop;
for i in my_array loop
if (my_array(i) == 4) then
-- do something
end if;
end loop;
for i in my_array loop
if (my_array(i) == 5) then
-- do something
end if;
end loop;
Top. 8
We found in the code the following select:
Difficult to understand? Why do you need a sub query? why not to put it directly in the where condition?
Since it is exactly the same result, try to read it in this form:
Unfortunately I did not have the honor to work with this genious.
I had to adapt the code in order to obfuscate confidential content of course, but the result was the same...
Given the following table:
create table myTable (
a numeric,
b numeric,
c numeric
);
We found in the code the following select:
select a
from mytable
where b = (
select b
from mytable
where c = param
)
Difficult to understand? Why do you need a sub query? why not to put it directly in the where condition?
Since it is exactly the same result, try to read it in this form:
select a
from mytable
where c = param
Top. 7
We found in the code the following select:
The second group by (the external, the surrounding one) is totally useless. It makes the sum of a single record... Let's see a simplified version...
Some confusion with the group by...
Some genious of previous masterpiece. I also in this case I have obfuscated the code.
Given the same table of before:
create table myTable (
a numeric,
b numeric,
c numeric
);
We found in the code the following select:
select a, b, sum(c)
from (
select a, b, sum(c) as c
from mytable
group by a, b
)
group by a, b
The second group by (the external, the surrounding one) is totally useless. It makes the sum of a single record... Let's see a simplified version...
select a, b, sum(c) as c
from mytable
group by a, b
Top. 6
This one left me totally attonished!!!
Found in the code:
select max(id)
into lmaxid
from log_messages;
delete from log_messages
where id <= lmaxid;
Why should
I select from the max and delete everything lower than max… therefore I delete
everything?!?!
there is
nothing behind max…
delete from log_messages;
Top. 5
HOW THE HELL A SELECT COUNT CAN RETURN NO RECORD!!!!
In any case it returns one line with a number, mostly it will be 0!!!!
Found in the code:
DECLARE
v_cnt numeric;
param numeric;
BEGIN
BEGIN
SELECT COUNT(1)
INTO v_cnt
FROM my_table
WHERE field_1 = param;
EXCEPTION
WHEN NO_DATA_FOUND THEN
//do something else
END;
END;
Top. 4
What does it do the NVL function in Oracle?
Let's say:
NVL(a,b)
- If a is not null then it returns the value of a.
- If the a is a NULL value then it returns the value of b.
Tipically used in select statement in order to overwrite possible NULL values coming from the table.
Given the same table of before:
create table myTable (
a numeric,
b numeric,
c numeric
);
Found in the code:
select nvl('a', a), nvl('b', b), nvl('c', c) from dual;
How can it be possible that a constant value (like 'a') can be NULL?
Is it now more clear?
select
'a', 'b', 'c'
from dual;
Top. 3
How to make your life more complicated...
Look at this piece of code and try to understand what is doing.
select
case when count(*) > 0
then count(*)
else 0
end
from table
I am kind of sure you still do not get it... Try to see the version below, it is absolutely equivalent:
select
count(*)
from table
Top. 2 (Log, part 1)
Instructions about task:
Me: Hi Dev, I saw that you are using static code. Actually for this project has been decided to use dynamic code. In particular we need to store the EXACT query that has been executed! Including the parameters used in queries.
Therefore we make the query dynamic into a variable, save the variable in the log table and then execute the variable.
Dev: Ok, all clear, now make sense! So store the query in the log!
Expectation:
procedure myProcedure() begin
[...]
v_sql := 'insert into myTable
select * from mySource
where parameter = ' || var;
insert_in_log(v_sql);
execute v_sql;
[...]
end;
Here is what I got:
procedure myProcedure() begin
[...]
insert into myTable
select * from mySource
where parameter = var;
v_sql := 'insert into myTable
select * from mySource
where parameter = <<parameter>>';
insert_in_log(v_sql);
[...]
end;
Me: Dev.... you are still using static code
Dev: Yes, but the query is in the log!
Me: But not EXACTLY what has been executed... where is the parameter?
Dev: here look, where it is written <<parameter>>
Top. 1 (Log, part 2)
Instructions about task:
Me: Hi Dev, as you know we are using the severity of the log entries. We noticed that your code is writing too many entries at the same level. It does not matter if we use a log level as "debug" or as "production", it writes anyway too many entries. Can you reduce the number of entries in the log? Just assign properlyy the log severity.
Dev: Ok, all clear, reduce the number of entries in the log
Expectation (watch out the severity):
procedure myProcedure() begin
[...]
query 1
insert_in_log(query, high_severity);
query 2
insert_in_log(query, medium_severity);
query 3
insert_in_log(query, high_severity);
query 4
insert_in_log(query, low_severity);
end;
Here is what I got (watch out the severity and the final delete):
procedure myProcedure() begin
[...]
query 1
insert_in_log(query, high_severity);
query 2
insert_in_log(query, high_severity);
query 3
insert_in_log(query, high_severity);
query 4
insert_in_log(query, high_severity);
delete * from log;
[...]
end;
Me: Dev.... ehm... why do you delete the entries from the log?
Dev: Com'on... you told me you wanted less entries in the log!