• Non ci sono risultati.

PRESENTATION IS POWERFUL

N/A
N/A
Protected

Academic year: 2021

Condividi "PRESENTATION IS POWERFUL "

Copied!
30
0
0

Testo completo

(1)

PROGRAMMAZIONE I

A.A. 2017/2018

(2)

HINTS

(3)

PRESENTATION IS POWERFUL

Good presentation does not draw attention or distract; it serves only to reveal the code’s intent. This helps

programmers work with the code effectively.

You can see what the author intended here: return(0) was only to be called when the test failed. But the

presentation has hidden the real behaviour: the code will always exit.

int ok = thisCouldGoWrong();

if (!ok)

fprintf(stderr, "Error: exiting...\n");

return(0);

(4)

LAYOUT

Code layout concerns include indentation, use of whitespace around operators, capitalisation, brace

placement (be it K&R style, Allman, Whitesmith, or the like), and the age-old tabs versus spaces indent debate

while (x == y) {

something();

somethingelse();

}

Allman

while (x == y) { something();

somethingelse();

}

KR

while (x == y) {

something();

somethingelse();

}

Whitesmiths

(5)

KR

The K&R style is commonly used in C (Ritchie designed C).

int main(int argc, char *argv[]) {

/*...*/

while (x == y) { something();

somethingelse();

if (some_error) do_correct();

else

continue_as_usual();

}

finalthing();

/*...*/

}

(6)

NAMES

A name conveys the identity of an object; it describes the thing, indicates its behaviour and intended use.

A misnamed variable can be very confusing.

Do not create unnecessarily long variable name Do not use underscore as first character to avoid

confusion between system variable and user defined variables because many system variables starts with underscore

Variable names are case-Sensitive .

üsum,Sum,SUM, sUm these are different variable names.

Reserve words with one/more Capital letters

allowed eg. Int, Float, chAr are allowed but try to skip them.

(7)

NAMING CONVENTION

See the difference

üa = b * c;

üweekly_pay = hours_worked * pay_rate;

Struct TitleCase

Struct Members lower_case or lowerCase

Enum ETitleCase

Enum Members ALL_CAPS

Trivial variables i,x,n,f etc...

Local variables lower_case or lowerCase

Pointers p_lowerCase or p_lower_case

Global variables g_lowerCase or g_lower_case

(8)

WRITE LESS CODE

More code means there is more to read and more to understand—it makes our programs harder to

comprehend.

The more code there is, the more work is required to make modifications—the program is harder to modify.

Code harbours bugs. The more code you have, the more places there are for bugs to hide.

(9)

EXAMPLE

A simple and common class of pointless code is the unnecessary use of conditional statements and

tautological logic constructs.

if (expression) return 1;

else

return 0;

return expression;

if (something != 0) { // ...

}

if (something) { // ...

}

(10)

COMMENTS

Add comments as more as you can

For instance, before each function declaration to let understand other programmers what they do with the parameters they take as input.

But there are also stupid comments:

++i; // increment i // loop over all items, and add them up int total = 0;

for (int n = 0; n < MAX; n++) { total += items[n];

}

(11)

SCANF

(12)

PROBLEMS WITH SCANF

Example scanf.c #include <stdio.h>

int main() { char a, b;

int c= 0;

printf("Enter a character: ");

scanf("%c", &a);

printf("Enter a character: ");

scanf("%c", &b);

printf("Enter an int: ");

scanf("%d", &c);

return 0;

}

MacBook-Francesco:ProgrammI francescosantini$ ./scanf Enter a character: d

Enter a character: Enter an int: 5

d

Input buffer

\n

(13)

SOLUTION

#include <stdio.h>

int main() { char a, b;

int c= 0;

printf("Enter a character: ");

scanf("%c", &a);

printf("Enter a character: ");

scanf(” %c", &b);

printf("Enter an int: ");

scanf("%d", &c);

return 0;

}

(14)

FGETS

#include<stdio.h>

int main(void) {

char a[10];

if (fgets (a, 10, stdin) == NULL) printf("Error\n");

printf("%s", a);

}

MacBook-Francesco:ProgrammI francescosantini$ ./fgets abcdefghilmnopq

abcdefghi

(15)

WHY C (C++)

(16)

POPULARITY (2016)

https://blog.newrelic.com/2016/08/18/popul ar-programming-languages-2016-go/

(17)

ONE MORE CHART

http://pypl.github.io/PYPL.html

(18)

QUOTATIONS

So what do all these data points add up to? Java

remains incredibly popular, and job seekers can’t go wrong learning it. JavaScript skills may not land you a

job as quickly, but you’ll be in good company with legions of other programmers. There’s still interest in the

various flavors of C as well, and PHP plays a big role in the mid-market.

But perhaps the real takeaway is that we seem to be moving into a polyglot world where organizations

increasingly employ multiple languages. The idea is to take advantage of each language’s special strengths as well as the varied expertise of the programming team.

(19)

WEB

http://spectrum.ieee.org/static/interactive- the-top-programming-languages-2016

(20)

ENTERPRISE

(21)

EMBEDDED

(22)

C PERFORMANCE

(23)

WHY C IS FASTER

Newer languages which have support for garbage collection, dynamic typing and other facilities which

make it easier for the programmer to write programs.

The catch is, there is additional processing overhead

which will degrade the performance of the application. C doesn't have any of that, which means that there is no overhead.

üThat means that the programmer needs to be able to

allocate memory and free them to prevent memory leaks, and must deal with static typing of variables.

(24)

BITCORE

https://bitcore.io

(25)

JOBS

(26)

JOBS

(27)

JOBS

(28)

ARDUINO/GENUINO

(29)

INTRO

https://www.arduino.cc

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a

button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing

something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board.

Arduino was born at the Ivrea Interaction Design Institute as an easy tool for fast prototyping, aimed at students

without a background in electronics and programming.

(30)

ARDUINO

A worldwide community of makers - students,

hobbyists, artists, programmers, and professionals It runs on Mac, Windows, and Linux

Arduino Programming Language

ühttps://www.arduino.cc/en/Reference/HomePage

Examples

ühttps://youtu.be/EiD1LNPm_24 (http://tinyurl.com/huwmec8) ühttps://youtu.be/f4WUa8H0cPo

Riferimenti

Documenti correlati

wt: wild-type control samples; het1, het2 and het3: heterozygous control samples for G12A, G12C, G12R, G12S, G12V, G13D; G12D KRAS mutations; light grey squares represent

Ma è soprattutto con le Prefazioni alla edizione Pasquali delle proprie Opere (Venezia 1761-1778), comunemente note con la formula di Memorie italiane, che Goldoni sviluppa i

Introduction In this article, we present some asymptotic stability results for Kirchhoff systems, governed by the px-Laplacian operator, in the anisotropic setting given by the

Some examples are: the fusion calculus, where names can be unified under some conditions; the open bisimulation of π-calculus, which is defined by closure under all (also

The simulation results of MATLAB / SIMULINK software show that the fuzzy controller has better control performance than the conventional PID controller, which can realize

The structure emerging from one of the possible viewpoints is depicted in the right-hand panel, the forward plot of individual dissimilarity measures d i (m) for searches starting

PROGETTO / DESIGN LISSONI ARCHITETTURA FOTO / PHOTOS COURTESY SIMONE BOSSI TESTO / ARTICLE GIULIA SETTI.. BOLOGNA, UNA STRADA SOSPESA / A SUSPENDED STREET PROGETTO / DESIGN IOSA

Infatti, con l’uso del da Vinci Si l’approccio totalmente robotico può richiedere multipli docking del carrello paziente aumentando la complessità logistica e il tempo