點學寫靚PROGRAM
一舊膠買起你 2017-4-24 12:05:00 不如我舉個例子
:^(
:^(

假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement


public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}

Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}

public Foo parse1(String input) throws Exception {
Foo ret = new Foo();

// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();

return ret;
}

public Foo parse2(String input) {
Foo ret = new Foo();

// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;

return ret;
}

Ads

淫在橘子裡(找數) 2017-4-24 13:50:00
好啦既然大家咁熱心, 我又要問多幾個問題, 因為我又唔識
:^(
:^(
:^(


throw Exception定係Return Null/Error number 好D?

以java為例
Throw exception真係好煩,
1)有時唔知點分係邊個case
2)淨係見到個try catch block都唔開胃
3)話就話Exception可以上多幾層 stack, 咪又係每個method都係加返throws declaration


Try catch 好好用
Debug 都易啲因爲你會知到邊到出錯
你可以加自己message 同 run time value
同埋會 bubble up

覺得有機會有 exception 就 handle 咗
加埋 finally 去 gracefully terminate 其他嘢

點解會唔開胃
:^(
:^(

點樣加runtime value
:^(
:^(
:^(
:^(
:^(

你唔覺try-catch-finally好佔版面之餘, 仲要D logic飛黎飛去好混亂
:^(

bubble up 又係咩意思
:^(
:^(



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別
淫在橘子裡(找數) 2017-4-24 13:56:06
不如我舉個例子
:^(
:^(

假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement


public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}

Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}

public Foo parse1(String input) throws Exception {
Foo ret = new Foo();

// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();

return ret;
}

public Foo parse2(String input) {
Foo ret = new Foo();

// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;

return ret;
}



個人會 prefer exception. InvalidValueException
Etc. Null 可以解input 係 null string 亦可以解 parameter not in the expected format. 用 exception 可以清楚啲同埋可以 bubble up as needed. Check for null 你要所有 dev 都知你個 null 係乜意思同埋個個都用 null 解同一樣野 個個記得要check

呢啲有IO 有 type conversion 我傾向寫 try catch

冇話啱唔唔嘅我估
純個人偏好
:^(
一舊膠買起你 2017-4-24 14:18:20



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別

我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝
:^(
kekekejjjj 2017-4-24 14:24:36
學左兩年都仲好似半桶水
DESIGN PATTERN學左
但好多技巧都唔識,
E.G.今日睇到Convention over Configuration又發現自己一路做得唔好
:^(
:^(

E.G.落手做做極都唔可以一勞永逸
等等

學到好LOST


Design Pattern係對特定情景既model answer,
但你唔識中間既推導過程, 你遇上open question既時候就會gg

基本上講Design Pattern既書都一定會先講Design Principle
Design Principle才是判斷是否好code既基本工

btw, 個人推薦不寫if學習法
一直堅持唔寫if, 直到必要時才用

未聽過不寫if 學習法
但我認同係會有用
除非用java/C,我覺得不寫loop 學習法都會work
Loading... 2017-4-24 14:35:49 想寫得靚,首先Design pattern 最好睇一次
唔洗刻意用. 因為好多時候每個pattern 都係某個情況出現先會用

例如你寫if else, 發覺不斷重覆,你就可以用design pattern 其中幾招 (factory pattern ) 去remove o左d if else.

又例如你發現想用single object 先用singleton.

每個pattern 都有佢既用處,但又會多o左d限制。


另外最好睇下d arch code
例如MVC MVP MVVM.

呢兩年最興係observer pattern 都好值得睇
例如rx .

多d睇github, 睇下勁人點寫,唔好自己用死某個pattern .

convention over cofigutaion 呢個係ruby on rails 核心既野,有興趣search ruby girl 用三十分鐘玩下有幾神奇
:^(
上高登冇句真 2017-4-24 17:09:13
何必驕枉過正, 我一直都係話三思先用if, if對底層係好重要
但好多時咩為之靚code唔係我扔條絕對rule出黎就可以寫到
寫code前三思好重要

唔明有咩問題,我未見過有program冇if
:^(

sorting點樣唔用if寫到
一舊膠買起你 2017-4-24 17:48:09
你自己既靚program定義係咩先

我就覺得考慮到time complexity, maintenance同readability就已經好夠
第一樣 我覺得真係要望多d 唔同algorithm
第二樣 要你長期做同一個project你先會研究到最好既solution
第三樣 要有teammate插你d code佢睇唔明

所以,都係出去搵份工啦
:^(

以前自學睇過各個要素
我會prefer readability
:^(
:^(
, 然後係maintainability
BTW, 其實我已經返緊SE
:^(
:^(
人生書家 2017-4-24 17:52:25
不如我舉個例子
:^(
:^(

假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement


public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}

Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}

public Foo parse1(String input) throws Exception {
Foo ret = new Foo();

// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();

return ret;
}

public Foo parse2(String input) {
Foo ret = new Foo();

// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;

return ret;
}

其實好多時候都會extend個新嘅exception class出嚟
一舊膠買起你 2017-4-24 18:16:19

其實好多時候都會extend個新嘅exception class出嚟

:^(
:^(
具體係點
:^(
:^(

extends 邊個, 改D咩? 定係加自訂field入去?
巴打實際Project都係咁做?
Angular 2017-4-24 22:01:52
學左兩年都仲好似半桶水
DESIGN PATTERN學左
但好多技巧都唔識,
E.G.今日睇到Convention over Configuration又發現自己一路做得唔好
:^(
:^(

E.G.落手做做極都唔可以一勞永逸
等等

學到好LOST


Design Pattern係對特定情景既model answer,
但你唔識中間既推導過程, 你遇上open question既時候就會gg

基本上講Design Pattern既書都一定會先講Design Principle
Design Principle才是判斷是否好code既基本工

btw, 個人推薦不寫if學習法
一直堅持唔寫if, 直到必要時才用

未聽過不寫if 學習法
但我認同係會有用
除非用java/C,我覺得不寫loop 學習法都會work

當然啦,我自創架
:^(

事實上真係有一個流派係玩極端不寫if,我參考佢地而已

Ads

淫在橘子裡(找數) 2017-4-24 23:07:30
想寫得靚,首先Design pattern 最好睇一次
唔洗刻意用. 因為好多時候每個pattern 都係某個情況出現先會用

例如你寫if else, 發覺不斷重覆,你就可以用design pattern 其中幾招 (factory pattern ) 去remove o左d if else.

又例如你發現想用single object 先用singleton.

每個pattern 都有佢既用處,但又會多o左d限制。


另外最好睇下d arch code
例如MVC MVP MVVM.

呢兩年最興係observer pattern 都好值得睇
例如rx .

多d睇github, 睇下勁人點寫,唔好自己用死某個pattern .

convention over cofigutaion 呢個係ruby on rails 核心既野,有興趣search ruby girl 用三十分鐘玩下有幾神奇
:^(


聽講最近少咗人用 ruby, 但見學過嘅朋友讚口不絕
好似話自從 twitter 唔用佢就少咗好多人用
請問
有冇用過嘅巴絲講解下 RoR 嘅利弊
:^(
:^(
淫在橘子裡(找數) 2017-4-24 23:09:42



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別

我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝
:^(



唔知你是咪真心
:^(


但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java


https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
一舊膠買起你 2017-4-24 23:11:50



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別

我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝
:^(



唔知你是咪真心
:^(


但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java


https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

佢講既message parameter係比你LOG低
你諗住用黎判斷情形?
:^(
:^(
一舊膠買起你 2017-4-24 23:13:08



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別

我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝
:^(



唔知你是咪真心
:^(


但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java


https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

佢講既message parameter係比你LOG低
你諗住用黎判斷情形?
:^(
:^(

C#個Exception有個prop叫Data
果個K/V PAIR就真係比你攝有關EXCEPTION既資料
淫在橘子裡(找數) 2017-4-24 23:25:17



你 catch 完可以 create a new exception with custom message, with run-time value and re throw

咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up

try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose


利申講緊 C#. Java etc 可能有別

我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝
:^(



唔知你是咪真心
:^(


但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java


https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

佢講既message parameter係比你LOG低
你諗住用黎判斷情形?
:^(
:^(

C#個Exception有個prop叫Data
果個K/V PAIR就真係比你攝有關EXCEPTION既資料


唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.

可以放 string 喎
:^(


點解講到咁複雜嘅
:^(
:^(
一舊膠買起你 2017-4-24 23:27:44


唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.

可以放 string 喎
:^(


點解講到咁複雜嘅
:^(
:^(

個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好
淫在橘子裡(找數) 2017-4-24 23:34:26


唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.

可以放 string 喎
:^(


點解講到咁複雜嘅
:^(
:^(

個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好

我一直都係話寫個error message

你可以 throw new exception("error in parameter" + validNumber + " expected but " + actualValue + " found.", ex)

你都可以放多啲你需要用啲 debug 嘅資料入去
完全唔使 parse 乜嘢

本身c# doc 都有用呢個 logging 做 example.
唔明你所指嘅 parsing 係指乜?

Sorry for using string concat and my possible typos. Using my phone right now
一舊膠買起你 2017-4-24 23:38:35


唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.

可以放 string 喎
:^(


點解講到咁複雜嘅
:^(
:^(

個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好

我一直都係話寫個error message

你可以 throw new exception("error in parameter" + validNumber + " expected but " + actualValue + " found.", ex)

你都可以放多啲你需要用啲 debug 嘅資料入去
完全唔使 parse 乜嘢

本身c# doc 都有用呢個 logging 做 example.
唔明你所指嘅 parsing 係指乜?

Sorry for using string concat and my possible typos. Using my phone right now

:^(
:^(
:^(

我講緊既係,你要比program了解你既ERROR係乜
從而係program logic層面進行唔同既應對方案
淫在橘子裡(找數) 2017-4-24 23:54:12


唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.

可以放 string 喎
:^(


點解講到咁複雜嘅
:^(
:^(

個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好

我一直都係話寫個error message

你可以 throw new exception("error in parameter" + validNumber + " expected but " + actualValue + " found.", ex)

你都可以放多啲你需要用啲 debug 嘅資料入去
完全唔使 parse 乜嘢

本身c# doc 都有用呢個 logging 做 example.
唔明你所指嘅 parsing 係指乜?

Sorry for using string concat and my possible typos. Using my phone right now

:^(
:^(
:^(

我講緊既係,你要比program了解你既ERROR係乜
從而係program logic層面進行唔同既應對方案

Exception can bubble up with rethrow so the calling functions know shit happened

If then can't achieve that
一舊膠買起你 2017-4-25 00:01:09

Exception can bubble up with rethrow so the calling functions know shit happened

If then can't achieve that

而家係我唔知你真心定假膠
:^(
同rethrow完全唔關事
:^(

不過我醒起左個解法係點,無野了

Ads

Loading... 2017-4-25 00:35:10
想寫得靚,首先Design pattern 最好睇一次
唔洗刻意用. 因為好多時候每個pattern 都係某個情況出現先會用

例如你寫if else, 發覺不斷重覆,你就可以用design pattern 其中幾招 (factory pattern ) 去remove o左d if else.

又例如你發現想用single object 先用singleton.

每個pattern 都有佢既用處,但又會多o左d限制。


另外最好睇下d arch code
例如MVC MVP MVVM.

呢兩年最興係observer pattern 都好值得睇
例如rx .

多d睇github, 睇下勁人點寫,唔好自己用死某個pattern .

convention over cofigutaion 呢個係ruby on rails 核心既野,有興趣search ruby girl 用三十分鐘玩下有幾神奇
:^(


聽講最近少咗人用 ruby, 但見學過嘅朋友讚口不絕
好似話自從 twitter 唔用佢就少咗好多人用
請問
有冇用過嘅巴絲講解下 RoR 嘅利弊
:^(
:^(



ROR 而家真係冇咁流行,係香港主要係少人識難請人,公司都唔敢用,寫個個走o左就冇人識改

但係學習寫程式,我會好建議學下最基本個個ruby girl 個sample ,學下個idea, 令你感受寫靚code 既重要

RoR 初學好易,但想進一步,好難,非常難,但一熟,就超有效率

同PhP 相反, 學完就會識.


認真,學完RoR 既執著之後,搵食請學javascript
,因為 市場需求好大.
nodeJS , angular ,react 都係呢兩年既會比較流行.


:^(
小弟一開始寫野都係一舊舊,而家都ok ga ,有心學會學得好快

btw, 個人建議
1. design pattern handle到就用pattern
2. 如果冇,用if (唔好迫自己唔用if , 因為係要用得合理,唔係一招用到腦)
3. 如果if 都handle 唔到,先throw exception

寫程式, 最重要有興趣,唔係會好辛苦
:^(
忌廉燴飯 2017-4-25 00:56:53 救命,完全搭唔到嘴
:^(

睇都睇得好辛苦,仲要有睇唔明嘅地方……
Loading... 2017-4-25 00:58:15
救命,完全搭唔到嘴
:^(

睇都睇得好辛苦,仲要有睇唔明嘅地方……

sorry, 我最初都係咁,唔明人地up乜,太多名字唔明...

你唔明邊句我可以慢慢解釋返
Angular 2017-4-25 01:08:00
救命,完全搭唔到嘴
:^(

睇都睇得好辛苦,仲要有睇唔明嘅地方……

我建議你開新post問
呢個post講寫靚code, 從一開始就推高左個門檻...