64
Ať se z kódu nepráší! Juraj Michálek 7 . 5. 2012 - FI MUNI - Brno http://georgik.sinusgear.com http://twitter.com/sinusgear

Ať se z kódu nepráší!

Embed Size (px)

DESCRIPTION

Talk about programming in C for FI MUNI Brno. Topics: Uni

Citation preview

Page 1: Ať se z kódu nepráší!

Ať se z kódu nepráší!Juraj Michálek

7. 5. 2012 - FI MUNI - Brnohttp://georgik.sinusgear.comhttp://twitter.com/sinusgear

Page 2: Ať se z kódu nepráší!

Niečo o mne

Page 3: Ať se z kódu nepráší!

Čo nás čakáTestovanie v C

Kvalita kódu, open source a projekty

C vo svete Applu, Objective-C

Page 4: Ať se z kódu nepráší!

Když se dívám na svůj kód,tak se musím pochválit.Nevypadá věru zle,zkrátka program jak má být.

Před naší, za našíať se z kódu nepráší! Hej!

Page 5: Ať se z kódu nepráší!

Háá! Chybáá!

Page 6: Ať se z kódu nepráší!

Testovanie

žiadnemanuálneautomatické

Page 7: Ať se z kódu nepráší!

Žiadne testovanie

Page 8: Ať se z kódu nepráší!

Ľudová múdrosť

Bez testů do vývoje nelez!

Page 9: Ať se z kódu nepráší!

Manuálne - black boxSpustíme a aplikácia ...

Page 10: Ať se z kódu nepráší!

Manuálne - debugger

Když něco rozeberem, tak leda debuggeremhttp://georgik.sinusgear.com/2010/09/19/zaznam-z-prednasky-kdyz-neco-rozeberem-tak-leda-debuggerem/

Page 11: Ať se z kódu nepráší!

Prečo automatizovať?

Som vývojár alebo debuggrista?

Page 12: Ať se z kódu nepráší!

Automatizované testy

Záchranná sieť pre vývojárovŽivá dokumentáciaLepší dizajnViac času na vývoj

Page 13: Ať se z kódu nepráší!

Typy testov

Unit testing - jednotkové testyIntegračné testyAkceptačné testyRegresné testy

Page 14: Ať se z kódu nepráší!

Test Driven Devel.

Testy prvé

Potom kód

Page 15: Ať se z kódu nepráší!

Unit Testing

izoláciasetUp - príprava prostrediatest - samotný testtearDown - upratanie prostredia

Page 16: Ať se z kódu nepráší!

Príklad testu

int a=41;a++;assert( a == 42, “Universe should be ok”);

Page 17: Ať se z kódu nepráší!

Minunit

Minimalist Unit Testing Framework for C

http://www.jera.com/techinfo/jtns/jtn002.html

Page 18: Ať se z kódu nepráší!

Minunit - code

/* file: minunit.h */ #define mu_assert(message, test) do { if (!(test)) return message; } while (0) #define mu_run_test(test) do { char *message = test(); tests_run++; \ if (message) return message; } while (0) extern int tests_run;

Page 19: Ať se z kódu nepráší!

mu_runtest

mu_runtest( test_currency );

mu_runtest( test_accounting );

Page 20: Ať se z kódu nepráší!

mu_assert

mu_assert(“Should not be null”, state == null);

Page 21: Ať se z kódu nepráší!

Spustenie

gcc *.c -o test./test

ALL TEST PASSEDTests run: 2

Page 22: Ať se z kódu nepráší!

gcc - c99for (int i=0; i<42; i++)

error: ‘for’ loop initial declaration used outside C99 mode

zmena normy parametrom:gcc -std=c99

Page 23: Ať se z kódu nepráší!

Check frameworkhttp://check.sourceforge.net/

Inštalácia./configuremakemake install

Page 24: Ať se z kódu nepráší!

Organizácia testovTest Case

testovaný prípadTest Suite

sada Test CaseTest Runner

Page 25: Ať se z kódu nepráší!

Test Fixtures

void setup();

void teardown();

Page 26: Ať se z kódu nepráší!

Kontrólne fcie

fail_if ( employee->age != 43, “Problem”);

fail_unless( employee->age == 43, "Incorrect age.");

Page 27: Ať se z kódu nepráší!

Kompilácia

gcc -lcheck test.c -o test

Page 28: Ať se z kódu nepráší!

Spustenie testu./testPríklad:

Running suite(s): Employee testing0%: Checks: 1, Failures: 1, Errors: 0main.c:45:F:Core tests:test_age:0: Incorrect age.

Page 29: Ať se z kódu nepráší!

Ďalšie frameworky

C/C++CxxTest: http://cxxtest.com/Boost: http://boost.org

Page 30: Ať se z kódu nepráší!

Continuous Integration

Jenkinshttp://ci.sinusgear.com

Page 31: Ať se z kódu nepráší!

Open source

Projekty

Licencie

Page 32: Ať se z kódu nepráší!

Code_swarmOrganic software visualization of project repositorieshttp://code.google.com/p/codeswarm/

Page 33: Ať se z kódu nepráší!

Gource

http://code.google.com/p/gource/

Page 34: Ať se z kódu nepráší!

Ohloh.net

hodnotenie open source projektov

Page 35: Ať se z kódu nepráší!

Lynda.comDobrý zdroj znalostí

Page 36: Ať se z kódu nepráší!

se-radio.net

Page 37: Ať se z kódu nepráší!

Kvalita kódu

Kód sa viackrát číta, než sa píše.

Review kódu

Page 38: Ať se z kódu nepráší!

Otvorenie kódu

GitHub.comSocial Codingfork

Page 39: Ať se z kódu nepráší!

LicencieFree software

GNU/GPL, LGPL

Open sourceBSD, MIT, Apachebusiness friendly

Page 40: Ať se z kódu nepráší!

Čítajte licencie Copyright (c) 2002 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

Page 41: Ať se z kódu nepráší!

Čítajte licencie Copyright (c) 2002 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

Page 42: Ať se z kódu nepráší!

Objective-C

programovací jazyk vychádzajúci z C

objektové princípy zo Smalltalku

Page 43: Ať se z kódu nepráší!

Objective-C1983 - Brad Cox a Tom Love

zakladajú StepStone1988 - Steve Jobs - NeXT

licencuje Objective-C od StepStonerozširuje GCC, vytvára AppKit

Page 44: Ať se z kódu nepráší!

Objective-C

1996 - Apple ohlásil akvizíciu NeXT429 mil. $

Cocoa framework

Page 45: Ať se z kódu nepráší!

Apple a C

Mac OS 10.7Xcode 4Command Line Tools for Xcode

http://developer.apple.com

Page 46: Ať se z kódu nepráší!

Xcode 4

Nutné: Mac OS X 10.7

Page 47: Ať se z kódu nepráší!

Objective-C

Syntax: cammelCase

Prefix: NS (od NextStep)

Page 48: Ať se z kódu nepráší!

Hello FI MUNI!

Command + R

Page 49: Ať se z kódu nepráší!

NSString

NSString *who = @”FI MUNI”;

NSLog(“Hello %@!”, who);

Page 50: Ať se z kódu nepráší!

Boolean

BOOL isFIBest = YES;

BOOL isCBoring = NO;

Page 51: Ať se z kódu nepráší!

Správa pamäteNové verzie Mac OS podporujú auto-garbage collection.

Nespoliehajte sa na to!Naučte sa manažovať pamäť.Nefunguje na mobilných zariadeniach.

Page 52: Ať se z kódu nepráší!

Retain Counter

alokácia - zvýšenie počítadla

dealokácia - zníženie počítadla

Page 53: Ať se z kódu nepráší!

Pravidlá

Ak objekt vlastníš, musíš ho uvoľniť.

Ak objekt nevlastníč, nesmieč ho uvoľniť.

Page 54: Ať se z kódu nepráší!

Vlastnenie objektu

alloc, new, retain, copy

Každý ANRC musí mať release!

Page 55: Ať se z kódu nepráší!

Alokácia - new

Page 56: Ať se z kódu nepráší!

Alokácia a inicializácia

Preferovaný spôsob

Page 57: Ať se z kódu nepráší!

Rozdelenie kódu

rozhranie (interface) - *.h

implementácia - *.m

Page 58: Ať se z kódu nepráší!

Interface@interface Employee : NSObject {@private NSString *name; NSString *surname; NSDate *age;}@end

Page 59: Ať se z kódu nepráší!

Implementácia@implementation Employee

- (void) myMethod { NSLog(@"Hello");}

@end

Page 60: Ať se z kódu nepráší!

Znak pred metódou

- (void) myMethod

znak mínus - metóda inštancieznak plus - statická metóda

Page 61: Ať se z kódu nepráší!

Prístup k zdrojomNSURL

NSURL *myURL = [NSURL fileURLWithPath:path];

file:///Users/georgik/file.txt

Page 62: Ať se z kódu nepráší!

Prečo používať NSURL?Pretože to tak Apple povedal.

rýchlejšie na iOSzachytenie chyby v prípade zlého formátuomnoho mocnejšie

Page 63: Ať se z kódu nepráší!

Allegro

podpora aj pre iPhone

Page 64: Ať se z kódu nepráší!

Ať se z kódu nepráší!Juraj Michálek

7. 5. 2012 - FI MUNI - Brnohttp://georgik.sinusgear.comhttp://twitter.com/sinusgear